diff --git a/changelog_unreleased/typescript/19572.md b/changelog_unreleased/typescript/19572.md new file mode 100644 index 000000000..114051ad1 --- /dev/null +++ b/changelog_unreleased/typescript/19572.md @@ -0,0 +1,24 @@ +#### Ignore comments inside mapped type when checking type parameter comments (#19572 by @fisker) + + +```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; +}>(); +``` diff --git a/src/language-js/print/type-parameters.js b/src/language-js/print/type-parameters.js index a2d74fc42..40d9ed706 100644 --- a/src/language-js/print/type-parameters.js +++ b/src/language-js/print/type-parameters.js @@ -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 [ diff --git a/tests/format/typescript/type-parameters-arguments/19505.ts b/tests/format/typescript/type-parameters-arguments/19505.ts new file mode 100644 index 000000000..07e53b34d --- /dev/null +++ b/tests/format/typescript/type-parameters-arguments/19505.ts @@ -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 +]>(); diff --git a/tests/format/typescript/type-parameters-arguments/__snapshots__/format.test.js.snap b/tests/format/typescript/type-parameters-arguments/__snapshots__/format.test.js.snap index 79bec9af6..33ee49f43 100644 --- a/tests/format/typescript/type-parameters-arguments/__snapshots__/format.test.js.snap +++ b/tests/format/typescript/type-parameters-arguments/__snapshots__/format.test.js.snap @@ -130,6 +130,70 @@ foo(/* 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"]