Skip to content

Commit 3c70a06

Browse files
committed
fix(formatter): print parenthesis for sequence expression in ReturnStatement and ExpressionStatement
1 parent 3b6fc54 commit 3c70a06

File tree

4 files changed

+13
-26
lines changed

4 files changed

+13
-26
lines changed

crates/oxc_formatter/src/parentheses/expression.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -579,14 +579,15 @@ impl NeedsParentheses<'_> for AstNode<'_, SequenceExpression<'_>> {
579579
return false;
580580
}
581581

582-
!matches!(
583-
self.parent,
582+
match self.parent {
584583
AstNodes::ReturnStatement(_)
584+
| AstNodes::ThrowStatement(_)
585585
// There's a precedence for writing `x++, y++`
586586
| AstNodes::ForStatement(_)
587-
| AstNodes::ExpressionStatement(_)
588-
| AstNodes::SequenceExpression(_)
589-
)
587+
| AstNodes::SequenceExpression(_) => false,
588+
AstNodes::ExpressionStatement(stmt) => !stmt.is_arrow_function_body(),
589+
_ => true,
590+
}
590591
}
591592
}
592593

crates/oxc_formatter/src/write/return_or_throw_statement.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl<'a> Format<'a> for FormatAdjacentArgument<'a, '_> {
9595
&& has_argument_leading_comments(argument, f)
9696
{
9797
write!(f, [text("("), &block_indent(&argument), text(")")])
98-
} else if is_binary_or_sequence_argument(argument) {
98+
} else if argument.is_binaryish() {
9999
write!(
100100
f,
101101
[group(&format_args!(
@@ -104,6 +104,8 @@ impl<'a> Format<'a> for FormatAdjacentArgument<'a, '_> {
104104
if_group_breaks(&text(")"))
105105
))]
106106
)
107+
} else if matches!(argument.as_ref(), Expression::SequenceExpression(_)) {
108+
write!(f, [group(&format_args!(text("("), soft_block_indent(&argument), text(")")))])
107109
} else {
108110
write!(f, argument)
109111
}
@@ -168,11 +170,7 @@ fn has_argument_leading_comments(argument: &AstNode<Expression>, f: &Formatter<'
168170
false
169171
}
170172

173+
#[inline]
171174
fn is_binary_or_sequence_argument(argument: &Expression) -> bool {
172-
matches!(
173-
argument,
174-
Expression::BinaryExpression(_)
175-
| Expression::LogicalExpression(_)
176-
| Expression::SequenceExpression(_)
177-
)
175+
matches!(argument, Expression::BinaryExpression(_) | Expression::LogicalExpression(_))
178176
}

tasks/prettier_conformance/snapshots/prettier.js.snap.md

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
js compatibility: 673/749 (89.85%)
1+
js compatibility: 684/749 (91.32%)
22

33
# Failed
44

@@ -43,24 +43,14 @@ js compatibility: 673/749 (89.85%)
4343
| js/last-argument-expansion/dangling-comment-in-arrow-function.js | 💥 | 22.22% |
4444
| js/logical-expressions/multiple-comments/17192.js | 💥 | 60.00% |
4545
| js/method-chain/issue-17457.js | 💥 | 0.00% |
46-
| js/no-semi/issue2006.js | 💥💥 | 75.00% |
47-
| js/no-semi/no-semi.js | 💥💥 | 98.90% |
4846
| js/object-multiline/multiline.js | 💥✨ | 22.22% |
4947
| js/quote-props/classes.js | 💥💥✨✨ | 47.06% |
5048
| js/quote-props/objects.js | 💥💥✨✨ | 45.10% |
5149
| js/quote-props/with_numbers.js | 💥💥✨✨ | 46.43% |
5250
| js/quotes/objects.js | 💥💥 | 80.00% |
5351
| js/require/comments.js | 💥 | 81.25% |
5452
| js/require/long-module-name.js | 💥 | 18.18% |
55-
| js/reserved-word/interfaces.js | 💥 | 85.71% |
56-
| js/reserved-word/let.js | 💥 | 85.71% |
57-
| js/reserved-word/yield.js | 💥 | 85.71% |
58-
| js/sequence-break/break.js | 💥 | 90.91% |
59-
| js/sequence-expression/expression.js | 💥 | 33.33% |
6053
| js/sequence-expression/ignored.js | 💥 | 25.00% |
61-
| js/sequence-expression/return.js | 💥 | 50.00% |
62-
| js/sequence-expression/no-semi/expression.js | 💥 | 50.00% |
63-
| js/strings/non-octal-eight-and-nine.js | 💥💥 | 71.43% |
6454
| js/strings/template-literals.js | 💥💥 | 98.01% |
6555
| js/ternaries/binary.js | 💥💥💥💥✨✨✨✨ | 18.42% |
6656
| js/ternaries/func-call.js | 💥💥💥💥✨✨✨✨ | 25.00% |
@@ -76,7 +66,6 @@ js compatibility: 673/749 (89.85%)
7666
| js/trailing-comma/dynamic-import.js | 💥💥💥 | 0.00% |
7767
| jsx/fbt/test.js | 💥 | 84.06% |
7868
| jsx/jsx/quotes.js | 💥💥💥💥 | 79.41% |
79-
| jsx/jsx/regex.js | 💥💥💥💥 | 75.00% |
8069
| jsx/optional-chaining/optional-chaining.jsx | 💥 | 85.96% |
8170
| jsx/single-attribute-per-line/single-attribute-per-line.js | 💥✨ | 43.37% |
8271
| jsx/text-wrap/test.js | 💥 | 99.56% |

tasks/prettier_conformance/snapshots/prettier.ts.snap.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
ts compatibility: 537/598 (89.80%)
1+
ts compatibility: 538/598 (89.97%)
22

33
# Failed
44

55
| Spec path | Failed or Passed | Match ratio |
66
| :-------- | :--------------: | :---------: |
77
| jsx/fbt/test.js | 💥 | 84.06% |
88
| jsx/jsx/quotes.js | 💥💥💥💥 | 79.41% |
9-
| jsx/jsx/regex.js | 💥💥💥💥 | 75.00% |
109
| jsx/optional-chaining/optional-chaining.jsx | 💥 | 85.96% |
1110
| jsx/single-attribute-per-line/single-attribute-per-line.js | 💥✨ | 43.37% |
1211
| jsx/text-wrap/test.js | 💥 | 99.56% |

0 commit comments

Comments
 (0)