Ignore dangling comments when checking type parameter comments (#19572)

This commit is contained in:
fisker Cheung authored and GitHub committed 2026-07-09 11:34:36 +08:00
1 parent 908503e9ee
commit 3d063b57b1
4 files changed
+132 -10

No files matched your search

+24
View File
@@ -0,0 +1,24 @@
#### Ignore comments inside mapped type when checking type parameter comments (#19572 by @fisker)
<!-- prettier-ignore -->
```tsx
// Input
foo<{
// comment
[key in keyof Foo]: number
}>();
// Prettier stable
foo<
{
// comment
[key in keyof Foo]: number;
}
>();
// Prettier main
foo<{
// comment
[key in keyof Foo]: number;
}>();
```
+19 -10
View File
@@ -11,7 +11,12 @@ import {
import { printDanglingComments } from "../../main/comments/print.js";
import hasNewline from "../../utilities/has-newline.js";
import { locEnd } from "../location/index.js";
import { CommentCheckFlags, hasComment } from "../utilities/comments.js";
import { isLineComment } from "../utilities/comment-types.js";
import {
CommentCheckFlags,
getComments,
hasComment,
} from "../utilities/comments.js";
import { getFunctionParameters } from "../utilities/function-parameters.js";
import { isObjectType } from "../utilities/node-types.js";
import { isTestCall } from "../utilities/test-libraries.js";
@@ -83,15 +88,19 @@ function printTypeParameters(path, options, print, paramsKey) {
(parameters.length === 1 &&
(parameters[0].type === "NullableTypeAnnotation" ||
shouldHugType(parameters[0])))) &&
!parameters.some(
(node) =>
hasComment(node, CommentCheckFlags.Line) ||
// This condition base on existing one in class-body.js
// It is not really correct, but we don't have a way to check how comments are printed
hasComment(node, CommentCheckFlags.Last, (comment) =>
hasNewline(options.originalText, locEnd(comment)),
),
));
!parameters.some((node) => {
const comments = getComments(
node,
(comment) => comment.leading || comment.trailing,
);
return (
comments.length > 0 &&
(comments.some((comment) => isLineComment(comment)) ||
// This condition base on existing one in class-body.js
// It is not really correct, but we don't have a way to check how comments are printed
hasNewline(options.originalText, locEnd(comments.at(-1))))
);
}));
if (shouldInline) {
return [
@@ -0,0 +1,25 @@
createObject<{
// dangling comment
[key in keyof Function]: TypeInfo | null
}>()
createObject<
// comment
{
[key in keyof Function]: TypeInfo | null
}>()
createObject<
{
[key in keyof Function]: TypeInfo | null
}
// comment
>()
foo<{
// comment
}>();
foo<[
// comment
]>();
@@ -130,6 +130,70 @@ foo</* Comment */ Type>(/* Comment */ value);
================================================================================
`;
exports[`19505.ts format 1`] = `
====================================options=====================================
parsers: ["typescript"]
printWidth: 80 (default) |
=====================================input======================================
createObject<{
// dangling comment
[key in keyof Function]: TypeInfo | null
}>()
createObject<
// comment
{
[key in keyof Function]: TypeInfo | null
}>()
createObject<
{
[key in keyof Function]: TypeInfo | null
}
// comment
>()
foo<{
// comment
}>();
foo<[
// comment
]>();
=====================================output=====================================
createObject<{
// dangling comment
[key in keyof Function]: TypeInfo | null;
}>();
createObject<
// comment
{
[key in keyof Function]: TypeInfo | null;
}
>();
createObject<
{
[key in keyof Function]: TypeInfo | null;
}
// comment
>();
foo<{
// comment
}>();
foo<
[
// comment
]
>();
================================================================================
`;
exports[`class-method.ts format 1`] = `
====================================options=====================================
parsers: ["typescript"]