TypeScript: Keep parentheses around a function called with non-null assertion. (#6136)

master
Sosuke Suzuki 2019-05-20 01:15:02 +09:00 committed by Lucas Duailibe
parent 4c64f13ef4
commit 3b3c411642
4 changed files with 41 additions and 0 deletions

View File

@ -276,6 +276,25 @@ type G = keyof T[];
type G = (keyof T)[];
```
### TypeScript: Keep parentheses around a function called with non-null assertion. ([6136] by [@sosukesuzuki])
Previously, Prettier removes necessary parentheses around a call expression with non-null assertion. It happens when it's return value is called as function.
<!-- 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()!)();
const b = new (c()!)();
```
[#5979]: https://github.com/prettier/prettier/pull/5979
[#6086]: https://github.com/prettier/prettier/pull/6086
[#6088]: https://github.com/prettier/prettier/pull/6088
@ -288,6 +307,7 @@ type G = (keyof T)[];
[#6129]: https://github.com/prettier/prettier/pull/6129
[#6130]: https://github.com/prettier/prettier/pull/6130
[#6131]: https://github.com/prettier/prettier/pull/6131
[#6136]: https://github.com/prettier/prettier/pull/6136
[@belochub]: https://github.com/belochub
[@brainkim]: https://github.com/brainkim
[@duailibe]: https://github.com/duailibe

View File

@ -717,6 +717,15 @@ 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 false;

View File

@ -56,6 +56,10 @@ function* g() {
return (yield * foo())!;
}
const a = (b()!)();
const b = c!();
const c = new (d()!)();
=====================================output=====================================
(a ? b : c)![tokenKey];
(a || b)![tokenKey];
@ -69,5 +73,9 @@ function* g() {
return (yield* foo())!;
}
const a = (b()!)();
const b = c!();
const c = new (d()!)();
================================================================================
`;

View File

@ -9,3 +9,7 @@ async function f() {
function* g() {
return (yield * foo())!;
}
const a = (b()!)();
const b = c!();
const c = new (d()!)();