diff --git a/src/services/formatting/rules.ts b/src/services/formatting/rules.ts index 806dc6003f510..45d80841d1894 100644 --- a/src/services/formatting/rules.ts +++ b/src/services/formatting/rules.ts @@ -933,6 +933,13 @@ function isSemicolonDeletionContext(context: FormattingContext): boolean { || nextTokenKind === SyntaxKind.EndOfFileToken; } + if ( + nextTokenKind === SyntaxKind.SemicolonToken && + context.currentTokenSpan.kind === SyntaxKind.SemicolonToken + ) { + return true; + } + if ( nextTokenKind === SyntaxKind.SemicolonClassElement || nextTokenKind === SyntaxKind.SemicolonToken diff --git a/tests/cases/fourslash/formatRemoveSemicolons1.ts b/tests/cases/fourslash/formatRemoveSemicolons1.ts index b80ecee7edb2d..39735fe0a8d93 100644 --- a/tests/cases/fourslash/formatRemoveSemicolons1.ts +++ b/tests/cases/fourslash/formatRemoveSemicolons1.ts @@ -51,7 +51,7 @@ class C { ["p"] zero: void ["one"] = {}; - ["two"]; + ["two"] ; } a; @@ -59,7 +59,7 @@ a; b; (3) 4; -/ regex /; +/ regex / ; []; /** blah */[0] diff --git a/tests/cases/fourslash/formatRemoveSemicolons4.ts b/tests/cases/fourslash/formatRemoveSemicolons4.ts new file mode 100644 index 0000000000000..299efa837a74c --- /dev/null +++ b/tests/cases/fourslash/formatRemoveSemicolons4.ts @@ -0,0 +1,48 @@ +/// + +////declare const opt: number | undefined; +//// +////const a = 1; +////const b = 2; +////;[1, 2, 3] +//// +////const c = opt ? 1 : 2; +////const d = opt ? 1 : 2; +////;[1, 2, 3] +//// +////const e = opt ?? 1; +////const f = opt ?? 1; +////;[1, 2, 3] +//// +////type a = 1; +////type b = 2; +////;[1, 2, 3] +//// +////type c = typeof opt extends 1 ? 1 : 2; +////type d = typeof opt extends 1 ? 1 : 2; +////;[1, 2, 3] + +format.setFormatOptions({ ...format.copyFormatOptions(), semicolons: ts.SemicolonPreference.Remove }); +format.document(); +verify.currentFileContentIs( +`declare const opt: number | undefined + +const a = 1 +const b = 2 +;[1, 2, 3] + +const c = opt ? 1 : 2 +const d = opt ? 1 : 2 +;[1, 2, 3] + +const e = opt ?? 1 +const f = opt ?? 1 +;[1, 2, 3] + +type a = 1 +type b = 2 +;[1, 2, 3] + +type c = typeof opt extends 1 ? 1 : 2 +type d = typeof opt extends 1 ? 1 : 2 +;[1, 2, 3]`);