Skip to content

Commit dc57a2b

Browse files
committed
fix(formatter): incorrect handling of VariableDeclarator with an ArrowFunctionExpression initializer (#14731)
1 parent 537185d commit dc57a2b

File tree

3 files changed

+43
-14
lines changed

3 files changed

+43
-14
lines changed

crates/oxc_formatter/src/utils/assignment_like.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ impl<'a> AssignmentLike<'a, '_> {
392392
return AssignmentLikeLayout::BreakAfterOperator;
393393
}
394394

395-
if self.should_break_left_hand_side() {
395+
if self.should_break_left_hand_side(left_may_break) {
396396
return AssignmentLikeLayout::BreakLeftHandSide;
397397
}
398398

@@ -518,7 +518,7 @@ impl<'a> AssignmentLike<'a, '_> {
518518

519519
/// Particular function that checks if the left hand side of a [AssignmentLike] should
520520
/// be broken on multiple lines
521-
fn should_break_left_hand_side(&self) -> bool {
521+
fn should_break_left_hand_side(&self, left_may_break: bool) -> bool {
522522
if self.is_complex_destructuring() {
523523
return true;
524524
}
@@ -534,9 +534,11 @@ impl<'a> AssignmentLike<'a, '_> {
534534
let type_annotation = declarator.id.type_annotation.as_ref();
535535

536536
type_annotation.is_some_and(|ann| is_complex_type_annotation(ann))
537-
|| (self.get_right_expression().is_some_and(|expr| {
538-
matches!(expr.as_ref(), Expression::ArrowFunctionExpression(_))
539-
}) && type_annotation.is_some_and(|ann| is_annotation_breakable(ann)))
537+
|| (left_may_break
538+
&& declarator
539+
.init
540+
.as_ref()
541+
.is_some_and(|expr| matches!(expr, Expression::ArrowFunctionExpression(_))))
540542
}
541543

542544
/// Checks if the current assignment is eligible for [AssignmentLikeLayout::BreakAfterOperator]
@@ -1009,15 +1011,6 @@ fn is_complex_type_arguments(type_arguments: &TSTypeParameterInstantiation) -> b
10091011
false
10101012
}
10111013

1012-
/// Checks if the annotation is breakable
1013-
fn is_annotation_breakable(annotation: &TSTypeAnnotation) -> bool {
1014-
matches!(
1015-
&annotation.type_annotation,
1016-
TSType::TSTypeReference(reference_type)
1017-
if reference_type.type_arguments.as_ref().is_some_and(|type_args| !type_args.params.is_empty())
1018-
)
1019-
}
1020-
10211014
/// [Prettier applies]: <https://github.com/prettier/prettier/blob/fde0b49d7866e203ca748c306808a87b7c15548f/src/language-js/print/assignment.js#L278>
10221015
pub fn is_complex_type_annotation(annotation: &TSTypeAnnotation) -> bool {
10231016
match &annotation.type_annotation {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
const onPanning: ComponenASDtProps<
3+
typeof TransformWrapper
4+
>["onPanning"] = () => {
5+
}
6+
}
7+
const onPanning: ComponenASDtProps<
8+
typeof TransformWrapper
9+
>["onPanning"] = () => {
10+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
source: crates/oxc_formatter/tests/fixtures/mod.rs
3+
---
4+
==================== Input ====================
5+
{
6+
const onPanning: ComponenASDtProps<
7+
typeof TransformWrapper
8+
>["onPanning"] = () => {
9+
}
10+
}
11+
const onPanning: ComponenASDtProps<
12+
typeof TransformWrapper
13+
>["onPanning"] = () => {
14+
}
15+
16+
==================== Output ====================
17+
{
18+
const onPanning: ComponenASDtProps<
19+
typeof TransformWrapper
20+
>["onPanning"] = () => {};
21+
}
22+
const onPanning: ComponenASDtProps<
23+
typeof TransformWrapper
24+
>["onPanning"] = () => {};
25+
26+
===================== End =====================

0 commit comments

Comments
 (0)