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"]);