diff --git a/changelog_unreleased/typescript/16574.md b/changelog_unreleased/typescript/16574.md new file mode 100644 index 000000000..ed79e8c5a --- /dev/null +++ b/changelog_unreleased/typescript/16574.md @@ -0,0 +1,35 @@ +#### Preserve a comment on between decorator and modified parameter property (#16574 by @sosukesuzuki) + +The current version of Prettier unexpectedly moves a line comment between a parameter property modified by `readonly`, `private`, `public`, etc and a decorator. This output results in invalid TypeScript code. + +This change ensures that the original format is preserved. + + +```ts +// Input +class Foo { + constructor( + @decorator + // comment + readonly foo, + ) {} +} + +// Output (stable) +class Foo { + constructor( + @decorator + readonly // comment + foo, + ) {} +} + +// Output (main) +class Foo { + constructor( + @decorator + // comment + readonly foo, + ) {} +} +``` diff --git a/src/language-js/comments/handle-comments.js b/src/language-js/comments/handle-comments.js index 407f80b86..dbe120c97 100644 --- a/src/language-js/comments/handle-comments.js +++ b/src/language-js/comments/handle-comments.js @@ -495,6 +495,7 @@ const propertyLikeNodeTypes = new Set([ "ClassAccessorProperty", "AccessorProperty", "TSAbstractAccessorProperty", + "TSParameterProperty", ]); function handleMethodNameComments({ comment, diff --git a/tests/format/typescript/comments/16065.ts b/tests/format/typescript/comments/16065.ts new file mode 100644 index 000000000..101c1278b --- /dev/null +++ b/tests/format/typescript/comments/16065.ts @@ -0,0 +1,11 @@ +class Foo { + constructor( + @decorator1 + // comment1 + readonly baz1: string, + + @decorator2 + // comment2 + private baz2: string, + ) {} +} diff --git a/tests/format/typescript/comments/__snapshots__/format.test.js.snap b/tests/format/typescript/comments/__snapshots__/format.test.js.snap index 1c8718267..254b0e7f8 100644 --- a/tests/format/typescript/comments/__snapshots__/format.test.js.snap +++ b/tests/format/typescript/comments/__snapshots__/format.test.js.snap @@ -122,6 +122,40 @@ const [ ================================================================================ `; +exports[`16065.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +class Foo { + constructor( + @decorator1 + // comment1 + readonly baz1: string, + + @decorator2 + // comment2 + private baz2: string, + ) {} +} + +=====================================output===================================== +class Foo { + constructor( + @decorator1 + // comment1 + readonly baz1: string, + + @decorator2 + // comment2 + private baz2: string, + ) {} +} + +================================================================================ +`; + exports[`abstract_class.ts format 1`] = ` ====================================options===================================== parsers: ["typescript"]