Fix edge cases with parentheses around optional chaining (#7500)
* fix disappearing parens in (foo?.bar)() * fix `new (foo?.bar)();`
This commit is contained in:
1 parent
54cce374da
commit
850e705d10
5 files changed
+42
-2
No files matched your search
@@ -0,0 +1,16 @@
|
||||
#### Fix edge cases with parentheses around optional chaining ([#7500](https://github.com/prettier/prettier/pull/7500) by [@thorn0](https://github.com/thorn0))
|
||||
|
||||
<!-- prettier-ignore -->
|
||||
```jsx
|
||||
// Input
|
||||
(foo?.bar)();
|
||||
new (foo?.bar)();
|
||||
|
||||
// Prettier stable
|
||||
foo?.bar();
|
||||
new foo?.bar();
|
||||
|
||||
// Prettier master
|
||||
(foo?.bar)();
|
||||
new (foo?.bar)();
|
||||
```
|
||||
@@ -676,7 +676,9 @@ function needsParens(path, options) {
|
||||
case "OptionalCallExpression":
|
||||
if (
|
||||
(parent.type === "MemberExpression" && name === "object") ||
|
||||
(parent.type === "CallExpression" && name === "callee")
|
||||
((parent.type === "CallExpression" ||
|
||||
parent.type === "NewExpression") &&
|
||||
name === "callee")
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1194,7 +1194,11 @@ function printPathNoParens(path, options, print, args) {
|
||||
|
||||
// We detect calls on member lookups and possibly print them in a
|
||||
// special chain format. See `printMemberChain` for more info.
|
||||
if (!isNew && isMemberish(n.callee)) {
|
||||
if (
|
||||
!isNew &&
|
||||
isMemberish(n.callee) &&
|
||||
!path.call(path => pathNeedsParens(path, options), "callee")
|
||||
) {
|
||||
return printMemberChain(path, options, print);
|
||||
}
|
||||
|
||||
|
||||
@@ -46,6 +46,8 @@ a?.[b?.c]?.d();
|
||||
|
||||
(one?.fn());
|
||||
(one?.two).fn();
|
||||
(one?.two)();
|
||||
(one?.two())();
|
||||
(one.two?.fn());
|
||||
(one.two?.three).fn();
|
||||
(one.two?.three?.fn());
|
||||
@@ -84,6 +86,10 @@ a = () => ({}?.b() && a);
|
||||
({}?.a().b());
|
||||
({ a: 1 }?.entries());
|
||||
|
||||
new (foo?.bar)();
|
||||
new (foo?.bar())();
|
||||
new (foo?.())();
|
||||
|
||||
=====================================output=====================================
|
||||
var street = user.address?.street;
|
||||
var fooValue = myForm.querySelector("input[name=foo]")?.value;
|
||||
@@ -125,6 +131,8 @@ a?.[b?.c]?.d();
|
||||
|
||||
one?.fn();
|
||||
(one?.two).fn();
|
||||
(one?.two)();
|
||||
(one?.two())();
|
||||
one.two?.fn();
|
||||
(one.two?.three).fn();
|
||||
one.two?.three?.fn();
|
||||
@@ -167,6 +175,10 @@ a = () => ({}?.b() && a);
|
||||
({}?.a().b());
|
||||
({ a: 1 }?.entries());
|
||||
|
||||
new (foo?.bar)();
|
||||
new (foo?.bar())();
|
||||
new (foo?.())();
|
||||
|
||||
================================================================================
|
||||
`;
|
||||
|
||||
|
||||
@@ -38,6 +38,8 @@ a?.[b?.c]?.d();
|
||||
|
||||
(one?.fn());
|
||||
(one?.two).fn();
|
||||
(one?.two)();
|
||||
(one?.two())();
|
||||
(one.two?.fn());
|
||||
(one.two?.three).fn();
|
||||
(one.two?.three?.fn());
|
||||
@@ -75,3 +77,7 @@ a = () => ({}?.b() && a);
|
||||
(x) => ({}?.b.b);
|
||||
({}?.a().b());
|
||||
({ a: 1 }?.entries());
|
||||
|
||||
new (foo?.bar)();
|
||||
new (foo?.bar())();
|
||||
new (foo?.())();
|
||||
Reference in new issue
Block a user