Skip to content

Commit 8094007

Browse files
authored
fix(52277): switch/case completions fail when has a negative literal type (#52278)
1 parent 16c695c commit 8094007

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

src/services/completions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,10 +1050,10 @@ function getExhaustiveCaseSnippets(
10501050
else if (!tracker.hasValue(type.value)) {
10511051
switch (typeof type.value) {
10521052
case "object":
1053-
elements.push(factory.createBigIntLiteral(type.value));
1053+
elements.push(type.value.negative ? factory.createPrefixUnaryExpression(SyntaxKind.MinusToken, factory.createBigIntLiteral({ negative: false, base10Value: type.value.base10Value })) : factory.createBigIntLiteral(type.value));
10541054
break;
10551055
case "number":
1056-
elements.push(factory.createNumericLiteral(type.value));
1056+
elements.push(type.value < 0 ? factory.createPrefixUnaryExpression(SyntaxKind.MinusToken, factory.createNumericLiteral(-type.value)) : factory.createNumericLiteral(type.value));
10571057
break;
10581058
case "string":
10591059
elements.push(factory.createStringLiteral(type.value, quotePreference === QuotePreference.Single));
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @newline: LF
4+
////export function foo(position: -1 | 0 | 1) {
5+
//// switch (position) {
6+
//// /**/
7+
//// }
8+
////}
9+
10+
verify.completions(
11+
{
12+
marker: "",
13+
isNewIdentifierLocation: false,
14+
includes: [
15+
{
16+
name: "case 0: ...",
17+
source: completion.CompletionSource.SwitchCases,
18+
sortText: completion.SortText.GlobalsOrKeywords,
19+
insertText:
20+
`case 0:
21+
case 1:
22+
case -1:`,
23+
},
24+
],
25+
preferences: {
26+
includeCompletionsWithInsertText: true,
27+
},
28+
},
29+
);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @newline: LF
4+
////export function foo(position: -1n | 0n) {
5+
//// switch (position) {
6+
//// /**/
7+
//// }
8+
////}
9+
10+
verify.completions(
11+
{
12+
marker: "",
13+
isNewIdentifierLocation: false,
14+
includes: [
15+
{
16+
name: "case 0n: ...",
17+
source: completion.CompletionSource.SwitchCases,
18+
sortText: completion.SortText.GlobalsOrKeywords,
19+
insertText:
20+
`case 0n:
21+
case -1n:`,
22+
},
23+
],
24+
preferences: {
25+
includeCompletionsWithInsertText: true,
26+
},
27+
},
28+
);

0 commit comments

Comments
 (0)