Keep parens around non-null assertions for new-expressions only (#6140)
parent
2c914c7c1a
commit
b3adb46ba9
|
@ -325,22 +325,19 @@ export default (function log() {}).toString();
|
|||
export default (function log() {} as typeof console.log);
|
||||
```
|
||||
|
||||
### TypeScript: Keep parentheses around a function called with non-null assertion. ([6136] by [@sosukesuzuki])
|
||||
### TypeScript: Keep necessary parentheses around non-null assertions. ([#6136] by [@sosukesuzuki], [#6140] by [@thorn0])
|
||||
|
||||
Previously, Prettier removes necessary parentheses around a call expression with non-null assertion. It happens when it's return value is called as function.
|
||||
Previously, Prettier removed necessary parentheses around non-null assertions if the result of the assertion expression was called as a constructor.
|
||||
|
||||
<!-- prettier-ignore -->
|
||||
```ts
|
||||
// Input
|
||||
const a = (b()!)();
|
||||
const b = new (c()!)();
|
||||
|
||||
// Output (Prettier stable)
|
||||
const a = b()!();
|
||||
const b = new c()!();
|
||||
|
||||
// Output (prettier master)
|
||||
const a = (b()!)();
|
||||
// Output (Prettier master)
|
||||
const b = new (c()!)();
|
||||
```
|
||||
|
||||
|
@ -359,6 +356,7 @@ const b = new (c()!)();
|
|||
[#6131]: https://github.com/prettier/prettier/pull/6131
|
||||
[#6133]: https://github.com/prettier/prettier/pull/6133
|
||||
[#6136]: https://github.com/prettier/prettier/pull/6136
|
||||
[#6140]: https://github.com/prettier/prettier/pull/6140
|
||||
[@belochub]: https://github.com/belochub
|
||||
[@brainkim]: https://github.com/brainkim
|
||||
[@duailibe]: https://github.com/duailibe
|
||||
|
@ -367,3 +365,4 @@ const b = new (c()!)();
|
|||
[@jridgewell]: https://github.com/jridgewell
|
||||
[@jwbay]: https://github.com/jwbay
|
||||
[@sosukesuzuki]: https://github.com/sosukesuzuki
|
||||
[@thorn0]: https://github.com/thorn0
|
||||
|
|
|
@ -725,14 +725,12 @@ function needsParens(path, options) {
|
|||
return false;
|
||||
}
|
||||
return true;
|
||||
|
||||
case "TSNonNullExpression": {
|
||||
if (
|
||||
node.expression.type !== "Identifier" &&
|
||||
(parent.type === "CallExpression" || parent.type === "NewExpression")
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
return (
|
||||
node.expression.type === "CallExpression" &&
|
||||
parent.type === "NewExpression"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -56,9 +56,12 @@ function* g() {
|
|||
return (yield * foo())!;
|
||||
}
|
||||
|
||||
const a = (b()!)();
|
||||
const a = (b()!)(); // parens aren't necessary
|
||||
const b = c!();
|
||||
|
||||
// parens are necessary if the expression result is called as a constructor
|
||||
const c = new (d()!)();
|
||||
const c = new (d()!);
|
||||
|
||||
=====================================output=====================================
|
||||
(a ? b : c)![tokenKey];
|
||||
|
@ -73,8 +76,11 @@ function* g() {
|
|||
return (yield* foo())!;
|
||||
}
|
||||
|
||||
const a = (b()!)();
|
||||
const a = b()!(); // parens aren't necessary
|
||||
const b = c!();
|
||||
|
||||
// parens are necessary if the expression result is called as a constructor
|
||||
const c = new (d()!)();
|
||||
const c = new (d()!)();
|
||||
|
||||
================================================================================
|
||||
|
|
|
@ -10,6 +10,9 @@ function* g() {
|
|||
return (yield * foo())!;
|
||||
}
|
||||
|
||||
const a = (b()!)();
|
||||
const a = (b()!)(); // parens aren't necessary
|
||||
const b = c!();
|
||||
|
||||
// parens are necessary if the expression result is called as a constructor
|
||||
const c = new (d()!)();
|
||||
const c = new (d()!);
|
||||
|
|
Loading…
Reference in New Issue