Repository: prettier/prettier (JavaScript)

Issue:
Inconsistent between comments inside export and import specifiers 
**Prettier 2.6.2**
[Playground link](https://prettier.io/playground/#N4Igxg9gdgLgprEAuEcAeAHCAnGACYAHSjzwDMIIAaY02vAIwENs8mBnUgei70gFt+CGMUIj8zAF40oAX3LYI-PAHIKEFaKjosuAvXUy6JRizac8PPkqGxR409OKytAS3678RE4fr1mrBzcvAK2IlBiYo4y8mSKymqUKiBUIBAYMK7Q7MigLIoA7gAKLAg5KEwANgVMAJ45qQzYTGAA1nAwAMpMQgAyrtrIZFXscI3NbR2dGC0DAObIMNgArmOo-AxwACZb271MUHPLTHNwAGI4-EwwmYfIIEzLMBApIAAWMPyVAOpvrvDsGZgOCdMr-VwAN3+tXuYHYDRAA1GuCKzTmVyGIzWACt2GhOvNKnAAIrLCDwTGVUapGbYZH3ZibSqvDDYAYwb6uLYwN7IAAcAAYaYpRt9mhh7qy4MiIXBXgBHMnwVHpcoPdgAWm0222r2wcEVrn1qJOGKQwypa1G-FcixWVsJJKVcvNWNSMCYDE53N5SAATO7mq5KvMAMI2Jj3aUAVley1GABVPeULdSQBDVgBJKC7WCdMBsjIAQRznRgtSJlNGslkQA)
<!-- prettier-ignore -->
```sh
--parser babel
```

**Input:**
<!-- prettier-ignore -->
```jsx
export {
  foo,
  
  bar as  // comment
		 baz,
} from 'foo'

export {
  foo,
  
  bar as  // comment
		 baz,
}

import {
  foo,
  
  bar as  // comment
		 baz,
} from 'foo'
```

**Output:**
<!-- prettier-ignore -->
```jsx
export {
  foo,
  bar as baz, // comment
} from "foo";

export {
  foo,
  bar as baz, // comment
};

import {
  foo,
  // comment
  bar as baz,
} from "foo";

```

**Expected behavior:**

This is edge case, but worth to investigate where the difference is.


[#12746](https://deploy-preview-12746--prettier.netlify.app/playground/#N4Igxg9gdgLgprEAuEcAeAHCAnGACYAHSjzwDMIIAaY02vAIwENs8mBnUgei70gFt+CGMUIj8zAF40oAX3LYI-PAHIKEFaKjosuAvXUy6JRizac8PPkqGxR409OKytAS3678RE4fr1mrBzcvAK2IlBiYo4y8mSKymqUKiBUIBAYMK7Q7MigLIoA7gAKLAg5KEwANgVMAJ45qQzYTGAA1nAwAMpMQgAyrtrIZFXscI3NbR2dGC0DAObIMNgArmOo-AxwACZb271MUHPLTHNwAGI4-EwwmYfIIEzLMBApIAAWMPyVAOpvrvDsGZgOCdMr-VwAN3+tXuYHYDRAA1GuCKzTmVyGIzWACt2GhOvNKnAAIrLCDwTGVUapGbYZH3ZibSqvDDYAYwb6uLYwN7IAAcAAYaYpRt9mhh7qy4MiIXBXgBHMnwVHpcoPdgAWm0222r2wcEVrn1qJOGKQwypa1G-FcixWVsJJKVcvNWNSMCYDE53N5SAATO7mq5KvMAMI2Jj3aUAVley1GABVPeULdSQBDVgBJKC7WCdMBsjIAQRznRgtSJlNGslkQA) didn't change output.



Hints:

Patch:
diff --git a/src/language-js/comments.js b/src/language-js/comments.js
--- a/src/language-js/comments.js
+++ b/src/language-js/comments.js
@@ -61,7 +61,6 @@ function handleOwnLineComment(context) {
     handleWhileComments,
     handleTryStatementComments,
     handleClassComments,
-    handleImportSpecifierComments,
     handleForComments,
     handleUnionTypeComments,
     handleOnlyComments,
@@ -82,7 +81,7 @@ function handleEndOfLineComment(context) {
     handleClosureTypeCastComments,
     handleLastFunctionArgComments,
     handleConditionalExpressionComments,
-    handleImportSpecifierComments,
+    handleModuleSpecifiersComments,
     handleIfStatementComments,
     handleWhileComments,
     handleTryStatementComments,
@@ -623,14 +622,6 @@ function handleLastFunctionArgComments({
   return false;
 }
 
-function handleImportSpecifierComments({ comment, enclosingNode }) {
-  if (enclosingNode?.type === "ImportSpecifier") {
-    addLeadingComment(enclosingNode, comment);
-    return true;
-  }
-  return false;
-}
-
 function handleLabeledStatementComments({ comment, enclosingNode }) {
   if (enclosingNode?.type === "LabeledStatement") {
     addLeadingComment(enclosingNode, comment);
@@ -768,6 +759,14 @@ function handleModuleSpecifiersComments({
   enclosingNode,
   text,
 }) {
+  if (
+    enclosingNode?.type === "ImportSpecifier" ||
+    enclosingNode?.type === "ExportSpecifier"
+  ) {
+    addLeadingComment(enclosingNode, comment);
+    return true;
+  }
+
   const isImportDeclaration =
     precedingNode?.type === "ImportSpecifier" &&
     enclosingNode?.type === "ImportDeclaration";


Test patch:
diff --git a/tests/format/js/comments/__snapshots__/jsfmt.spec.js.snap b/tests/format/js/comments/__snapshots__/jsfmt.spec.js.snap
--- a/tests/format/js/comments/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/format/js/comments/__snapshots__/jsfmt.spec.js.snap
@@ -1474,6 +1474,24 @@ export {
   barr, // comment
 }
 
+const foooo = ''
+const barrr = ''
+export {
+  foooo,
+
+  barrr as  // comment
+		 baz,
+} from 'foo'
+
+const fooooo = ''
+const barrrr = ''
+export {
+  fooooo,
+
+  barrrr as  // comment
+		 bazz,
+}
+
 =====================================output=====================================
 export //comment
  {}
@@ -1498,6 +1516,22 @@ export {
   barr, // comment
 }
 
+const foooo = ""
+const barrr = ""
+export {
+  foooo,
+  // comment
+  barrr as baz,
+} from "foo"
+
+const fooooo = ""
+const barrrr = ""
+export {
+  fooooo,
+  // comment
+  barrrr as bazz,
+}
+
 ================================================================================
 `;
 
@@ -1530,6 +1564,24 @@ export {
   barr, // comment
 }
 
+const foooo = ''
+const barrr = ''
+export {
+  foooo,
+
+  barrr as  // comment
+		 baz,
+} from 'foo'
+
+const fooooo = ''
+const barrrr = ''
+export {
+  fooooo,
+
+  barrrr as  // comment
+		 bazz,
+}
+
 =====================================output=====================================
 export //comment
  {};
@@ -1554,6 +1606,141 @@ export {
   barr, // comment
 };
 
+const foooo = "";
+const barrr = "";
+export {
+  foooo,
+  // comment
+  barrr as baz,
+} from "foo";
+
+const fooooo = "";
+const barrrr = "";
+export {
+  fooooo,
+  // comment
+  barrrr as bazz,
+};
+
+================================================================================
+`;
+
+exports[`export-and-import.js - {"semi":false} format 1`] = `
+====================================options=====================================
+parsers: ["babel", "flow", "typescript"]
+printWidth: 80
+semi: false
+                                                                                | printWidth
+=====================================input======================================
+// These are tests to compare comment formats in \`export\` and \`import\`.
+
+export {
+  foo,
+
+  bar as  // comment
+		 baz,
+} from 'foo'
+
+const fooo = ""
+const barr = ""
+
+export {
+  fooo,
+
+  barr as  // comment
+		 bazz,
+}
+
+import {
+  foo,
+
+  bar as  // comment
+		 baz,
+} from 'foo'
+
+=====================================output=====================================
+// These are tests to compare comment formats in \`export\` and \`import\`.
+
+export {
+  foo,
+  // comment
+  bar as baz,
+} from "foo"
+
+const fooo = ""
+const barr = ""
+
+export {
+  fooo,
+  // comment
+  barr as bazz,
+}
+
+import {
+  foo,
+  // comment
+  bar as baz,
+} from "foo"
+
+================================================================================
+`;
+
+exports[`export-and-import.js format 1`] = `
+====================================options=====================================
+parsers: ["babel", "flow", "typescript"]
+printWidth: 80
+                                                                                | printWidth
+=====================================input======================================
+// These are tests to compare comment formats in \`export\` and \`import\`.
+
+export {
+  foo,
+
+  bar as  // comment
+		 baz,
+} from 'foo'
+
+const fooo = ""
+const barr = ""
+
+export {
+  fooo,
+
+  barr as  // comment
+		 bazz,
+}
+
+import {
+  foo,
+
+  bar as  // comment
+		 baz,
+} from 'foo'
+
+=====================================output=====================================
+// These are tests to compare comment formats in \`export\` and \`import\`.
+
+export {
+  foo,
+  // comment
+  bar as baz,
+} from "foo";
+
+const fooo = "";
+const barr = "";
+
+export {
+  fooo,
+  // comment
+  barr as bazz,
+};
+
+import {
+  foo,
+  // comment
+  bar as baz,
+} from "foo";
+
 ================================================================================
 `;
 
diff --git a/tests/format/js/comments/export-and-import.js b/tests/format/js/comments/export-and-import.js
new file mode 100644
--- /dev/null
+++ b/tests/format/js/comments/export-and-import.js
@@ -0,0 +1,25 @@
+// These are tests to compare comment formats in `export` and `import`.
+
+export {
+  foo,
+
+  bar as  // comment
+		 baz,
+} from 'foo'
+
+const fooo = ""
+const barr = ""
+
+export {
+  fooo,
+
+  barr as  // comment
+		 bazz,
+}
+
+import {
+  foo,
+
+  bar as  // comment
+		 baz,
+} from 'foo'
diff --git a/tests/format/js/comments/export.js b/tests/format/js/comments/export.js
--- a/tests/format/js/comments/export.js
+++ b/tests/format/js/comments/export.js
@@ -20,3 +20,21 @@ export {
   fooo, // comment
   barr, // comment
 }
+
+const foooo = ''
+const barrr = ''
+export {
+  foooo,
+
+  barrr as  // comment
+		 baz,
+} from 'foo'
+
+const fooooo = ''
+const barrrr = ''
+export {
+  fooooo,
+
+  barrrr as  // comment
+		 bazz,
+}