Repository: prettier/prettier (JavaScript)
Date: 2020-11-21 18:15:59+00:00
Task type: Bug Fix

Issue:
Inconsistency language comment detect
**Prettier 2.1.2**
[Playground link](https://prettier.io/playground/#N4Igxg9gdgLgprEAuEAzCEAEB6AVJgCQBUBZAGU120wF5MADAHgBEBJANQD5Hs2v7MIADQgIABxgBLaAGdkoAIYAnJRADuABWUI5KBQBs1CgJ5yRAIyUKwAazgwAymOuSoAc2QwlAVzgi4ALbmcAAmIaFkCu7eCm5wAGIQSgEKMFLuyCAK3jAQwiAAFjAB+gDqBZLwMs5gcA46lZIAbpXGmWAyZiCuMnBKMBpWbinIqAa9IgBWMgAeAEJWtvYOCgFwZK5wo+N+INMzDq5u+nAAit4Q8Nv6EyDOSr1Kmaj66vliSq4wpZIhMAXIAAcAAYRB8IL1SlYxJkPnBHk0tiIAI4XeCDcS6LIyAC0UDgoVC+SUcFRkhJg1iIyQYxuu16AUknh89KOJ3Oly2NJ2IhgCnMPz+AKQACZeVZJPojgBhCABakgeEAVny3l6RH5ulptyavlYUHCsAcYE+EgAggaHDBjCdrr0AL72oA)
```sh
--parser flow
```

**Input:**
```jsx
foo /* HTML */ = `<DIV></DIV>` 
```

**Output:**
```jsx
foo /* HTML */ = `<DIV></DIV>`;

```

**Prettier 2.1.2**
[Playground link](https://prettier.io/playground/#N4Igxg9gdgLgprEAuEAzCEAEB6AVJgCQBUBZAGU120wF5MADAHgBEBJANQD5Hs2v7MIADQgIABxgBLaAGdkoAIYAnJRADuABWUI5KBQBs1CgJ5yRAIyUKwAazgwAymOuSoAc2QwlAVzgi4ALbmcAAmIaFkCu7eCm5wAGIQSgEKMFLuyCAK3jAQwiAAFjAB+gDqBZLwMs5gcA46lZIAbpXGmWAyZiCuMnBKMBpWbinIqAa9IgBWMgAeAEJWtvYOCgFwZK5wo+N+INMzDq5u+nAAit4Q8Nv6EyDOSr1KmeYKwfr5YkquMKWSITAFZAADgADCJPhBeqUrGJMp84I8mlsRABHC7wQbiXRZGQAWigcFCoXySjgaMkpMGsRGSDGN12vQCkk8PgZRxO50uW1pOxEMFev3+gKQACY+VZJPojgBhCABGkgBEAVny3l6RFeujptyavlYUHCsAcYC+EgAggaHDBjCdrr0AL72oA)
```sh
--parser babel
```

**Input:**
```jsx
foo /* HTML */ = `<DIV></DIV>` 
```

**Output:**
```jsx
foo /* HTML */ = `<div></div>`;

```



Issue discussion:

Solution patch:
diff --git a/changelog_unreleased/javascript/9743.md b/changelog_unreleased/javascript/9743.md
new file mode 100644
index 000000000000..376d20e92616
--- /dev/null
+++ b/changelog_unreleased/javascript/9743.md
@@ -0,0 +1,19 @@
+#### Fix inconsistent language comment detection (#9743 by @fisker)
+
+<!-- prettier-ignore -->
+```jsx
+// Input
+foo /* HTML */ = `<DIV>
+</DIV>`;
+
+// Prettier stable (--parser=babel)
+foo /* HTML */ = `<div></div>`;
+
+// Prettier stable (--parser=meriyah)
+foo /* HTML */ = `<DIV>
+</DIV>`;
+
+// Prettier master (All JavaScript parsers)
+foo /* HTML */ = `<DIV>
+</DIV>`;
+```
diff --git a/src/language-js/comments.js b/src/language-js/comments.js
index af0866175f23..91cd34ffac77 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 7c7ee3eb9c7b..3058e7868dc8 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} `
   );
 }
 


Test patch:
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 000000000000..025e71c48160
--- /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 */ = \`<DIV>
+
+</DIV>\`;
+
+=====================================output=====================================
+const html /* HTML */ = \`<DIV>
+
+</DIV>\`;
+
+================================================================================
+`;
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 000000000000..eb85eda6bd02
--- /dev/null
+++ b/tests/js/multiparser-html/language-comment/jsfmt.spec.js
@@ -0,0 +1,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 000000000000..ee5517b7b78b
--- /dev/null
+++ b/tests/js/multiparser-html/language-comment/not-language-comment.js
@@ -0,0 +1,3 @@
+const html /* HTML */ = `<DIV>
+
+</DIV>`;