Skip to content

Commit ca02b37

Browse files
authored
Fix unstable comment print (#18395)
1 parent 7efb988 commit ca02b37

File tree

7 files changed

+167
-7
lines changed

7 files changed

+167
-7
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#### Fix unstable comment print in union type comments (#18395 by @fisker)
2+
3+
<!-- prettier-ignore -->
4+
```tsx
5+
// Input
6+
type X = (A | B) & (
7+
// comment
8+
A | B
9+
);
10+
11+
// Prettier stable (first format)
12+
type X = (A | B) &
13+
(// comment
14+
A | B);
15+
16+
// Prettier stable (second format)
17+
type X = (
18+
| A
19+
| B // comment
20+
) &
21+
(A | B);
22+
23+
// Prettier main
24+
type X = (A | B) &
25+
// comment
26+
(A | B);
27+
```

src/language-js/comments/will-print-own-comments.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
hasNodeIgnoreComment,
44
isJsxElement,
55
isUnionType,
6+
shouldUnionTypePrintOwnComments,
67
} from "../utilities/index.js";
78

89
/**
@@ -47,7 +48,15 @@ function willPrintOwnComments(path) {
4748
return false;
4849
}
4950

50-
return isJsxElement(node) || isUnionType(node);
51+
if (isUnionType(node)) {
52+
return shouldUnionTypePrintOwnComments(path);
53+
}
54+
55+
if (isJsxElement(node)) {
56+
return true;
57+
}
58+
59+
return false;
5160
}
5261

5362
export default willPrintOwnComments;

src/language-js/print/union-type.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@ import {
1919
hasLeadingOwnLineComment,
2020
isConditionalType,
2121
isTypeAlias,
22-
isUnionType,
22+
shouldUnionTypePrintOwnComments,
2323
} from "../utilities/index.js";
2424

25+
/**
26+
@import {Doc} from "../../document/index.js";
27+
*/
28+
2529
// `TSUnionType` and `UnionTypeAnnotation`
2630
function printUnionType(path, options, print) {
2731
const { node } = path;
@@ -78,11 +82,13 @@ function printUnionType(path, options, print) {
7882
return printComments(path, printedType, options);
7983
}, "types");
8084

81-
const { leading, trailing } =
82-
// If it's `types` of union type, parent will print comment for us
83-
path.key === "types" && isUnionType(path.parent)
84-
? { leading: "", trailing: "" }
85-
: printCommentsSeparately(path, options);
85+
/** @type {Doc} */
86+
let leading = "";
87+
/** @type {Doc} */
88+
let trailing = "";
89+
if (shouldUnionTypePrintOwnComments(path)) {
90+
({ leading, trailing } = printCommentsSeparately(path, options));
91+
}
8692

8793
if (shouldHug) {
8894
return [leading, join(" | ", printed), trailing];

src/language-js/utilities/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,6 +1085,19 @@ const isTypeAlias = createTypeCheckFunction([
10851085
"TypeAlias",
10861086
]);
10871087

1088+
function shouldUnionTypePrintOwnComments({ key, parent }) {
1089+
if (
1090+
// If it's `types` of union type, parent will print comment for it
1091+
(key === "types" && isUnionType(parent)) ||
1092+
// Inside intersection type let the comment print outside of parentheses
1093+
(key === "types" && isIntersectionType(parent))
1094+
) {
1095+
return false;
1096+
}
1097+
1098+
return true;
1099+
}
1100+
10881101
export {
10891102
CommentCheckFlags,
10901103
createTypeCheckFunction,
@@ -1145,6 +1158,7 @@ export {
11451158
needsHardlineAfterDanglingComment,
11461159
shouldFlatten,
11471160
shouldPrintComma,
1161+
shouldUnionTypePrintOwnComments,
11481162
startsWithNoLookaheadToken,
11491163
};
11501164
export { default as isMeaningfulEmptyStatement } from "./is-meaningful-empty-statement.js";

src/main/comments/print.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ function printDanglingComments(
155155
return shouldIndent ? indent([hardline, doc]) : doc;
156156
}
157157

158+
/**
159+
@returns {{leading?: Doc, trailing?: Doc}}
160+
*/
158161
function printCommentsSeparately(path, options) {
159162
const value = path.node;
160163
if (!value) {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
type A1 =
2+
(
3+
A | B // comment 1
4+
) & (
5+
// comment2
6+
A | B
7+
)
8+
9+
type A2 =
10+
(
11+
A | B // prettier-ignore
12+
) & (
13+
// prettier-ignore
14+
A | B
15+
)
16+
17+
type A1 =
18+
// comment 1
19+
(A | B)
20+
& (
21+
// comment2
22+
A | B
23+
)
24+
25+
type A1 =
26+
// prettier-ignore
27+
(A | B)
28+
& (
29+
// prettier-ignore
30+
A | B
31+
)

tests/format/typescript/union/comments/__snapshots__/format.test.js.snap

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,76 @@ type A3 =
9696
================================================================================
9797
`;
9898

99+
exports[`18379.ts format 1`] = `
100+
====================================options=====================================
101+
parsers: ["typescript", "flow"]
102+
printWidth: 80
103+
| printWidth
104+
=====================================input======================================
105+
type A1 =
106+
(
107+
A | B // comment 1
108+
) & (
109+
// comment2
110+
A | B
111+
)
112+
113+
type A2 =
114+
(
115+
A | B // prettier-ignore
116+
) & (
117+
// prettier-ignore
118+
A | B
119+
)
120+
121+
type A1 =
122+
// comment 1
123+
(A | B)
124+
& (
125+
// comment2
126+
A | B
127+
)
128+
129+
type A1 =
130+
// prettier-ignore
131+
(A | B)
132+
& (
133+
// prettier-ignore
134+
A | B
135+
)
136+
137+
=====================================output=====================================
138+
type A1 = (
139+
| A
140+
| B // comment 1
141+
) &
142+
// comment2
143+
(A | B);
144+
145+
type A2 = (
146+
| A
147+
| B // prettier-ignore
148+
) &
149+
// prettier-ignore
150+
(A | B);
151+
152+
type A1 =
153+
// comment 1
154+
(A | B) &
155+
// comment2
156+
(A | B);
157+
158+
type A1 =
159+
// prettier-ignore
160+
(A | B)
161+
& (
162+
// prettier-ignore
163+
A | B
164+
);
165+
166+
================================================================================
167+
`;
168+
99169
exports[`18389.ts format 1`] = `
100170
====================================options=====================================
101171
parsers: ["typescript", "flow"]

0 commit comments

Comments
 (0)