From 9f2fad7aa47ff6a95ea768e2ad79a024862f5a0d Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Wed, 19 Nov 2025 13:04:32 +0800 Subject: [PATCH] Fix comments for method signature (#18287) --- src/language-js/comments/handle-comments.js | 2 + .../18216-mutiple-clauses.ts | 0 .../18216-type-parameters-mutiple-clauses.ts | 0 .../18216-type-parameters.ts | 0 .../18216.ts | 0 .../__snapshots__/format.test.js.snap | 0 .../format.test.js | 0 .../interface2/comments-ts-only/18278.ts | 23 ++++++++ .../__snapshots__/format.test.js.snap | 59 +++++++++++++++++++ .../comments-ts-only/format.test.js | 1 + 10 files changed, 85 insertions(+) rename tests/format/typescript/interface2/{comments => comments-ts-and-flow}/18216-mutiple-clauses.ts (100%) rename tests/format/typescript/interface2/{comments => comments-ts-and-flow}/18216-type-parameters-mutiple-clauses.ts (100%) rename tests/format/typescript/interface2/{comments => comments-ts-and-flow}/18216-type-parameters.ts (100%) rename tests/format/typescript/interface2/{comments => comments-ts-and-flow}/18216.ts (100%) rename tests/format/typescript/interface2/{comments => comments-ts-and-flow}/__snapshots__/format.test.js.snap (100%) rename tests/format/typescript/interface2/{comments => comments-ts-and-flow}/format.test.js (100%) create mode 100644 tests/format/typescript/interface2/comments-ts-only/18278.ts create mode 100644 tests/format/typescript/interface2/comments-ts-only/__snapshots__/format.test.js.snap create mode 100644 tests/format/typescript/interface2/comments-ts-only/format.test.js diff --git a/src/language-js/comments/handle-comments.js b/src/language-js/comments/handle-comments.js index ed46701fd..e03ec7ef1 100644 --- a/src/language-js/comments/handle-comments.js +++ b/src/language-js/comments/handle-comments.js @@ -931,6 +931,7 @@ function handleVariableDeclaratorComments({ function handleTSFunctionTrailingComments({ comment, enclosingNode, + precedingNode, followingNode, text, }) { @@ -939,6 +940,7 @@ function handleTSFunctionTrailingComments({ (enclosingNode?.type === "TSMethodSignature" || enclosingNode?.type === "TSDeclareFunction" || enclosingNode?.type === "TSAbstractMethodDefinition") && + (!precedingNode || precedingNode !== enclosingNode.returnType) && getNextNonSpaceNonCommentCharacter(text, locEnd(comment)) === ";" ) { addTrailingComment(enclosingNode, comment); diff --git a/tests/format/typescript/interface2/comments/18216-mutiple-clauses.ts b/tests/format/typescript/interface2/comments-ts-and-flow/18216-mutiple-clauses.ts similarity index 100% rename from tests/format/typescript/interface2/comments/18216-mutiple-clauses.ts rename to tests/format/typescript/interface2/comments-ts-and-flow/18216-mutiple-clauses.ts diff --git a/tests/format/typescript/interface2/comments/18216-type-parameters-mutiple-clauses.ts b/tests/format/typescript/interface2/comments-ts-and-flow/18216-type-parameters-mutiple-clauses.ts similarity index 100% rename from tests/format/typescript/interface2/comments/18216-type-parameters-mutiple-clauses.ts rename to tests/format/typescript/interface2/comments-ts-and-flow/18216-type-parameters-mutiple-clauses.ts diff --git a/tests/format/typescript/interface2/comments/18216-type-parameters.ts b/tests/format/typescript/interface2/comments-ts-and-flow/18216-type-parameters.ts similarity index 100% rename from tests/format/typescript/interface2/comments/18216-type-parameters.ts rename to tests/format/typescript/interface2/comments-ts-and-flow/18216-type-parameters.ts diff --git a/tests/format/typescript/interface2/comments/18216.ts b/tests/format/typescript/interface2/comments-ts-and-flow/18216.ts similarity index 100% rename from tests/format/typescript/interface2/comments/18216.ts rename to tests/format/typescript/interface2/comments-ts-and-flow/18216.ts diff --git a/tests/format/typescript/interface2/comments/__snapshots__/format.test.js.snap b/tests/format/typescript/interface2/comments-ts-and-flow/__snapshots__/format.test.js.snap similarity index 100% rename from tests/format/typescript/interface2/comments/__snapshots__/format.test.js.snap rename to tests/format/typescript/interface2/comments-ts-and-flow/__snapshots__/format.test.js.snap diff --git a/tests/format/typescript/interface2/comments/format.test.js b/tests/format/typescript/interface2/comments-ts-and-flow/format.test.js similarity index 100% rename from tests/format/typescript/interface2/comments/format.test.js rename to tests/format/typescript/interface2/comments-ts-and-flow/format.test.js diff --git a/tests/format/typescript/interface2/comments-ts-only/18278.ts b/tests/format/typescript/interface2/comments-ts-only/18278.ts new file mode 100644 index 000000000..71766118f --- /dev/null +++ b/tests/format/typescript/interface2/comments-ts-only/18278.ts @@ -0,0 +1,23 @@ +interface a { + bar():any /* bat */; +} + +type b = { + bar():any /* bat */; +} + +declare class c { + bar():any /* bat */; +} + +// https://github.com/babel/babel/blob/c92c4919771105140015167f25f7bacac77c90d9/scripts/babel-plugin-bit-decorator/types.d.ts#L28-L37 +export type BitDecorator = BitDecoratorCall & { + (assertMask: number): BitDecoratorCall; + + storage( + value: unknown, + context: ClassFieldDecoratorContext + ): void /*{ + context.metadata.bitsStorage = context.access; + }*/; +}; diff --git a/tests/format/typescript/interface2/comments-ts-only/__snapshots__/format.test.js.snap b/tests/format/typescript/interface2/comments-ts-only/__snapshots__/format.test.js.snap new file mode 100644 index 000000000..de86867a4 --- /dev/null +++ b/tests/format/typescript/interface2/comments-ts-only/__snapshots__/format.test.js.snap @@ -0,0 +1,59 @@ +// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing + +exports[`18278.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +interface a { + bar():any /* bat */; +} + +type b = { + bar():any /* bat */; +} + +declare class c { + bar():any /* bat */; +} + +// https://github.com/babel/babel/blob/c92c4919771105140015167f25f7bacac77c90d9/scripts/babel-plugin-bit-decorator/types.d.ts#L28-L37 +export type BitDecorator = BitDecoratorCall & { + (assertMask: number): BitDecoratorCall; + + storage( + value: unknown, + context: ClassFieldDecoratorContext + ): void /*{ + context.metadata.bitsStorage = context.access; + }*/; +}; + +=====================================output===================================== +interface a { + bar(): any /* bat */; +} + +type b = { + bar(): any /* bat */; +}; + +declare class c { + bar(): any /* bat */; +} + +// https://github.com/babel/babel/blob/c92c4919771105140015167f25f7bacac77c90d9/scripts/babel-plugin-bit-decorator/types.d.ts#L28-L37 +export type BitDecorator = BitDecoratorCall & { + (assertMask: number): BitDecoratorCall; + + storage( + value: unknown, + context: ClassFieldDecoratorContext, + ): void /*{ + context.metadata.bitsStorage = context.access; + }*/; +}; + +================================================================================ +`; diff --git a/tests/format/typescript/interface2/comments-ts-only/format.test.js b/tests/format/typescript/interface2/comments-ts-only/format.test.js new file mode 100644 index 000000000..116dcbd78 --- /dev/null +++ b/tests/format/typescript/interface2/comments-ts-only/format.test.js @@ -0,0 +1 @@ +runFormatTest(import.meta, ["typescript"]);