Skip to content

Commit 190bb1e

Browse files
committed
fix(formatter): keep parens around an instantiation expression ending an operand through any tight parent (#28168)
Closes #28108 Fixes `(<any>(make<T>)) + y` (#28108) case, and also `(#p in make<T>) > y` and `(<A>await make<T>) + y` cases.
1 parent a5314a1 commit 190bb1e

3 files changed

Lines changed: 55 additions & 24 deletions

File tree

‎crates/oxc_formatter/src/parentheses/expression.rs‎

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -908,7 +908,7 @@ fn class_extends_needs_parens_through_non_null(expression: &Expression<'_>) -> b
908908
}
909909

910910
impl NeedsParentheses<'_> for AstNode<'_, '_, TSInstantiationExpression<'_>> {
911-
fn needs_parentheses(&self, _f: &JsFormatter<'_, '_>) -> bool {
911+
fn needs_parentheses(&self, f: &JsFormatter<'_, '_>) -> bool {
912912
let object = match self.parent() {
913913
AstNodes::StaticMemberExpression(expr) => &expr.object,
914914
AstNodes::ComputedMemberExpression(expr) => &expr.object,
@@ -920,7 +920,7 @@ impl NeedsParentheses<'_> for AstNode<'_, '_, TSInstantiationExpression<'_>> {
920920
AstNodes::CallExpression(expr) if expr.type_arguments.is_some() => &expr.callee,
921921
AstNodes::NewExpression(expr) if expr.type_arguments.is_some() => &expr.callee,
922922
AstNodes::TaggedTemplateExpression(expr) if expr.type_arguments.is_some() => &expr.tag,
923-
_ => return is_followed_by_comparison_or_sign(self),
923+
_ => return is_followed_by_comparison_or_sign(self, f),
924924
};
925925

926926
self.span == object.span()
@@ -929,34 +929,43 @@ impl NeedsParentheses<'_> for AstNode<'_, '_, TSInstantiationExpression<'_>> {
929929

930930
/// Whether the instantiation expression ends the left operand of `+`, `-`, `<` or `>`-led operators:
931931
/// TypeScript reads `f<T> + x` as `f < T > +x` and rejects `f<T> < x`,
932-
/// also when `f<T>` only ends the operand (`(a + f<T>) - x`, `(typeof f<T>) + x`).
932+
/// also when `f<T>` only ends that operand without parentheses (`(a + f<T>) - x`, `(<A>await f<T>) + x`).
933933
fn is_followed_by_comparison_or_sign(
934934
node: &AstNode<'_, '_, TSInstantiationExpression<'_>>,
935+
f: &JsFormatter<'_, '_>,
935936
) -> bool {
936937
let mut span = node.span;
937-
let mut parent = node.parent();
938-
loop {
939-
match parent {
940-
AstNodes::BinaryExpression(expr) if expr.right.span() == span => {
941-
(span, parent) = (expr.span, expr.parent());
942-
}
943-
AstNodes::UnaryExpression(expr) => (span, parent) = (expr.span, expr.parent()),
944-
AstNodes::BinaryExpression(expr) => {
945-
return expr.left.span() == span
946-
&& matches!(
947-
expr.operator,
948-
BinaryOperator::Addition
949-
| BinaryOperator::Subtraction
950-
| BinaryOperator::LessThan
951-
| BinaryOperator::GreaterThan
952-
| BinaryOperator::GreaterEqualThan
953-
| BinaryOperator::ShiftRight
954-
| BinaryOperator::ShiftRightZeroFill
955-
);
956-
}
957-
_ => return false,
938+
for ancestor in node.ancestors() {
939+
if let AstNodes::BinaryExpression(expr) = ancestor
940+
&& expr.left.span() == span
941+
{
942+
return matches!(
943+
expr.operator,
944+
BinaryOperator::Addition
945+
| BinaryOperator::Subtraction
946+
| BinaryOperator::LessThan
947+
| BinaryOperator::GreaterThan
948+
| BinaryOperator::GreaterEqualThan
949+
| BinaryOperator::ShiftRight
950+
| BinaryOperator::ShiftRightZeroFill
951+
);
952+
}
953+
// `f<T>` still ends these as the right or only operand, unless their own parentheses close it.
954+
// Looser ones (`a && f<T>`, `yield f<T>`, ...) always print them before a binary operator.
955+
if !matches!(
956+
ancestor,
957+
AstNodes::BinaryExpression(_)
958+
| AstNodes::PrivateInExpression(_)
959+
| AstNodes::UnaryExpression(_)
960+
| AstNodes::AwaitExpression(_)
961+
| AstNodes::TSTypeAssertion(_)
962+
) || prints_own_parens(ancestor, f)
963+
{
964+
return false;
958965
}
966+
span = ancestor.span();
959967
}
968+
false
960969
}
961970

962971
fn binary_like_needs_parens(

‎crates/oxc_formatter/tests/fixtures/ts/parenthesis/instantiation-expression-parens.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ x = (make<T>) + y;
1010
x = (make<T>) >> y;
1111
x = (a + make<T>) - y;
1212
x = (typeof make<T>) + y;
13+
x = (<any>(make<T>)) + y;
14+
x = (<A>await make<T>) + y;
15+
class C { #p; m() { x = (#p in make<T>) > y; } }
1316
export default (class {})<string>;
1417
export default (function () {})<string>;
1518
export default (class {})<string> as A;

‎crates/oxc_formatter/tests/fixtures/ts/parenthesis/instantiation-expression-parens.ts.snap‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ x = (make<T>) + y;
1414
x = (make<T>) >> y;
1515
x = (a + make<T>) - y;
1616
x = (typeof make<T>) + y;
17+
x = (<any>(make<T>)) + y;
18+
x = (<A>await make<T>) + y;
19+
class C { #p; m() { x = (#p in make<T>) > y; } }
1720
export default (class {})<string>;
1821
export default (function () {})<string>;
1922
export default (class {})<string> as A;
@@ -46,6 +49,14 @@ x = (make<T>) + y;
4649
x = (make<T>) >> y;
4750
x = a + (make<T>) - y;
4851
x = typeof (make<T>) + y;
52+
x = <any>(make<T>) + y;
53+
x = <A>await (make<T>) + y;
54+
class C {
55+
#p;
56+
m() {
57+
x = #p in (make<T>) > y;
58+
}
59+
}
4960
export default (class {})<string>;
5061
export default (function () {})<string>;
5162
export default (class {})<string> as A;
@@ -77,6 +88,14 @@ x = (make<T>) + y;
7788
x = (make<T>) >> y;
7889
x = a + (make<T>) - y;
7990
x = typeof (make<T>) + y;
91+
x = <any>(make<T>) + y;
92+
x = <A>await (make<T>) + y;
93+
class C {
94+
#p;
95+
m() {
96+
x = #p in (make<T>) > y;
97+
}
98+
}
8099
export default (class {})<string>;
81100
export default (function () {})<string>;
82101
export default (class {})<string> as A;

0 commit comments

Comments
 (0)