Fix edge cases for parentheses in new expressions (#19295)

This commit is contained in:
fisker Cheung authored and GitHub committed 2026-06-05 17:16:35 +08:00
1 parent bfa620fa45
commit 96f7a375bd
8 files changed
+63 -1

No files matched your search

@@ -787,6 +787,7 @@ function needsParentheses(path, options) {
case "CallExpression":
case "MemberExpression":
case "TaggedTemplateExpression":
case "ImportExpression":
if (
key === "callee" &&
(parent.type === "BindExpression" || parent.type === "NewExpression")
@@ -795,6 +796,7 @@ function needsParentheses(path, options) {
while (object) {
switch (object.type) {
case "CallExpression":
case "ImportExpression":
return true;
case "MemberExpression":
case "OptionalMemberExpression":
@@ -166,6 +166,10 @@ new f(a::b);
f[a::b];
f[a::b()];
a::(import("foo"));
a::(import("foo").bar);
a::(import("foo")\`bar\`);
=====================================output=====================================
;(a || b)::c
a || b::c
@@ -202,6 +206,10 @@ new f(a::b)
f[a::b]
f[a::b()]
a::(import("foo"))
a::(import("foo").bar)
a::(import("foo")\`bar\`)
================================================================================
`;
@@ -284,6 +292,10 @@ new f(a::b);
f[a::b];
f[a::b()];
a::(import("foo"));
a::(import("foo").bar);
a::(import("foo")\`bar\`);
=====================================output=====================================
(a || b)::c;
a || b::c;
@@ -320,6 +332,10 @@ new f(a::b);
f[a::b];
f[a::b()];
a::(import("foo"));
a::(import("foo").bar);
a::(import("foo")\`bar\`);
================================================================================
`;
@@ -32,3 +32,7 @@ new (a::b)();
new f(a::b);
f[a::b];
f[a::b()];
a::(import("foo"));
a::(import("foo").bar);
a::(import("foo")`bar`);
@@ -19,7 +19,7 @@ new (factory())(factory());
================================================================================
`;
exports[`new_expression.js format 1`] = `
exports[`new-expression.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80 (default) |
@@ -48,6 +48,23 @@ new (a\`\`())();
================================================================================
`;
exports[`parentheses.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80 (default) |
=====================================input======================================
new (import("foo"))();
new (import("foo").bar)();
new (import("foo")\`bar\`)();
=====================================output=====================================
new (import("foo"))();
new (import("foo").bar)();
new (import("foo")\`bar\`)();
================================================================================
`;
exports[`with-member-expression.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
@@ -0,0 +1,3 @@
new (import("foo"))();
new (import("foo").bar)();
new (import("foo")`bar`)();
@@ -140,6 +140,23 @@ interface TimerConstructor {
================================================================================
`;
exports[`parentheses.ts format 1`] = `
====================================options=====================================
parsers: ["typescript"]
printWidth: 80 (default) |
=====================================input======================================
new (import("foo")!)();
new (import("foo")!.bar)();
new (import("foo")!\`bar\`)();
=====================================output=====================================
new (import("foo")!)();
new (import("foo")!.bar)();
new (import("foo")!\`bar\`)();
================================================================================
`;
exports[`with-member-expression.ts format 1`] = `
====================================options=====================================
parsers: ["typescript"]
@@ -0,0 +1,3 @@
new (import("foo")!)();
new (import("foo")!.bar)();
new (import("foo")!`bar`)();