From 72ac2bbe5f2c821b6820b427dfe577a20415a143 Mon Sep 17 00:00:00 2001 From: Lucas Duailibe Date: Sat, 29 Jan 2022 19:25:53 -0300 Subject: [PATCH] Fix comment in abstract methods (#12185) --- changelog_unreleased/javascript/12185.md | 26 +++++++++++++ src/language-js/comments.js | 1 + .../comments/__snapshots__/jsfmt.spec.js.snap | 38 +++++++++++++++++++ .../typescript/comments/abstract_methods.ts | 11 ++++++ tests/format/typescript/comments/methods.ts | 2 + 5 files changed, 78 insertions(+) create mode 100644 changelog_unreleased/javascript/12185.md create mode 100644 tests/format/typescript/comments/abstract_methods.ts diff --git a/changelog_unreleased/javascript/12185.md b/changelog_unreleased/javascript/12185.md new file mode 100644 index 000000000..94fcaf7ea --- /dev/null +++ b/changelog_unreleased/javascript/12185.md @@ -0,0 +1,26 @@ +#### Fix misplaced argument comment in abstract methods (#12185 by @duailibe) + + +```ts +// Input +abstract class Foo { + abstract bar( + /** comment explaining userId param */ + userId + ) +} + +// Prettier stable +abstract class Foo { + abstract bar(userId); + /** comment explaining userId param */ +} + +// Prettier main +abstract class Foo { + abstract bar( + /** comment explaining userId param */ + userId + ); +} +``` diff --git a/src/language-js/comments.js b/src/language-js/comments.js index f392f1bbf..d58c99d76 100644 --- a/src/language-js/comments.js +++ b/src/language-js/comments.js @@ -459,6 +459,7 @@ function handleMethodNameComments({ if ( enclosingNode && precedingNode && + getNextNonSpaceNonCommentCharacter(text, comment, locEnd) === "(" && // "MethodDefinition" is handled in getCommentChildNodes (enclosingNode.type === "Property" || enclosingNode.type === "TSDeclareMethod" || diff --git a/tests/format/typescript/comments/__snapshots__/jsfmt.spec.js.snap b/tests/format/typescript/comments/__snapshots__/jsfmt.spec.js.snap index b20ab8598..9d0dde209 100644 --- a/tests/format/typescript/comments/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/typescript/comments/__snapshots__/jsfmt.spec.js.snap @@ -28,6 +28,40 @@ abstract class AbstractRule { ================================================================================ `; +exports[`abstract_methods.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +abstract class AbstractFoo { + abstract method1(/* comment */ arg: string); + abstract method2( + /* comment */ + arg: string + ); + abstract method3( + // comment + arg: string + ); +} + +=====================================output===================================== +abstract class AbstractFoo { + abstract method1(/* comment */ arg: string); + abstract method2( + /* comment */ + arg: string + ); + abstract method3( + // comment + arg: string + ); +} + +================================================================================ +`; + exports[`after_jsx_generic.ts format 1`] = ` ====================================options===================================== parsers: ["typescript"] @@ -602,6 +636,8 @@ export class Point { mismatchedIndentation() {} inline /* foo*/ (/* bar */) /* baz */ {} + + noBody(/* comment */ arg); } =====================================output===================================== @@ -651,6 +687,8 @@ export class Point { mismatchedIndentation() {} inline /* foo*/(/* bar */) /* baz */ {} + + noBody(/* comment */ arg); } ================================================================================ diff --git a/tests/format/typescript/comments/abstract_methods.ts b/tests/format/typescript/comments/abstract_methods.ts new file mode 100644 index 000000000..9ed4bd383 --- /dev/null +++ b/tests/format/typescript/comments/abstract_methods.ts @@ -0,0 +1,11 @@ +abstract class AbstractFoo { + abstract method1(/* comment */ arg: string); + abstract method2( + /* comment */ + arg: string + ); + abstract method3( + // comment + arg: string + ); +} diff --git a/tests/format/typescript/comments/methods.ts b/tests/format/typescript/comments/methods.ts index b48947c79..a4cbca618 100644 --- a/tests/format/typescript/comments/methods.ts +++ b/tests/format/typescript/comments/methods.ts @@ -44,4 +44,6 @@ export class Point { mismatchedIndentation() {} inline /* foo*/ (/* bar */) /* baz */ {} + + noBody(/* comment */ arg); }