diff --git a/changelog_unreleased/javascript/19345.md b/changelog_unreleased/javascript/19345.md new file mode 100644 index 000000000..33a8316fe --- /dev/null +++ b/changelog_unreleased/javascript/19345.md @@ -0,0 +1,18 @@ +#### Fix unstable comment between a tagged template's tag and quasi (#19345 by @sarathfrancis90) + + +```jsx +// Input +foo /* comment */ +`x`; + +// Prettier stable +foo /* comment */`x`; + +// Prettier stable (second format) +foo /* comment */ `x`; + +// Prettier main +foo /* comment */ +`x`; +``` diff --git a/src/language-js/comments/handle-comments.js b/src/language-js/comments/handle-comments.js index 86009bf5b..25d2a4293 100644 --- a/src/language-js/comments/handle-comments.js +++ b/src/language-js/comments/handle-comments.js @@ -126,6 +126,7 @@ function handleEndOfLineComment(context) { handleParenthesizedExpressionTrailingComment, handlePropertySignatureComments, handleBinaryCastExpressionComment, + handleTaggedTemplateExpressionComments, ].some((fn) => fn(context)); } @@ -822,6 +823,21 @@ function handleAssignmentLikeComments(context) { return false; } +function handleTaggedTemplateExpressionComments({ + comment, + enclosingNode, + followingNode, +}) { + if ( + enclosingNode?.type === "TaggedTemplateExpression" && + followingNode === enclosingNode.quasi + ) { + addLeadingComment(followingNode, comment); + return true; + } + return false; +} + function handleTSFunctionTrailingComments({ comment, enclosingNode, diff --git a/tests/config/format-test/failed-format-tests.js b/tests/config/format-test/failed-format-tests.js index 625c6a413..df1e444fc 100644 --- a/tests/config/format-test/failed-format-tests.js +++ b/tests/config/format-test/failed-format-tests.js @@ -10,7 +10,6 @@ const unstableTests = new Map( ], ["js/ignore/semi/head-ignored.js", (options) => options.semi === false], "js/comments/return-statement.js", - "js/comments/tagged-template-literal.js", [ "js/multiparser-markdown/codeblock.js", (options) => options.proseWrap === "always", diff --git a/tests/format/js/comments/__snapshots__/format.test.js.snap b/tests/format/js/comments/__snapshots__/format.test.js.snap index 67004e2c4..451077c5e 100644 --- a/tests/format/js/comments/__snapshots__/format.test.js.snap +++ b/tests/format/js/comments/__snapshots__/format.test.js.snap @@ -6849,6 +6849,12 @@ foo /* comment 5 */ \` \`; +foo /* comment 6 */ +\`x\`; + +foo // comment 7 +\`x\`; + =====================================output===================================== foo\`\` // comment 1 @@ -6862,8 +6868,15 @@ foo // comment 3 foo /* comment 4 */ \` \` -foo /* comment 5 */\` +foo /* comment 5 */ \` +\` + +foo /* comment 6 */ +\`x\` + +foo // comment 7 +\`x\` ================================================================================ `; @@ -6889,6 +6902,12 @@ foo /* comment 5 */ \` \`; +foo /* comment 6 */ +\`x\`; + +foo // comment 7 +\`x\`; + =====================================output===================================== foo\`\`; // comment 1 @@ -6902,9 +6921,16 @@ foo // comment 3 foo /* comment 4 */ \` \`; -foo /* comment 5 */\` +foo /* comment 5 */ +\` \`; +foo /* comment 6 */ +\`x\`; + +foo // comment 7 +\`x\`; + ================================================================================ `; diff --git a/tests/format/js/comments/tagged-template-literal.js b/tests/format/js/comments/tagged-template-literal.js index 7afab7961..ebb43b860 100644 --- a/tests/format/js/comments/tagged-template-literal.js +++ b/tests/format/js/comments/tagged-template-literal.js @@ -13,3 +13,9 @@ foo /* comment 4 */` foo /* comment 5 */ ` `; + +foo /* comment 6 */ +`x`; + +foo // comment 7 +`x`; diff --git a/tests/format/js/multiparser-comments/__snapshots__/format.test.js.snap b/tests/format/js/multiparser-comments/__snapshots__/format.test.js.snap index 9eeb77132..efbfd4a6a 100644 --- a/tests/format/js/multiparser-comments/__snapshots__/format.test.js.snap +++ b/tests/format/js/multiparser-comments/__snapshots__/format.test.js.snap @@ -173,15 +173,13 @@ foo(html // oops \`
bar
foobar
- foo -bar
+ foo +