From a62ac67b5011bd9eeb8d4b9add0de7e3bd487e9a Mon Sep 17 00:00:00 2001 From: "Oleksandr T." Date: Wed, 23 Oct 2024 01:42:28 +0300 Subject: [PATCH] fix(59779): The semicolons: "remove" formatting option doesn't remove extraneous semicolons (#59797) --- src/services/formatting/rules.ts | 7 +++ .../fourslash/formatRemoveSemicolons1.ts | 4 +- .../fourslash/formatRemoveSemicolons4.ts | 48 +++++++++++++++++++ 3 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 tests/cases/fourslash/formatRemoveSemicolons4.ts diff --git a/src/services/formatting/rules.ts b/src/services/formatting/rules.ts index 806dc6003f5..45d80841d18 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 b80ecee7edb..39735fe0a8d 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 00000000000..299efa837a7 --- /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]`);