From 90eed411d4c02c0084be6e2c618057a8c934723e Mon Sep 17 00:00:00 2001 From: Evilebot Tnawi Date: Wed, 20 Nov 2019 20:59:25 +0300 Subject: [PATCH] =?UTF-8?q?preserve=20parentheses=20for=20JSDoc=20type=20c?= =?UTF-8?q?asting=20with=20extra=20spaces=20a=E2=80=A6=20(#7011)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- changelog_unreleased/javascript/pr-7011.md | 18 ++++++++++++ src/language-js/needs-parens.js | 2 +- .../__snapshots__/jsfmt.spec.js.snap | 28 +++++++++++++++++++ .../extra-spaces-and-asterisks.js | 8 ++++++ 4 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 changelog_unreleased/javascript/pr-7011.md create mode 100644 tests/comments_closure_typecast/extra-spaces-and-asterisks.js diff --git a/changelog_unreleased/javascript/pr-7011.md b/changelog_unreleased/javascript/pr-7011.md new file mode 100644 index 000000000..f86a2699d --- /dev/null +++ b/changelog_unreleased/javascript/pr-7011.md @@ -0,0 +1,18 @@ +#### Preserve parentheses for JSDoc type casting with extra spaces and asterisk at end ([#7011](https://github.com/prettier/prettier/pull/7011) by [@evilebottnawi](https://github.com/evilebottnawi)) + +Formatting of comments in labeled statements was not stable + + +```javascript +// Input +const foo = /** @type {!Foo} **/ (baz); +const bar = /** @type {!Foo} * * */(baz); + +// Prettier stable +const foo = /** @type {!Foo} **/ baz; +const bar = /** @type {!Foo} * * */ baz; + +// Prettier master +const foo = /** @type {!Foo} **/ (baz); +const bar = /** @type {!Foo} * * */ (baz); +``` diff --git a/src/language-js/needs-parens.js b/src/language-js/needs-parens.js index e45ff2972..e6b5aa0e2 100644 --- a/src/language-js/needs-parens.js +++ b/src/language-js/needs-parens.js @@ -53,7 +53,7 @@ function hasClosureCompilerTypeCastComment(text, path) { const cleaned = comment .trim() .split("\n") - .map(line => line.replace(/^[\s*]+/, "")) + .map(line => line.replace(/^[\s*]+/, "").replace(/[\s*]+$/, "")) .join(" ") .trim(); if (!/^@type\s*\{[^]+\}$/.test(cleaned)) { diff --git a/tests/comments_closure_typecast/__snapshots__/jsfmt.spec.js.snap b/tests/comments_closure_typecast/__snapshots__/jsfmt.spec.js.snap index 109e446fe..e0650b287 100644 --- a/tests/comments_closure_typecast/__snapshots__/jsfmt.spec.js.snap +++ b/tests/comments_closure_typecast/__snapshots__/jsfmt.spec.js.snap @@ -156,3 +156,31 @@ const v8 = /**@type{string} */ (value); ================================================================================ `; + +exports[`extra-spaces-and-asterisks.js 1`] = ` +====================================options===================================== +parsers: ["babel"] +printWidth: 80 + | printWidth +=====================================input====================================== +const foo1 = /** @type {!Foo} */(bar); +const foo2 = /** @type {!Foo} **/(bar); +const foo3 = /** @type {!Foo} * */(bar); +const foo4 = /** @type {!Foo} ***/(bar); +const foo5 = /** @type {!Foo} * * */(bar); +const foo6 = /** @type {!Foo} *****/(bar); +const foo7 = /** @type {!Foo} * * * * */(bar); +const foo8 = /** @type {!Foo} ** * * */(bar); + +=====================================output===================================== +const foo1 = /** @type {!Foo} */ (bar); +const foo2 = /** @type {!Foo} **/ (bar); +const foo3 = /** @type {!Foo} * */ (bar); +const foo4 = /** @type {!Foo} ***/ (bar); +const foo5 = /** @type {!Foo} * * */ (bar); +const foo6 = /** @type {!Foo} *****/ (bar); +const foo7 = /** @type {!Foo} * * * * */ (bar); +const foo8 = /** @type {!Foo} ** * * */ (bar); + +================================================================================ +`; diff --git a/tests/comments_closure_typecast/extra-spaces-and-asterisks.js b/tests/comments_closure_typecast/extra-spaces-and-asterisks.js new file mode 100644 index 000000000..45572417a --- /dev/null +++ b/tests/comments_closure_typecast/extra-spaces-and-asterisks.js @@ -0,0 +1,8 @@ +const foo1 = /** @type {!Foo} */(bar); +const foo2 = /** @type {!Foo} **/(bar); +const foo3 = /** @type {!Foo} * */(bar); +const foo4 = /** @type {!Foo} ***/(bar); +const foo5 = /** @type {!Foo} * * */(bar); +const foo6 = /** @type {!Foo} *****/(bar); +const foo7 = /** @type {!Foo} * * * * */(bar); +const foo8 = /** @type {!Foo} ** * * */(bar);