From 69ea417f6bb34e19f46dee5456c3d40b2d973f28 Mon Sep 17 00:00:00 2001 From: Lucas Duailibe Date: Sat, 5 Feb 2022 11:56:37 -0300 Subject: [PATCH] Prevent multiple parenthesis around superclass with typecast (#12226) --- changelog_unreleased/javascript/12222.md | 10 ++++++---- src/language-js/needs-parens.js | 1 - .../__snapshots__/jsfmt.spec.js.snap | 14 ++++++++++++++ .../js/comments-closure-typecast/superclass.js | 1 + 4 files changed, 21 insertions(+), 5 deletions(-) create mode 100644 tests/format/js/comments-closure-typecast/superclass.js diff --git a/changelog_unreleased/javascript/12222.md b/changelog_unreleased/javascript/12222.md index 1cf4d91ba..215b77dde 100644 --- a/changelog_unreleased/javascript/12222.md +++ b/changelog_unreleased/javascript/12222.md @@ -1,13 +1,15 @@ -#### Fix moving comments after `extends` in class declarations (#12222 by @duailibe) +#### Fix typecast of superclass in class declarations (#12222 & #12226 by @duailibe) + +This was a combination of two separate bugs: moving the comments before the superclass and adding multiple parenthesis around the superclass. ```js // Input -class Foo extends /* comment explaining Bar */ Bar {} +class Foo extends /** @type {Type} */ (Bar) {} // Prettier stable -class Foo /* comment explaining Bar */ extends Bar {} +class Foo /** @type {Type} */ extends ((Bar)) {} // Prettier main -class Foo extends /* comment explaining Bar */ Bar {} +class Foo extends /** @type {Type} */ (Bar) {} ``` diff --git a/src/language-js/needs-parens.js b/src/language-js/needs-parens.js index 6ede2f45d..970ebd713 100644 --- a/src/language-js/needs-parens.js +++ b/src/language-js/needs-parens.js @@ -95,7 +95,6 @@ function needsParens(path, options) { node.type === "LogicalExpression" || node.type === "NewExpression" || node.type === "ObjectExpression" || - node.type === "ParenthesizedExpression" || node.type === "SequenceExpression" || node.type === "TaggedTemplateExpression" || node.type === "UnaryExpression" || diff --git a/tests/format/js/comments-closure-typecast/__snapshots__/jsfmt.spec.js.snap b/tests/format/js/comments-closure-typecast/__snapshots__/jsfmt.spec.js.snap index 9aa8734ed..722445bec 100644 --- a/tests/format/js/comments-closure-typecast/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/js/comments-closure-typecast/__snapshots__/jsfmt.spec.js.snap @@ -603,6 +603,20 @@ const OverlapWrapper = ================================================================================ `; +exports[`superclass.js format 1`] = ` +====================================options===================================== +parsers: ["babel"] +printWidth: 80 + | printWidth +=====================================input====================================== +class Foo extends /** @type {string} */ (Bar) {} + +=====================================output===================================== +class Foo extends /** @type {string} */ (Bar) {} + +================================================================================ +`; + exports[`ways-to-specify-type.js format 1`] = ` ====================================options===================================== parsers: ["babel"] diff --git a/tests/format/js/comments-closure-typecast/superclass.js b/tests/format/js/comments-closure-typecast/superclass.js new file mode 100644 index 000000000..0f5ad0587 --- /dev/null +++ b/tests/format/js/comments-closure-typecast/superclass.js @@ -0,0 +1 @@ +class Foo extends /** @type {string} */ (Bar) {}