From 993e5cadaf7747947f6aa9199d90f6f03749e67b Mon Sep 17 00:00:00 2001 From: Oleksandr T Date: Thu, 29 Aug 2024 22:59:41 +0300 Subject: [PATCH 1/2] fix(59779): eliminate redundant semicolons before existing semicolons --- 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 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]`); From b7bddd8e3062428aa51c1d4c9c2c3c58a1ab25f4 Mon Sep 17 00:00:00 2001 From: Oleksandr T Date: Thu, 29 Aug 2024 23:36:07 +0300 Subject: [PATCH 2/2] fix formatting --- src/compiler/checker.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 5d4be5660aabe..614a958d618d4 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -22056,7 +22056,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { const [sourceType, targetType] = getTypeNamesForErrorDisplay(source, target); let generalizedSource = source; let generalizedSourceType = sourceType; - + // Don't generalize on 'never' - we really want the original type // to be displayed for use-cases like 'assertNever'. if (!(target.flags & TypeFlags.Never) && isLiteralType(source) && !typeCouldHaveTopLevelSingletonTypes(target)) {