From 664d0d33b519b093f2be3a35357debeecdf941e2 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Thu, 26 Nov 2020 19:30:57 +0800 Subject: [PATCH] Fix inconsistent language comment detection (#9743) --- changelog_unreleased/javascript/9743.md | 19 +++++++++++++++++++ src/language-js/comments.js | 15 --------------- src/language-js/embed.js | 9 ++++----- .../__snapshots__/jsfmt.spec.js.snap | 19 +++++++++++++++++++ .../language-comment/jsfmt.spec.js | 1 + .../language-comment/not-language-comment.js | 3 +++ 6 files changed, 46 insertions(+), 20 deletions(-) create mode 100644 changelog_unreleased/javascript/9743.md create mode 100644 tests/js/multiparser-html/language-comment/__snapshots__/jsfmt.spec.js.snap create mode 100644 tests/js/multiparser-html/language-comment/jsfmt.spec.js create mode 100644 tests/js/multiparser-html/language-comment/not-language-comment.js diff --git a/changelog_unreleased/javascript/9743.md b/changelog_unreleased/javascript/9743.md new file mode 100644 index 000000000..376d20e92 --- /dev/null +++ b/changelog_unreleased/javascript/9743.md @@ -0,0 +1,19 @@ +#### Fix inconsistent language comment detection (#9743 by @fisker) + + +```jsx +// Input +foo /* HTML */ = `
+
`; + +// Prettier stable (--parser=babel) +foo /* HTML */ = `
`; + +// Prettier stable (--parser=meriyah) +foo /* HTML */ = `
+
`; + +// Prettier master (All JavaScript parsers) +foo /* HTML */ = `
+
`; +``` diff --git a/src/language-js/comments.js b/src/language-js/comments.js index af0866175..91cd34ffa 100644 --- a/src/language-js/comments.js +++ b/src/language-js/comments.js @@ -19,8 +19,6 @@ const { hasFlowShorthandAnnotationComment, hasFlowAnnotationComment, hasIgnoreComment, - hasComment, - CommentCheckFlags, } = require("./utils"); const { locStart, locEnd } = require("./loc"); @@ -866,18 +864,6 @@ function handleTSMappedTypeComments({ return false; } -/** - * @param {Node} node - * @param {(comment: Comment) => boolean} fn - * @returns boolean - */ -function hasLeadingComment(node, fn = () => true) { - if (node.leadingComments) { - return node.leadingComments.some(fn); - } - return hasComment(node, CommentCheckFlags.Leading, fn); -} - /** * @param {Node} node * @returns {boolean} @@ -994,7 +980,6 @@ module.exports = { handleOwnLineComment, handleEndOfLineComment, handleRemainingComment, - hasLeadingComment, isTypeCastComment, getGapRegex, getCommentChildNodes, diff --git a/src/language-js/embed.js b/src/language-js/embed.js index 7c7ee3eb9..3058e7868 100644 --- a/src/language-js/embed.js +++ b/src/language-js/embed.js @@ -1,7 +1,6 @@ "use strict"; -const { isBlockComment } = require("./utils"); -const { hasLeadingComment } = require("./comments"); +const { hasComment, CommentCheckFlags } = require("./utils"); const formatMarkdown = require("./embed/markdown"); const formatCss = require("./embed/css"); const formatGraphql = require("./embed/graphql"); @@ -267,10 +266,10 @@ function hasLanguageComment(node, languageName) { // we will not trim the comment value and we will expect exactly one space on // either side of the GraphQL string // Also see ./clean.js - return hasLeadingComment( + return hasComment( node, - (comment) => - isBlockComment(comment) && comment.value === ` ${languageName} ` + CommentCheckFlags.Block | CommentCheckFlags.Leading, + ({ value }) => value === ` ${languageName} ` ); } diff --git a/tests/js/multiparser-html/language-comment/__snapshots__/jsfmt.spec.js.snap b/tests/js/multiparser-html/language-comment/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000..025e71c48 --- /dev/null +++ b/tests/js/multiparser-html/language-comment/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,19 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`not-language-comment.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +const html /* HTML */ = \`
+ +
\`; + +=====================================output===================================== +const html /* HTML */ = \`
+ +
\`; + +================================================================================ +`; diff --git a/tests/js/multiparser-html/language-comment/jsfmt.spec.js b/tests/js/multiparser-html/language-comment/jsfmt.spec.js new file mode 100644 index 000000000..eb85eda6b --- /dev/null +++ b/tests/js/multiparser-html/language-comment/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname, ["babel", "flow", "typescript"]); diff --git a/tests/js/multiparser-html/language-comment/not-language-comment.js b/tests/js/multiparser-html/language-comment/not-language-comment.js new file mode 100644 index 000000000..ee5517b7b --- /dev/null +++ b/tests/js/multiparser-html/language-comment/not-language-comment.js @@ -0,0 +1,3 @@ +const html /* HTML */ = `
+ +
`;