New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TypeScript: Support a TS 3.9 breaking change for Optional Chaining and Non-Null Assertions #8450
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#### Support TypeScript 3.9 breaking change for Optional Chaining and Non-Null Assertions ([#8450](https://github.com/prettier/prettier/pull/8450) by [@sosukesuzuki](https://github.com/sosukesuzuki)) | ||
|
||
See https://devblogs.microsoft.com/typescript/announcing-typescript-3-9/#breaking-changes | ||
|
||
<!-- prettier-ignore --> | ||
```ts | ||
// Input | ||
(a?.b)!.c; | ||
|
||
// Prettier stable | ||
a?.b!.c; | ||
|
||
// Prettier master | ||
(a?.b)!.c; | ||
|
||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -626,6 +626,14 @@ function needsParens(path, options) { | |
|
||
case "OptionalMemberExpression": | ||
case "OptionalCallExpression": | ||
if ( | ||
node.type === "OptionalMemberExpression" && | ||
parent.type === "TSNonNullExpression" && | ||
path.getParentNode(1).type === "MemberExpression" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Member expressions have two child nodes, so it's not enough to check only There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This? (a?.b)![a?.b!] There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep |
||
) { | ||
return true; | ||
} | ||
|
||
if ( | ||
(parent.type === "MemberExpression" && name === "object") || | ||
thorn0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
((parent.type === "CallExpression" || | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
a?.b!.c; | ||
a?.b!.c.d; | ||
a?.b.c!.d; | ||
a!.b?.c; | ||
a?.b!?.c; | ||
a?.b!.c?.c; | ||
(a?.b!).c; | ||
(a?.b)!.c; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OptionalCallExpression
should be handled too.