From 8094007a1dfa03443e69bfa760739912c7233488 Mon Sep 17 00:00:00 2001 From: Oleksandr T Date: Wed, 18 Jan 2023 00:10:29 +0200 Subject: [PATCH] fix(52277): switch/case completions fail when has a negative literal type (#52278) --- src/services/completions.ts | 4 +-- .../fourslash/exhaustiveCaseCompletions7.ts | 29 +++++++++++++++++++ .../fourslash/exhaustiveCaseCompletions8.ts | 28 ++++++++++++++++++ 3 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 tests/cases/fourslash/exhaustiveCaseCompletions7.ts create mode 100644 tests/cases/fourslash/exhaustiveCaseCompletions8.ts diff --git a/src/services/completions.ts b/src/services/completions.ts index 59166fe91ecf1..c2f8bb72aff06 100644 --- a/src/services/completions.ts +++ b/src/services/completions.ts @@ -1050,10 +1050,10 @@ function getExhaustiveCaseSnippets( else if (!tracker.hasValue(type.value)) { switch (typeof type.value) { case "object": - elements.push(factory.createBigIntLiteral(type.value)); + elements.push(type.value.negative ? factory.createPrefixUnaryExpression(SyntaxKind.MinusToken, factory.createBigIntLiteral({ negative: false, base10Value: type.value.base10Value })) : factory.createBigIntLiteral(type.value)); break; case "number": - elements.push(factory.createNumericLiteral(type.value)); + elements.push(type.value < 0 ? factory.createPrefixUnaryExpression(SyntaxKind.MinusToken, factory.createNumericLiteral(-type.value)) : factory.createNumericLiteral(type.value)); break; case "string": elements.push(factory.createStringLiteral(type.value, quotePreference === QuotePreference.Single)); diff --git a/tests/cases/fourslash/exhaustiveCaseCompletions7.ts b/tests/cases/fourslash/exhaustiveCaseCompletions7.ts new file mode 100644 index 0000000000000..5038caf94f1f1 --- /dev/null +++ b/tests/cases/fourslash/exhaustiveCaseCompletions7.ts @@ -0,0 +1,29 @@ +/// + +// @newline: LF +////export function foo(position: -1 | 0 | 1) { +//// switch (position) { +//// /**/ +//// } +////} + +verify.completions( + { + marker: "", + isNewIdentifierLocation: false, + includes: [ + { + name: "case 0: ...", + source: completion.CompletionSource.SwitchCases, + sortText: completion.SortText.GlobalsOrKeywords, + insertText: +`case 0: +case 1: +case -1:`, + }, + ], + preferences: { + includeCompletionsWithInsertText: true, + }, + }, +); diff --git a/tests/cases/fourslash/exhaustiveCaseCompletions8.ts b/tests/cases/fourslash/exhaustiveCaseCompletions8.ts new file mode 100644 index 0000000000000..28da2b9417b23 --- /dev/null +++ b/tests/cases/fourslash/exhaustiveCaseCompletions8.ts @@ -0,0 +1,28 @@ +/// + +// @newline: LF +////export function foo(position: -1n | 0n) { +//// switch (position) { +//// /**/ +//// } +////} + +verify.completions( + { + marker: "", + isNewIdentifierLocation: false, + includes: [ + { + name: "case 0n: ...", + source: completion.CompletionSource.SwitchCases, + sortText: completion.SortText.GlobalsOrKeywords, + insertText: +`case 0n: +case -1n:`, + }, + ], + preferences: { + includeCompletionsWithInsertText: true, + }, + }, +);