Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(59779): The semicolons: "remove" formatting option doesn't remove extraneous semicolons #59797

Merged
merged 5 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/services/formatting/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/cases/fourslash/formatRemoveSemicolons1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ class C {
["p"]
zero: void
["one"] = {};
["two"];
["two"]
;
}
a;
\`b\`
b;
(3)
4;
/ regex /;
/ regex /
;
[];
/** blah */[0]
Expand Down
48 changes: 48 additions & 0 deletions tests/cases/fourslash/formatRemoveSemicolons4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/// <reference path="fourslash.ts" />

////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]`);