From 74f4d2b3c089b66d95e900fe802bd1b18b4b76ce Mon Sep 17 00:00:00 2001 From: Sosuke Suzuki Date: Wed, 10 Jul 2019 22:56:53 +0900 Subject: [PATCH] JavaScript: Keep line breaks when tagged template literal has a single line comment. (#6236) --- CHANGELOG.unreleased.md | 22 ++++++++++ src/language-js/printer-estree.js | 3 +- .../comments/__snapshots__/jsfmt.spec.js.snap | 41 +++++++++++++++++++ tests/comments/tagged-template-literal.js | 15 +++++++ 4 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 tests/comments/tagged-template-literal.js diff --git a/CHANGELOG.unreleased.md b/CHANGELOG.unreleased.md index dcd6db277..7de8ebeff 100644 --- a/CHANGELOG.unreleased.md +++ b/CHANGELOG.unreleased.md @@ -195,11 +195,33 @@ Previously, Prettier would incorrectly decode HTML entiites.

``` +#### JavaScript: Stop moving comments inside tagged template literals ([#6236] by [@sosukesuzuki]) + +Previously, Prettier would move comments after the tag inside the template literal. This version fixes this problem. + + +```js +// Input +foo //comment +` +`; + +// Output (Prettier stable) +foo` // comment +`; + +// Output (Prettier master) +foo // comment +` +`; +``` + [#6186]: https://github.com/prettier/prettier/pull/6186 [#6206]: https://github.com/prettier/prettier/pull/6206 [#6209]: https://github.com/prettier/prettier/pull/6209 [#6217]: https://github.com/prettier/prettier/pull/6217 [#6234]: https://github.com/prettier/prettier/pull/6234 +[#6236]: https://github.com/prettier/prettier/pull/6236 [@duailibe]: https://github.com/duailibe [@gavinjoyce]: https://github.com/gavinjoyce [@sosukesuzuki]: https://github.com/sosukesuzuki diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index a78c0c79a..5b778b270 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -2406,7 +2406,7 @@ function printPathNoParens(path, options, print, args) { ); } - parts.push("`"); + parts.push(lineSuffixBoundary, "`"); path.each(childPath => { const i = childPath.getName(); @@ -3914,6 +3914,7 @@ function printJestEachTemplateLiteral(node, expressions, options) { }); parts.push( + lineSuffixBoundary, "`", indent( concat([ diff --git a/tests/comments/__snapshots__/jsfmt.spec.js.snap b/tests/comments/__snapshots__/jsfmt.spec.js.snap index 8e07e83ed..eb9df60e2 100644 --- a/tests/comments/__snapshots__/jsfmt.spec.js.snap +++ b/tests/comments/__snapshots__/jsfmt.spec.js.snap @@ -2012,6 +2012,47 @@ switch (foo) { ================================================================================ `; +exports[`tagged-template-literal.js 1`] = ` +====================================options===================================== +parsers: ["flow", "babel"] +printWidth: 80 + | printWidth +=====================================input====================================== +foo\`\`; // comment + +foo // comment +\`\`; + +foo // comment +\` +\`; + +foo /* comment */\` +\`; + +foo /* comment */ +\` +\`; + +=====================================output===================================== +foo\`\`; // comment + +foo // comment +\`\`; + +foo // comment +\` +\`; + +foo/* comment */ \` +\`; + +foo /* comment */\` +\`; + +================================================================================ +`; + exports[`template-literal.js 1`] = ` ====================================options===================================== parsers: ["flow", "babel"] diff --git a/tests/comments/tagged-template-literal.js b/tests/comments/tagged-template-literal.js new file mode 100644 index 000000000..11e997445 --- /dev/null +++ b/tests/comments/tagged-template-literal.js @@ -0,0 +1,15 @@ +foo``; // comment + +foo // comment +``; + +foo // comment +` +`; + +foo /* comment */` +`; + +foo /* comment */ +` +`;