Skip to content

Commit a62ac67

Browse files
authored
fix(59779): The semicolons: "remove" formatting option doesn't remove extraneous semicolons (#59797)
1 parent 241a6c9 commit a62ac67

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

src/services/formatting/rules.ts

+7
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,13 @@ function isSemicolonDeletionContext(context: FormattingContext): boolean {
933933
|| nextTokenKind === SyntaxKind.EndOfFileToken;
934934
}
935935

936+
if (
937+
nextTokenKind === SyntaxKind.SemicolonToken &&
938+
context.currentTokenSpan.kind === SyntaxKind.SemicolonToken
939+
) {
940+
return true;
941+
}
942+
936943
if (
937944
nextTokenKind === SyntaxKind.SemicolonClassElement ||
938945
nextTokenKind === SyntaxKind.SemicolonToken

tests/cases/fourslash/formatRemoveSemicolons1.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@ class C {
5151
["p"]
5252
zero: void
5353
["one"] = {};
54-
["two"];
54+
["two"]
5555
;
5656
}
5757
a;
5858
\`b\`
5959
b;
6060
(3)
6161
4;
62-
/ regex /;
62+
/ regex /
6363
;
6464
[];
6565
/** blah */[0]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
////declare const opt: number | undefined;
4+
////
5+
////const a = 1;
6+
////const b = 2;
7+
////;[1, 2, 3]
8+
////
9+
////const c = opt ? 1 : 2;
10+
////const d = opt ? 1 : 2;
11+
////;[1, 2, 3]
12+
////
13+
////const e = opt ?? 1;
14+
////const f = opt ?? 1;
15+
////;[1, 2, 3]
16+
////
17+
////type a = 1;
18+
////type b = 2;
19+
////;[1, 2, 3]
20+
////
21+
////type c = typeof opt extends 1 ? 1 : 2;
22+
////type d = typeof opt extends 1 ? 1 : 2;
23+
////;[1, 2, 3]
24+
25+
format.setFormatOptions({ ...format.copyFormatOptions(), semicolons: ts.SemicolonPreference.Remove });
26+
format.document();
27+
verify.currentFileContentIs(
28+
`declare const opt: number | undefined
29+
30+
const a = 1
31+
const b = 2
32+
;[1, 2, 3]
33+
34+
const c = opt ? 1 : 2
35+
const d = opt ? 1 : 2
36+
;[1, 2, 3]
37+
38+
const e = opt ?? 1
39+
const f = opt ?? 1
40+
;[1, 2, 3]
41+
42+
type a = 1
43+
type b = 2
44+
;[1, 2, 3]
45+
46+
type c = typeof opt extends 1 ? 1 : 2
47+
type d = typeof opt extends 1 ? 1 : 2
48+
;[1, 2, 3]`);

0 commit comments

Comments
 (0)