Skip to content
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

Merged
merged 3 commits into from May 31, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions changelog_unreleased/typescript/pr-8450.md
@@ -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;

```
8 changes: 8 additions & 0 deletions src/language-js/needs-parens.js
Expand Up @@ -626,6 +626,14 @@ function needsParens(path, options) {

case "OptionalMemberExpression":
case "OptionalCallExpression":
if (
node.type === "OptionalMemberExpression" &&
Copy link
Member

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.

parent.type === "TSNonNullExpression" &&
path.getParentNode(1).type === "MemberExpression"
Copy link
Member

Choose a reason for hiding this comment

The 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 type here.

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This?

(a?.b)![a?.b!]

Copy link
Member

Choose a reason for hiding this comment

The 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" ||
Expand Down
28 changes: 28 additions & 0 deletions tests/typescript/non-null/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -32,6 +32,34 @@ foo!.bar().baz().what();
================================================================================
`;

exports[`optional-chain.ts format 1`] = `
====================================options=====================================
parsers: ["typescript"]
printWidth: 80
| printWidth
=====================================input======================================
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;

=====================================output=====================================
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;

================================================================================
`;

exports[`parens.ts format 1`] = `
====================================options=====================================
parsers: ["typescript"]
Expand Down
8 changes: 8 additions & 0 deletions tests/typescript/non-null/optional-chain.ts
@@ -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;