From 958b355ccb8ad29a596b7a9a538a6f8d4a095be1 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Sat, 2 Sep 2023 09:16:36 +0800 Subject: [PATCH] Fix comment around `TSParameterProperty` (#15324) --- changelog_unreleased/typescript/15324.md | 34 +++++++++++++++++++ src/language-js/comments/handle-comments.js | 4 +-- .../comments/__snapshots__/jsfmt.spec.js.snap | 25 ++++++++++++++ .../comments/ts-parameter-proerty.ts | 7 ++++ 4 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 changelog_unreleased/typescript/15324.md create mode 100644 tests/format/typescript/comments/ts-parameter-proerty.ts 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 + ) { + } +}