diff --git a/changelog_unreleased/typescript/15324.md b/changelog_unreleased/typescript/15324.md new file mode 100644 index 000000000..67c65752c --- /dev/null +++ b/changelog_unreleased/typescript/15324.md @@ -0,0 +1,34 @@ +#### Fix unstable comment after the last parameter property (#15324 by @fisker) + + +```tsx +// Input +class Class { + constructor( + private readonly paramProp: Type, + // comment + ) { + } +} + +// Prettier stable +class Class { + constructor(private readonly paramProp: Type) // comment + {} +} + +// Prettier stable (Second format) +class Class { + constructor( + private readonly paramProp: Type, // comment + ) {} +} + +// Prettier main +class Class { + constructor( + private readonly paramProp: Type, + // comment + ) {} +} +``` diff --git a/src/language-js/comments/handle-comments.js b/src/language-js/comments/handle-comments.js index dfbdab0f6..5b0874c16 100644 --- a/src/language-js/comments/handle-comments.js +++ b/src/language-js/comments/handle-comments.js @@ -576,8 +576,8 @@ function handleLastFunctionArgComments({ precedingNode?.type === "AssignmentPattern" || precedingNode?.type === "ObjectPattern" || precedingNode?.type === "ArrayPattern" || - precedingNode?.type === "RestElement") && - enclosingNode && + precedingNode?.type === "RestElement" || + precedingNode?.type === "TSParameterProperty") && isRealFunctionLikeNode(enclosingNode) && getNextNonSpaceNonCommentCharacter(text, locEnd(comment)) === ")" ) { diff --git a/tests/format/typescript/comments/__snapshots__/jsfmt.spec.js.snap b/tests/format/typescript/comments/__snapshots__/jsfmt.spec.js.snap index 2874d7786..5970bf034 100644 --- a/tests/format/typescript/comments/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/typescript/comments/__snapshots__/jsfmt.spec.js.snap @@ -694,6 +694,31 @@ export class Point { ================================================================================ `; +exports[`ts-parameter-proerty.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +class A { + constructor( + private readonly paramProp: Type, + // comment + ) { + } +} + +=====================================output===================================== +class A { + constructor( + private readonly paramProp: Type, + // comment + ) {} +} + +================================================================================ +`; + exports[`type_literals.ts format 1`] = ` ====================================options===================================== parsers: ["typescript"] diff --git a/tests/format/typescript/comments/ts-parameter-proerty.ts b/tests/format/typescript/comments/ts-parameter-proerty.ts new file mode 100644 index 000000000..84cf85cc8 --- /dev/null +++ b/tests/format/typescript/comments/ts-parameter-proerty.ts @@ -0,0 +1,7 @@ +class A { + constructor( + private readonly paramProp: Type, + // comment + ) { + } +}