Skip to content

Commit 3d063b5

Browse files
authored
Ignore dangling comments when checking type parameter comments (#19572)
1 parent 908503e commit 3d063b5

4 files changed

Lines changed: 132 additions & 10 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#### Ignore comments inside mapped type when checking type parameter comments (#19572 by @fisker)
2+
3+
<!-- prettier-ignore -->
4+
```tsx
5+
// Input
6+
foo<{
7+
// comment
8+
[key in keyof Foo]: number
9+
}>();
10+
11+
// Prettier stable
12+
foo<
13+
{
14+
// comment
15+
[key in keyof Foo]: number;
16+
}
17+
>();
18+
19+
// Prettier main
20+
foo<{
21+
// comment
22+
[key in keyof Foo]: number;
23+
}>();
24+
```

‎src/language-js/print/type-parameters.js‎

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ import {
1111
import { printDanglingComments } from "../../main/comments/print.js";
1212
import hasNewline from "../../utilities/has-newline.js";
1313
import { locEnd } from "../location/index.js";
14-
import { CommentCheckFlags, hasComment } from "../utilities/comments.js";
14+
import { isLineComment } from "../utilities/comment-types.js";
15+
import {
16+
CommentCheckFlags,
17+
getComments,
18+
hasComment,
19+
} from "../utilities/comments.js";
1520
import { getFunctionParameters } from "../utilities/function-parameters.js";
1621
import { isObjectType } from "../utilities/node-types.js";
1722
import { isTestCall } from "../utilities/test-libraries.js";
@@ -83,15 +88,19 @@ function printTypeParameters(path, options, print, paramsKey) {
8388
(parameters.length === 1 &&
8489
(parameters[0].type === "NullableTypeAnnotation" ||
8590
shouldHugType(parameters[0])))) &&
86-
!parameters.some(
87-
(node) =>
88-
hasComment(node, CommentCheckFlags.Line) ||
89-
// This condition base on existing one in class-body.js
90-
// It is not really correct, but we don't have a way to check how comments are printed
91-
hasComment(node, CommentCheckFlags.Last, (comment) =>
92-
hasNewline(options.originalText, locEnd(comment)),
93-
),
94-
));
91+
!parameters.some((node) => {
92+
const comments = getComments(
93+
node,
94+
(comment) => comment.leading || comment.trailing,
95+
);
96+
return (
97+
comments.length > 0 &&
98+
(comments.some((comment) => isLineComment(comment)) ||
99+
// This condition base on existing one in class-body.js
100+
// It is not really correct, but we don't have a way to check how comments are printed
101+
hasNewline(options.originalText, locEnd(comments.at(-1))))
102+
);
103+
}));
95104

96105
if (shouldInline) {
97106
return [
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
createObject<{
2+
// dangling comment
3+
[key in keyof Function]: TypeInfo | null
4+
}>()
5+
6+
createObject<
7+
// comment
8+
{
9+
[key in keyof Function]: TypeInfo | null
10+
}>()
11+
12+
createObject<
13+
{
14+
[key in keyof Function]: TypeInfo | null
15+
}
16+
// comment
17+
>()
18+
19+
foo<{
20+
// comment
21+
}>();
22+
23+
foo<[
24+
// comment
25+
]>();

‎tests/format/typescript/type-parameters-arguments/__snapshots__/format.test.js.snap‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,70 @@ foo</* Comment */ Type>(/* Comment */ value);
130130
================================================================================
131131
`;
132132

133+
exports[`19505.ts format 1`] = `
134+
====================================options=====================================
135+
parsers: ["typescript"]
136+
printWidth: 80 (default) |
137+
=====================================input======================================
138+
createObject<{
139+
// dangling comment
140+
[key in keyof Function]: TypeInfo | null
141+
}>()
142+
143+
createObject<
144+
// comment
145+
{
146+
[key in keyof Function]: TypeInfo | null
147+
}>()
148+
149+
createObject<
150+
{
151+
[key in keyof Function]: TypeInfo | null
152+
}
153+
// comment
154+
>()
155+
156+
foo<{
157+
// comment
158+
}>();
159+
160+
foo<[
161+
// comment
162+
]>();
163+
164+
=====================================output=====================================
165+
createObject<{
166+
// dangling comment
167+
[key in keyof Function]: TypeInfo | null;
168+
}>();
169+
170+
createObject<
171+
// comment
172+
{
173+
[key in keyof Function]: TypeInfo | null;
174+
}
175+
>();
176+
177+
createObject<
178+
{
179+
[key in keyof Function]: TypeInfo | null;
180+
}
181+
// comment
182+
>();
183+
184+
foo<{
185+
// comment
186+
}>();
187+
188+
foo<
189+
[
190+
// comment
191+
]
192+
>();
193+
194+
================================================================================
195+
`;
196+
133197
exports[`class-method.ts format 1`] = `
134198
====================================options=====================================
135199
parsers: ["typescript"]

0 commit comments

Comments
 (0)