diff --git a/changelog_unreleased/typescript/19212.md b/changelog_unreleased/typescript/19212.md new file mode 100644 index 000000000..5ae4fea7e --- /dev/null +++ b/changelog_unreleased/typescript/19212.md @@ -0,0 +1,25 @@ +#### Avoid unnecessary semicolon before call signatures in `no-semi` mode (#19212 by @fisker) + + +```tsx +// Input +export interface MyInterface { + someMethod: (a: number) => Promise + anotherMethod: (a: string) => Promise + (a: string): Promise +} + +// Prettier stable (--no-semi) +export interface MyInterface { + someMethod: (a: number) => Promise + anotherMethod: (a: string) => Promise; + (a: string): Promise +} + +// Prettier main +export interface MyInterface { + someMethod: (a: number) => Promise + anotherMethod: (a: string) => Promise + (a: string): Promise +} +``` diff --git a/src/language-js/print/class-body.js b/src/language-js/print/class-body.js index b38fce42b..02b71cc3c 100644 --- a/src/language-js/print/class-body.js +++ b/src/language-js/print/class-body.js @@ -323,6 +323,10 @@ function shouldPrintSemicolonAfterInterfaceProperty( switch (nextNode.type) { case "TSCallSignatureDeclaration": + if (node.type === "TSPropertySignature" && node.typeAnnotation) { + return false; + } + return true; } diff --git a/tests/format/typescript/interface/no-semi/18858.ts b/tests/format/typescript/interface/no-semi/18858.ts new file mode 100644 index 000000000..a3c6f4782 --- /dev/null +++ b/tests/format/typescript/interface/no-semi/18858.ts @@ -0,0 +1,36 @@ +export interface MyInterface { + someMethod: (a: number) => Promise + (a: string): Promise +} + +export type MyType = { + someMethod: (a: number) => Promise + (a: string): Promise +} + +interface I1 { + prop: string; + ():string +} +type T1 ={ + prop: string; + ():string +} + +interface I2 { + prop; + ():string +} +type T2 ={ + prop; + ():string +} + +interface I3 { + prop: Foo; + ():string +} +type T3 ={ + prop: Foo; + ():string +} diff --git a/tests/format/typescript/interface/no-semi/__snapshots__/format.test.js.snap b/tests/format/typescript/interface/no-semi/__snapshots__/format.test.js.snap index f5e4d1cf4..a6479ddfc 100644 --- a/tests/format/typescript/interface/no-semi/__snapshots__/format.test.js.snap +++ b/tests/format/typescript/interface/no-semi/__snapshots__/format.test.js.snap @@ -174,14 +174,98 @@ type G2 = { type G3 = { get: X; foo(): X } interface H1 { - foo: X; + foo: X (): T } type H2 = { - foo: X; + foo: X (): T } type H3 = { foo: X; (): T } ================================================================================ `; + +exports[`18858.ts - {"semi":false} format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +semi: false + printWidth: 80 (default) | +=====================================input====================================== +export interface MyInterface { + someMethod: (a: number) => Promise + (a: string): Promise +} + +export type MyType = { + someMethod: (a: number) => Promise + (a: string): Promise +} + +interface I1 { + prop: string; + ():string +} +type T1 ={ + prop: string; + ():string +} + +interface I2 { + prop; + ():string +} +type T2 ={ + prop; + ():string +} + +interface I3 { + prop: Foo; + ():string +} +type T3 ={ + prop: Foo; + ():string +} + +=====================================output===================================== +export interface MyInterface { + someMethod: (a: number) => Promise + (a: string): Promise +} + +export type MyType = { + someMethod: (a: number) => Promise + (a: string): Promise +} + +interface I1 { + prop: string + (): string +} +type T1 = { + prop: string + (): string +} + +interface I2 { + prop; + (): string +} +type T2 = { + prop; + (): string +} + +interface I3 { + prop: Foo + (): string +} +type T3 = { + prop: Foo + (): string +} + +================================================================================ +`;