diff --git a/crates/oxc_formatter/src/write/mod.rs b/crates/oxc_formatter/src/write/mod.rs index 8f1422491e4be..512d03b27e653 100644 --- a/crates/oxc_formatter/src/write/mod.rs +++ b/crates/oxc_formatter/src/write/mod.rs @@ -1477,12 +1477,6 @@ impl<'a> Format<'a> for FormatTSSignature<'a, '_> { } } Semicolons::AsNeeded => { - let TSSignature::TSPropertySignature(property) = self.signature.as_ref() else { - return; - }; - - let has_no_type_annotation = property.type_annotation.is_none(); - // Needs semicolon anyway when: // 1. It's a non-computed property signature with type annotation followed by // a call signature that has type parameters @@ -1490,14 +1484,16 @@ impl<'a> Format<'a> for FormatTSSignature<'a, '_> { // 2. It's a non-computed property signature without type annotation followed by // a call signature or method signature // e.g for: `a; () => void` or `a; method(): void` - let needs_semicolon = !property.computed + let needs_semicolon = matches!( + self.signature.as_ref(), TSSignature::TSPropertySignature(property) if !property.computed && self.next_signature.is_some_and(|signature| match signature.as_ref() { TSSignature::TSCallSignatureDeclaration(call) => { - has_no_type_annotation || call.type_parameters.is_some() + property.type_annotation.is_none() || call.type_parameters.is_some() } - TSSignature::TSMethodSignature(_) => has_no_type_annotation, + TSSignature::TSMethodSignature(_) => property.type_annotation.is_none(), _ => false, - }); + }) + ); if needs_semicolon { write!(f, [";"]); diff --git a/crates/oxc_formatter/tests/fixtures/ts/semi/method-signatures.ts b/crates/oxc_formatter/tests/fixtures/ts/semi/method-signatures.ts index 18d7114e2cf09..3daaaea180ce5 100644 --- a/crates/oxc_formatter/tests/fixtures/ts/semi/method-signatures.ts +++ b/crates/oxc_formatter/tests/fixtures/ts/semi/method-signatures.ts @@ -2,3 +2,7 @@ export interface MethodSignatures { (arg: string): void; (arg: number): null; } + +export type MethodSignaturesAndPropertyOnSameLine = + | { json(): never; ok: false } + | { json(): Promise; ok: true } diff --git a/crates/oxc_formatter/tests/fixtures/ts/semi/method-signatures.ts.snap b/crates/oxc_formatter/tests/fixtures/ts/semi/method-signatures.ts.snap index 9038f88b68eb6..dbcc104d4957d 100644 --- a/crates/oxc_formatter/tests/fixtures/ts/semi/method-signatures.ts.snap +++ b/crates/oxc_formatter/tests/fixtures/ts/semi/method-signatures.ts.snap @@ -7,6 +7,10 @@ export interface MethodSignatures { (arg: number): null; } +export type MethodSignaturesAndPropertyOnSameLine = + | { json(): never; ok: false } + | { json(): Promise; ok: true } + ==================== Output ==================== ------------------------------- { printWidth: 80, semi: false } @@ -16,6 +20,10 @@ export interface MethodSignatures { (arg: number): null } +export type MethodSignaturesAndPropertyOnSameLine = + | { json(): never; ok: false } + | { json(): Promise; ok: true } + -------------------------------- { printWidth: 100, semi: false } -------------------------------- @@ -24,6 +32,10 @@ export interface MethodSignatures { (arg: number): null } +export type MethodSignaturesAndPropertyOnSameLine = + | { json(): never; ok: false } + | { json(): Promise; ok: true } + ------------------------------ { printWidth: 80, semi: true } ------------------------------ @@ -32,6 +44,10 @@ export interface MethodSignatures { (arg: number): null; } +export type MethodSignaturesAndPropertyOnSameLine = + | { json(): never; ok: false } + | { json(): Promise; ok: true }; + ------------------------------- { printWidth: 100, semi: true } ------------------------------- @@ -40,4 +56,8 @@ export interface MethodSignatures { (arg: number): null; } +export type MethodSignaturesAndPropertyOnSameLine = + | { json(): never; ok: false } + | { json(): Promise; ok: true }; + ===================== End =====================