Skip to content

Commit

Permalink
TypeScript: Support a TS 3.9 breaking change for Optional Chaining an…
Browse files Browse the repository at this point in the history
…d Non-Null Assertions (#8450)

* Suuport improvement optional chaining ergonomics

* Add changelog

* Fix reviewed points
  • Loading branch information
sosukesuzuki committed May 31, 2020
1 parent ffceb60 commit 3fc7579
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 2 deletions.
16 changes: 16 additions & 0 deletions changelog_unreleased/typescript/pr-8450.md
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;

```
9 changes: 7 additions & 2 deletions src/language-js/needs-parens.js
Original file line number Diff line number Diff line change
Expand Up @@ -625,15 +625,20 @@ function needsParens(path, options) {
}

case "OptionalMemberExpression":
case "OptionalCallExpression":
case "OptionalCallExpression": {
const parentParent = path.getParentNode(1);
if (
(parent.type === "MemberExpression" && name === "object") ||
((parent.type === "CallExpression" ||
parent.type === "NewExpression") &&
name === "callee")
name === "callee") ||
(parent.type === "TSNonNullExpression" &&
parentParent.type === "MemberExpression" &&
parentParent.object === parent)
) {
return true;
}
}
// fallthrough
case "CallExpression":
case "MemberExpression":
Expand Down
48 changes: 48 additions & 0 deletions tests/typescript/non-null/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,54 @@ 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;
a?.().b!.c;
a?.().b!.c.d;
a?.().b.c!.d;
a?.().b!?.c;
a?.().b!.c?.c;
(a?.().b!).c;
(a?.().b)!.c;
(a?.b)![c?.d!]
=====================================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;
a?.().b!.c;
a?.().b!.c.d;
a?.().b.c!.d;
a?.().b!?.c;
a?.().b!.c?.c;
(a?.().b)!.c;
(a?.().b)!.c;
(a?.b)![c?.d!];
================================================================================
`;

exports[`parens.ts format 1`] = `
====================================options=====================================
parsers: ["typescript"]
Expand Down
18 changes: 18 additions & 0 deletions tests/typescript/non-null/optional-chain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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;

a?.().b!.c;
a?.().b!.c.d;
a?.().b.c!.d;
a?.().b!?.c;
a?.().b!.c?.c;
(a?.().b!).c;
(a?.().b)!.c;

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

0 comments on commit 3fc7579

Please sign in to comment.