Repository: prettier/prettier (JavaScript)

Issue:
Last indented list item inline comment is being moved to outer comment and combined with existing comment
**Prettier 2.5.0**
[Playground link](https://prettier.io/playground/?source=post_page---------------------------#N4Igxg9gdgLgprEAuEB6VACAKgTwA5wDKYATgJZ4wZmxwkBmAhmHADpQ3wPNzYAWNAOYBGDMHasYjaUgwAKCTEkAfDAHIYAqCLUZ0GAMIQAtsYQxhiles1CATLv1HT5u4oCUAbQC6Abj2YAGI0jAA2GJAusOwAviAANCAQlGTQAM7IoIwkJBAA7gAK2QgZKGF5jDgZiQBGJMwA1nAwhIxmADI0cMhMoWlwtfVgTS14zELIMCQArgMgcMY1cAAmyyvtjNrTjIJwgRAkxowwMBNl0zAQCSB8MMahAOoC8GljLIQlZKcAbl84yOA0tUQDR+iQYAV6oIjj0wv1EgArNIAD0IQlCcAAitMIPBYX05mMSGCATVGEtQtc8ORYA8yMtNMgABwABkS1Ig-Qe9TwAOpcDB326iQAjjj4JDkqUQIw0gBaKBwFYra4kOBishqyE7GFIXrwkD9YxkSYzOZpdFY8XdPVwuZSGp0hl8ZB2RJTRhkUJCZy6+ZpACs12m-Sw5NK+rm31mAEkoGtYMRyJQAILxwgwHAY-H9GIxIA)
<!-- prettier-ignore -->
```sh
--parser babel
```

**Input:**
<!-- prettier-ignore -->
```ts
// TypeScript interface
interface Thing1 {
	aaa: (
		| 'thing1' // Comment1
		| 'thing2' // Comment2
	)[]; // Final comment
}
```

**Output:**
<!-- prettier-ignore -->
```ts
// TypeScript interface
interface Thing1 {
  aaa: (
    | "thing1" // Comment1
    | "thing2"
  )[]; // Comment2 // Final comment
}

```

**Expected behavior:**
```ts
// TypeScript interface
interface Thing1 {
  aaa: (
    | "thing1" // Comment1
    | "thing2" // Comment2
  )[]; // Final comment
}
```

The placement of `Comment2` in the sample output is the point of concern.

Curiously, using 'flow', 'babel', 'babel-flow' and 'typescript' parsers, the same incorrect output is obtained.

But: when using `babel-ts` as the parser, it seems to generate the expected output.

So, I'm thinking this is a bug in Prettier rather than in the parsers?



Hints:
This is still happening in the latest version of Prettier, has anyone started looking into this?
Another half year later, and it appears that this issue is *still* happening!!!

Patch:
diff --git a/src/language-js/comments/handle-comments.js b/src/language-js/comments/handle-comments.js
--- a/src/language-js/comments/handle-comments.js
+++ b/src/language-js/comments/handle-comments.js
@@ -84,6 +84,7 @@ function handleEndOfLineComment(context) {
     handleVariableDeclaratorComments,
     handleBreakAndContinueStatementComments,
     handleSwitchDefaultCaseComments,
+    handleLastUnionElementInExpression,
   ].some((fn) => fn(context));
 }
 
