Skip to content

Commit c79c007

Browse files
committed
fix(formatter): should not wrap parenthesis for ComputedMemberExpression when it is not an option or it doesn't contain a call expression
1 parent de66dc5 commit c79c007

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

crates/oxc_formatter/src/parentheses/expression.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ impl NeedsParentheses<'_> for AstNode<'_, MemberExpression<'_>> {
262262
impl NeedsParentheses<'_> for AstNode<'_, ComputedMemberExpression<'_>> {
263263
fn needs_parentheses(&self, f: &Formatter<'_, '_>) -> bool {
264264
matches!(self.parent, AstNodes::NewExpression(_))
265-
&& (!self.optional || member_chain_callee_needs_parens(&self.expression))
265+
&& (self.optional || member_chain_callee_needs_parens(&self.object))
266266
}
267267
}
268268

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
new (get(win))[ty](xxx);
22
new (get(win))[ty][ty](xxx);
3+
4+
new (A?.[ty])(xxx);
5+
new (A?.B[ty])(xxx);
6+
7+
new window['TouchEvent'](xxx);

crates/oxc_formatter/tests/fixtures/js/computed-members/parenthesis.js.snap

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,18 @@ source: crates/oxc_formatter/tests/fixtures/mod.rs
55
new (get(win))[ty](xxx);
66
new (get(win))[ty][ty](xxx);
77

8+
new (A?.[ty])(xxx);
9+
new (A?.B[ty])(xxx);
10+
11+
new window['TouchEvent'](xxx);
12+
813
==================== Output ====================
914
new (get(win)[ty])(xxx);
1015
new (get(win)[ty][ty])(xxx);
1116

17+
new (A?.[ty])(xxx);
18+
new (A?.B[ty])(xxx);
19+
20+
new window["TouchEvent"](xxx);
21+
1222
===================== End =====================

0 commit comments

Comments
 (0)