Prettier 3.8.1
Playground link
Input:
type UnionInIntersection = (
| "thing1" // Comment1
| "thing2" // Comment2
) & Bar; // Final comment2
type NestedUnion = (
| "thing1" // Comment1
| "thing2" // Comment2
) | Bar; // Final comment2
type UnionInIntersection2 =
(
A | B // comment 1
) & (
// comment2
A | B
)
type NestedUnion2 =
(
A | B // comment 1
) | (
// comment2
A | B
)
Output:
type UnionInIntersection = (
| "thing1" // Comment1
| "thing2" // Comment2
) &
Bar; // Final comment2
type NestedUnion =
| (
| "thing1" // Comment1
| "thing2"
) // Comment2
| Bar; // Final comment2
type UnionInIntersection2 = (
| A
| B // comment 1
) &
// comment2
(A | B);
type NestedUnion2 =
| (A | B) // comment 1
// comment2
| (A | B);
Expected output:
type UnionInIntersection = (
| "thing1" // Comment1
| "thing2" // Comment2
) &
Bar; // Final comment2
type NestedUnion = (
| "thing1" // Comment1
| "thing2" // Comment2
) |
Bar; // Final comment2
type UnionInIntersection2 = (
| A
| B // comment 1
) &
// comment2
(A | B);
type NestedUnion2 = (
| A
| B // comment 1
) |
// comment2
(A | B);
Why?
The current output shows inconsistent comment handling between union-in-intersection and union-in-union cases:
-
UnionInIntersection - Comments stay on their original lines:
type UnionInIntersection = (
| "thing1" // Comment1
| "thing2" // Comment2
) & Bar;
-
NestedUnion - Comments get repositioned, with // Comment2 moved outside the parentheses:
type NestedUnion =
| (
| "thing1" // Comment1
| "thing2"
) // Comment2
| Bar;
This inconsistency is problematic because:
- Comment semantics change:
// Comment2 was originally associated with "thing2", but after formatting it appears to comment on the entire parenthesized group
- Developer intent is lost: Comments are placed deliberately; reformatting should preserve their associations
The expected output keeps the parenthesized union structure intact with comments in their original positions, matching the behavior of UnionInIntersection.
Prettier 3.8.1
Playground link
Input:
Output:
Expected output:
Why?
The current output shows inconsistent comment handling between union-in-intersection and union-in-union cases:
UnionInIntersection- Comments stay on their original lines:NestedUnion- Comments get repositioned, with// Comment2moved outside the parentheses:This inconsistency is problematic because:
// Comment2was originally associated with"thing2", but after formatting it appears to comment on the entire parenthesized groupThe expected output keeps the parenthesized union structure intact with comments in their original positions, matching the behavior of
UnionInIntersection.