diff --git a/changelog_unreleased/typescript/16891.md b/changelog_unreleased/typescript/16891.md new file mode 100644 index 000000000..df150bbad --- /dev/null +++ b/changelog_unreleased/typescript/16891.md @@ -0,0 +1,40 @@ +#### Fix comments print on class methods with decorators (#16891 by @fisker) + + +```jsx +// Input +class A { + @decorator + /** + * The method description + * + */ + async method(foo: Foo, bar: Bar) { + console.log(foo); + } +} + +// Prettier stable +class A { + @decorator + async /** + * The method description + * + */ + method(foo: Foo, bar: Bar) { + console.log(foo); + } +} + +// Prettier main +class A { + @decorator + /** + * The method description + * + */ + async method(foo: Foo, bar: Bar) { + console.log(foo); + } +} +``` diff --git a/src/language-js/comments/handle-comments.js b/src/language-js/comments/handle-comments.js index 5f29dbc07..45036f3c3 100644 --- a/src/language-js/comments/handle-comments.js +++ b/src/language-js/comments/handle-comments.js @@ -543,7 +543,7 @@ function handleMethodNameComments({ if ( precedingNode?.type === "Decorator" && propertyLikeNodeTypes.has(enclosingNode?.type) && - isLineComment(comment) + (isLineComment(comment) || comment.placement === "ownLine") ) { addTrailingComment(precedingNode, comment); return true; diff --git a/tests/format/typescript/comments/16889.ts b/tests/format/typescript/comments/16889.ts new file mode 100644 index 000000000..4c1665979 --- /dev/null +++ b/tests/format/typescript/comments/16889.ts @@ -0,0 +1,71 @@ +class A { + @decorator + /** + * The method description + * + */ + async method() { + } + + @decorator/** + * The method description + * + */ + async method() { + } + + @decorator/** + * The method description + * + */ async method() { + } + + @decorator + async /* comment */ method() { + } + + @decorator /* comment */ async method() { + } + + @decorator + // line comment + async method() { + } + + @decorator // line comment + async method() { + } + + + @decorator + /* comment */ + public async method() { + } + + @decorator + /* comment */ + static async method() { + } + + @decorator + /* comment */ + protected async method() { + } + + @decorator + /* comment */ + protected async method() { + } + + @decorator + /* comment */ + * method() {} + + @decorator + * /* comment */ method() {} + + /* comment */ + abstract method():void; + +} + diff --git a/tests/format/typescript/comments/__snapshots__/format.test.js.snap b/tests/format/typescript/comments/__snapshots__/format.test.js.snap index 21c8d1680..5d8c59465 100644 --- a/tests/format/typescript/comments/__snapshots__/format.test.js.snap +++ b/tests/format/typescript/comments/__snapshots__/format.test.js.snap @@ -248,6 +248,147 @@ class Foo { ================================================================================ `; +exports[`16889.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +class A { + @decorator + /** + * The method description + * + */ + async method() { + } + + @decorator/** + * The method description + * + */ + async method() { + } + + @decorator/** + * The method description + * + */ async method() { + } + + @decorator + async /* comment */ method() { + } + + @decorator /* comment */ async method() { + } + + @decorator + // line comment + async method() { + } + + @decorator // line comment + async method() { + } + + + @decorator + /* comment */ + public async method() { + } + + @decorator + /* comment */ + static async method() { + } + + @decorator + /* comment */ + protected async method() { + } + + @decorator + /* comment */ + protected async method() { + } + + @decorator + /* comment */ + * method() {} + + @decorator + * /* comment */ method() {} + + /* comment */ + abstract method():void; + +} + + +=====================================output===================================== +class A { + @decorator + /** + * The method description + * + */ + async method() {} + + @decorator /** + * The method description + * + */ + async method() {} + + @decorator /** + * The method description + * + */ + async method() {} + + @decorator + async /* comment */ method() {} + + @decorator /* comment */ async method() {} + + @decorator + // line comment + async method() {} + + @decorator // line comment + async method() {} + + @decorator + /* comment */ + public async method() {} + + @decorator + /* comment */ + static async method() {} + + @decorator + /* comment */ + protected async method() {} + + @decorator + /* comment */ + protected async method() {} + + @decorator + /* comment */ + *method() {} + + @decorator + */* comment */ method() {} + + /* comment */ + abstract method(): void; +} + +================================================================================ +`; + exports[`abstract_class.ts format 1`] = ` ====================================options===================================== parsers: ["typescript"]