diff --git a/crates/oxc_formatter/src/parentheses/expression.rs b/crates/oxc_formatter/src/parentheses/expression.rs index 1ba13bc47a812..d3b767ca2f12e 100644 --- a/crates/oxc_formatter/src/parentheses/expression.rs +++ b/crates/oxc_formatter/src/parentheses/expression.rs @@ -263,13 +263,7 @@ impl NeedsParentheses<'_> for AstNode<'_, StaticMemberExpression<'_>> { return false; } - self.is_new_callee() && { - ExpressionLeftSide::Expression(self.object()).iter().any(|expr| { - matches!(expr, ExpressionLeftSide::Expression(e) if - matches!(e.as_ref(), Expression::CallExpression(_)) - ) - }) - } + self.is_new_callee() && member_chain_callee_needs_parens(&self.object) } } diff --git a/crates/oxc_formatter/tests/fixtures/ts/new-expression/issue-16203.ts b/crates/oxc_formatter/tests/fixtures/ts/new-expression/issue-16203.ts new file mode 100644 index 0000000000000..84ab58d5f44ce --- /dev/null +++ b/crates/oxc_formatter/tests/fixtures/ts/new-expression/issue-16203.ts @@ -0,0 +1,16 @@ +const i1 = new (X() as I).y; +const i2 = new (X() as I); +const i3 = new (X()).y; +const i4 = new (X()); + +new (a.b)(); +new (a?.b)(); + +// js/new-expression/new_expression.js +new (x()``.y)(); + +// typescript/non-null/parens.ts +const c3 = new (d()!.e)(); +new (x()``!.y)(); +new (x()!``.y)(); +new (x!()``.y)(); diff --git a/crates/oxc_formatter/tests/fixtures/ts/new-expression/issue-16203.ts.snap b/crates/oxc_formatter/tests/fixtures/ts/new-expression/issue-16203.ts.snap new file mode 100644 index 0000000000000..902586465baf0 --- /dev/null +++ b/crates/oxc_formatter/tests/fixtures/ts/new-expression/issue-16203.ts.snap @@ -0,0 +1,63 @@ +--- +source: crates/oxc_formatter/tests/fixtures/mod.rs +--- +==================== Input ==================== +const i1 = new (X() as I).y; +const i2 = new (X() as I); +const i3 = new (X()).y; +const i4 = new (X()); + +new (a.b)(); +new (a?.b)(); + +// js/new-expression/new_expression.js +new (x()``.y)(); + +// typescript/non-null/parens.ts +const c3 = new (d()!.e)(); +new (x()``!.y)(); +new (x()!``.y)(); +new (x!()``.y)(); + +==================== Output ==================== +------------------ +{ printWidth: 80 } +------------------ +const i1 = new (X() as I).y(); +const i2 = new (X() as I)(); +const i3 = new (X().y)(); +const i4 = new (X())(); + +new a.b(); +new (a?.b)(); + +// js/new-expression/new_expression.js +new (x()``.y)(); + +// typescript/non-null/parens.ts +const c3 = new (d()!.e)(); +new (x()``!.y)(); +new (x()!``.y)(); +new (x!()``.y)(); + +------------------- +{ printWidth: 100 } +------------------- +const i1 = new (X() as I).y(); +const i2 = new (X() as I)(); +const i3 = new (X().y)(); +const i4 = new (X())(); + +new a.b(); +new (a?.b)(); + +// js/new-expression/new_expression.js +new (x()``.y)(); + +// typescript/non-null/parens.ts +const c3 = new (d()!.e)(); +new (x()``!.y)(); +new (x()!``.y)(); +new (x!()``.y)(); + +===================== End =====================