From 80260555c90f369fd4e9d502868c4b5233347cf9 Mon Sep 17 00:00:00 2001 From: Georgii Dolzhykov Date: Thu, 17 Oct 2019 14:23:14 +0300 Subject: [PATCH] ts: fix optional computed methods (#6673) --- CHANGELOG.unreleased.md | 21 +++++++++++++++++++ src/language-js/printer-estree.js | 16 +++++++------- .../__snapshots__/jsfmt.spec.js.snap | 8 +++++++ tests/typescript_class/optional.ts | 4 ++++ 4 files changed, 41 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.unreleased.md b/CHANGELOG.unreleased.md index c696d27a..2efe9ef3 100644 --- a/CHANGELOG.unreleased.md +++ b/CHANGELOG.unreleased.md @@ -958,6 +958,26 @@ class A { } ``` +#### TypeScript: Fix optional computed methods ([#6673] by [@thorn0]) + + +```ts +// Input +class A { + protected [s]?() {} +} + +// Output (Prettier stable) +class A { + protected [s?]() {} +} + +// Output (Prettier master) +class A { + protected [s]?() {} +} +``` + [#5910]: https://github.com/prettier/prettier/pull/5910 [#6033]: https://github.com/prettier/prettier/pull/6033 [#6186]: https://github.com/prettier/prettier/pull/6186 @@ -991,6 +1011,7 @@ class A { [#6605]: https://github.com/prettier/prettier/pull/6605 [#6640]: https://github.com/prettier/prettier/pull/6640 [#6646]: https://github.com/prettier/prettier/pull/6646 +[#6673]: https://github.com/prettier/prettier/pull/6673 [@brainkim]: https://github.com/brainkim [@duailibe]: https://github.com/duailibe [@gavinjoyce]: https://github.com/gavinjoyce diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index b5b6d189..d73d9429 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -3632,15 +3632,9 @@ function printPropertyKey(path, options, print) { function printMethod(path, options, print) { const node = path.getNode(); const kind = node.kind; + const value = node.value || node; const parts = []; - const value = - node.type === "ObjectMethod" || - node.type === "ClassMethod" || - node.type === "ClassPrivateMethod" - ? node - : node.value; - if (!kind || kind === "init" || kind === "method" || kind === "constructor") { if (value.async) { parts.push("async "); @@ -3656,6 +3650,7 @@ function printMethod(path, options, print) { parts.push( printPropertyKey(path, options, print), + node.optional || node.key.optional ? "?" : "", node === value ? printMethodInternal(path, options, print) : path.call(path => printMethodInternal(path, options, print), "value") @@ -4592,7 +4587,12 @@ function printClass(path, options, print) { function printOptionalToken(path) { const node = path.getValue(); - if (!node.optional) { + if ( + !node.optional || + // It's an optional computed method parsed by typescript-estree. + // "?" is printed in `printMethod`. + (node.type === "Identifier" && node === path.getParentNode().key) + ) { return ""; } if ( diff --git a/tests/typescript_class/__snapshots__/jsfmt.spec.js.snap b/tests/typescript_class/__snapshots__/jsfmt.spec.js.snap index a63f89c8..0edc7ee9 100644 --- a/tests/typescript_class/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript_class/__snapshots__/jsfmt.spec.js.snap @@ -205,12 +205,20 @@ class X { "a-prop"?: boolean; } +class A { + protected [s]?() {} +} + =====================================output===================================== class X { private foo? = undefined; "a-prop"?: boolean; } +class A { + protected [s]?() {} +} + ================================================================================ `; diff --git a/tests/typescript_class/optional.ts b/tests/typescript_class/optional.ts index 1e0752da..b540e547 100644 --- a/tests/typescript_class/optional.ts +++ b/tests/typescript_class/optional.ts @@ -2,3 +2,7 @@ class X { private foo? = undefined; "a-prop"?: boolean; } + +class A { + protected [s]?() {} +}