@@ -887,6 +888,44 @@ function handleSwitchDefaultCaseComments({
   return true;
 }
 
+/**
+ * Handle `Comment2` and `Comment4`.
+ *
+ *   type Foo = (
+ *     | "thing1" // Comment1
+ *     | "thing2" // Comment2
+ *   )[];
+ *
+ *   type Foo = (
+ *     | "thing1" // Comment3
+ *     | "thing2" // Comment4
+ *   ) & Bar;
+ *
+ * @param {CommentContext} context
+ * @returns {boolean}
+ */
+function handleLastUnionElementInExpression({
+  comment,
+  precedingNode,
+  enclosingNode,
+  followingNode,
+}) {
+  if (
+    precedingNode &&
+    (precedingNode.type === "TSUnionType" ||
+      precedingNode.type === "UnionTypeAnnotation") &&
+    (((enclosingNode.type === "TSArrayType" ||
+      enclosingNode.type === "ArrayTypeAnnotation") &&
+      followingNode === undefined) ||
+      enclosingNode.type === "TSIntersectionType" ||
+      enclosingNode.type === "IntersectionTypeAnnotation")
+  ) {
+    addTrailingComment(precedingNode.types.at(-1), comment);
+    return true;
+  }
+  return false;
+}
+
 /**
  * @param {Node} node
  * @returns {boolean}


Test patch:
diff --git a/tests/format/flow/union/__snapshots__/jsfmt.spec.js.snap b/tests/format/flow/union/__snapshots__/jsfmt.spec.js.snap
--- a/tests/format/flow/union/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/format/flow/union/__snapshots__/jsfmt.spec.js.snap
@@ -22,6 +22,37 @@ const myValue = (callcallcallcallcallcall(
 ================================================================================
 `;
 
+exports[`comments.js format 1`] = `
+====================================options=====================================
+parsers: ["flow"]
+printWidth: 80
+                                                                                | printWidth
+=====================================input======================================
+type Foo = (
+  | "thing1" // Comment1
+  | "thing2" // Comment2
+)[]; // Final comment1
+
+type Foo = (
+  | "thing1" // Comment1
+  | "thing2" // Comment2
+) & Bar; // Final comment2
+
+=====================================output=====================================
+type Foo = (
+  | "thing1" // Comment1
+  | "thing2" // Comment2
+)[]; // Final comment1
+
+type Foo = (
+  | "thing1" // Comment1
+  | "thing2" // Comment2
+) &
+  Bar; // Final comment2
+
+================================================================================
+`;
+
 exports[`union.js format 1`] = `
 ====================================options=====================================
 parsers: ["flow"]
diff --git a/tests/format/flow/union/comments.js b/tests/format/flow/union/comments.js
new file mode 100644
--- /dev/null
+++ b/tests/format/flow/union/comments.js
@@ -0,0 +1,9 @@
+type Foo = (
+  | "thing1" // Comment1
+  | "thing2" // Comment2
+)[]; // Final comment1
+
+type Foo = (
+  | "thing1" // Comment1
+  | "thing2" // Comment2
+) & Bar; // Final comment2
diff --git a/tests/format/typescript/union/__snapshots__/jsfmt.spec.js.snap b/tests/format/typescript/union/__snapshots__/jsfmt.spec.js.snap
--- a/tests/format/typescript/union/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/format/typescript/union/__snapshots__/jsfmt.spec.js.snap
@@ -1,5 +1,36 @@
 // Jest Snapshot v1, https://goo.gl/fbAQLP
 
+exports[`comments.ts format 1`] = `
+====================================options=====================================
+parsers: ["typescript"]
+printWidth: 80
+                                                                                | printWidth
+=====================================input======================================
+type Foo = (
+  | "thing1" // Comment1
+  | "thing2" // Comment2
+)[]; // Final comment1
+
+type Foo = (
+  | "thing1" // Comment1
+  | "thing2" // Comment2
+) & Bar; // Final comment2
+
+=====================================output=====================================
+type Foo = (
+  | "thing1" // Comment1
+  | "thing2" // Comment2
+)[]; // Final comment1
+
+type Foo = (
+  | "thing1" // Comment1
+  | "thing2" // Comment2
+) &
+  Bar; // Final comment2
+
+================================================================================
+`;
+
 exports[`inlining.ts format 1`] = `
 ====================================options=====================================
 parsers: ["typescript"]
diff --git a/tests/format/typescript/union/comments.ts b/tests/format/typescript/union/comments.ts
new file mode 100644
--- /dev/null
+++ b/tests/format/typescript/union/comments.ts
@@ -0,0 +1,9 @@
+type Foo = (
+  | "thing1" // Comment1
+  | "thing2" // Comment2
+)[]; // Final comment1
+
+type Foo = (
+  | "thing1" // Comment1
+  | "thing2" // Comment2
+) & Bar; // Final comment2