Skip to content

Commit 0e357f5

Browse files
authored
Exclude literal completions after closing quote and JSX attribute location (#52676)
1 parent a037407 commit 0e357f5

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

src/services/completions.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2927,7 +2927,10 @@ function getCompletionData(
29272927
log("getCompletionData: Semantic work: " + (timestamp() - semanticStart));
29282928
const contextualType = previousToken && getContextualType(previousToken, position, sourceFile, typeChecker);
29292929

2930-
const literals = mapDefined(
2930+
// exclude literal suggestions after <input type="text" [||] /> (#51667) and after closing quote (#52675)
2931+
// for strings getStringLiteralCompletions handles completions
2932+
const isLiteralExpected = !tryCast(previousToken, isStringLiteralLike) && !isJsxIdentifierExpected;
2933+
const literals = !isLiteralExpected ? [] : mapDefined(
29312934
contextualType && (contextualType.isUnion() ? contextualType.types : [contextualType]),
29322935
t => t.isLiteral() && !(t.flags & TypeFlags.EnumLiteral) ? t.value : undefined);
29332936

tests/cases/fourslash/completionsLiterals.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
////const x: 0 | "one" = /**/;
44
////const y: 0 | "one" | 1n = /*1*/;
5+
////const y2: 0 | "one" | 1n = 'one'/*2*/;
56

67
verify.completions({
78
marker: "",
@@ -20,3 +21,9 @@ verify.completions({
2021
],
2122
isNewIdentifierLocation: true,
2223
});
24+
verify.completions({
25+
marker: "2",
26+
excludes: [
27+
'"one"'
28+
],
29+
});

tests/cases/fourslash/stringLiteralCompletionsInJsxAttributeInitializer.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22

33
// @jsx: preserve
44
// @filename: /a.tsx
5-
////type Props = { a: number } | { b: "somethingelse" };
5+
////type Props = { a: number } | { b: "somethingelse", c: 0 | 1 };
66
////declare function Foo(args: Props): any
77
////
88
////const a1 = <Foo b={"/*1*/"} />
99
////const a2 = <Foo b="/*2*/" />
10+
////const a3 = <Foo b="somethingelse"/*3*/ />
11+
////const a4 = <Foo b={"somethingelse"} /*4*/ />
12+
////const a5 = <Foo b={"somethingelse"} c={0} /*5*/ />
1013

1114
verify.completions({ marker: ["1", "2"], exact: ["somethingelse"] });
15+
verify.completions({ marker: ["3", "4"], excludes: ['"somethingelse"'], });
16+
verify.completions({ marker: ["5"], excludes: ["0", "1"], });

0 commit comments

Comments
 (0)