Skip to content

Commit 505252c

Browse files
committed
feat(formatter): wrap parenthesis for AssignmentExpression that is a key of PropertyDefinition (#15243)
Align the latest Prettier behavior, which hasn't been released yet.
1 parent 99e520f commit 505252c

File tree

3 files changed

+21
-18
lines changed

3 files changed

+21
-18
lines changed

crates/oxc_formatter/src/parentheses/expression.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -531,22 +531,27 @@ impl NeedsParentheses<'_> for AstNode<'_, AssignmentExpression<'_>> {
531531
AstNodes::SequenceExpression(sequence) => {
532532
// Skip through SequenceExpression and ParenthesizedExpression ancestors
533533
if let Some(ancestor) = self.ancestors().find(|p| {
534-
!matches!(p, AstNodes::SequenceExpression(_) | AstNodes::ParenthesizedExpression(_))
535-
}) && let AstNodes::ForStatement(for_stmt) = ancestor {
536-
let is_initializer = for_stmt
537-
.init
538-
.as_ref()
539-
.is_some_and(|init| init.span().contains_inclusive(self.span()));
540-
let is_update = for_stmt.update.as_ref().is_some_and(|update| {
541-
update.span().contains_inclusive(self.span())
542-
});
543-
return !(is_initializer || is_update);
544-
}
534+
!matches!(
535+
p,
536+
AstNodes::SequenceExpression(_) | AstNodes::ParenthesizedExpression(_)
537+
)
538+
}) && let AstNodes::ForStatement(for_stmt) = ancestor
539+
{
540+
let is_initializer = for_stmt
541+
.init
542+
.as_ref()
543+
.is_some_and(|init| init.span().contains_inclusive(self.span()));
544+
let is_update = for_stmt
545+
.update
546+
.as_ref()
547+
.is_some_and(|update| update.span().contains_inclusive(self.span()));
548+
return !(is_initializer || is_update);
549+
}
545550

546551
true
547552
}
548-
// `interface { [a = 1]; }` and `class { [a = 1]; }` not need parens
549-
AstNodes::TSPropertySignature(_) | AstNodes::PropertyDefinition(_) |
553+
// `interface A { [a = 1]; }` not need parens
554+
AstNodes::TSPropertySignature(_) |
550555
// Never need parentheses in these contexts:
551556
// - `a = (b = c)` = nested assignments don't need extra parens
552557
AstNodes::AssignmentExpression(_) => false,
@@ -567,6 +572,7 @@ impl NeedsParentheses<'_> for AstNode<'_, AssignmentExpression<'_>> {
567572
// - `new (a = b)`
568573
// - `(a = b).prop`
569574
// - `await (a = b)`
575+
// - `class { [a = 1]; }`
570576
// - etc.
571577
_ => true,
572578
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
js compatibility: 696/749 (92.92%)
1+
js compatibility: 698/749 (93.19%)
22

33
# Failed
44

55
| Spec path | Failed or Passed | Match ratio |
66
| :-------- | :--------------: | :---------: |
77
| js/arrows/comment.js | 💥💥 | 88.89% |
8-
| js/assignment-expression/property-key.js | 💥 | 88.89% |
9-
| js/assignment-expression/property-value.js | 💥 | 88.89% |
108
| js/call/boolean/boolean.js | 💥 | 77.88% |
119
| js/class-comment/misc.js | 💥 | 72.73% |
1210
| js/class-comment/superclass.js | 💥 | 95.35% |

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
ts compatibility: 544/598 (90.97%)
1+
ts compatibility: 545/598 (91.14%)
22

33
# Failed
44

@@ -25,7 +25,6 @@ ts compatibility: 544/598 (90.97%)
2525
| typescript/comments/16121.ts | 💥 | 72.46% |
2626
| typescript/comments/mapped_types.ts | 💥 | 96.77% |
2727
| typescript/comments/method_types.ts | 💥 | 82.05% |
28-
| typescript/compiler/indexSignatureWithInitializer.ts | 💥 | 87.50% |
2928
| typescript/conditional-types/comments.ts | 💥✨ | 31.51% |
3029
| typescript/conditional-types/conditional-types.ts | 💥✨ | 34.48% |
3130
| typescript/conditional-types/infer-type.ts | 💥✨ | 4.76% |

0 commit comments

Comments
 (0)