Skip to content

Commit 96b0832

Browse files
author
Vincent Boivin
authored
fix(40322): bad completions for string literal type as indexed access type object (microsoft#40424)
* Added fourslash tests to work on the bug * fix(40322): bad completions for string literal type as indexed access type object * Added regression tests * Using nodes instead of text * Add verification for parenthesized nodes * Using range check * Change test markers
1 parent ee5f51b commit 96b0832

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

src/services/stringCompletions.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,11 @@ namespace ts.Completions.StringCompletions {
130130
// bar: string;
131131
// }
132132
// let x: Foo["/*completion position*/"]
133-
return stringLiteralCompletionsFromProperties(typeChecker.getTypeFromTypeNode((grandParent as IndexedAccessTypeNode).objectType));
133+
const { indexType, objectType } = grandParent as IndexedAccessTypeNode;
134+
if (!rangeContainsPosition(indexType, position)) {
135+
return undefined;
136+
}
137+
return stringLiteralCompletionsFromProperties(typeChecker.getTypeFromTypeNode(objectType));
134138
case SyntaxKind.ImportType:
135139
return { kind: StringLiteralCompletionKind.Paths, paths: getStringLiteralCompletionsFromModuleNames(sourceFile, node, compilerOptions, host, typeChecker) };
136140
case SyntaxKind.UnionType: {
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
//// let firstCase: "a/*case_1*/"["foo"]
4+
//// let secondCase: "b/*case_2*/"["bar"]
5+
//// let thirdCase: "c/*case_3*/"["baz"]
6+
//// let fourthCase: "en/*case_4*/"["qux"]
7+
//// interface Foo {
8+
//// bar: string;
9+
//// qux: string;
10+
//// }
11+
//// let fifthCase: Foo["b/*case_5*/"]
12+
//// let sixthCase: Foo["qu/*case_6*/"]
13+
14+
// fourslash tests for issue 40322
15+
verify.completions({
16+
marker: ["case_1", "case_2", "case_3", "case_4"],
17+
exact: undefined,
18+
isNewIdentifierLocation: true,
19+
});
20+
21+
// regression tests
22+
const test5 = test.marker("case_5");
23+
const test6 = test.marker("case_6");
24+
25+
verify.completions({
26+
marker: "case_5",
27+
includes: {
28+
name: "bar",
29+
replacementSpan: {
30+
fileName: test5.fileName,
31+
pos: test5.position - 1,
32+
end: test5.position,
33+
},
34+
},
35+
});
36+
37+
verify.completions({
38+
marker: "case_6",
39+
includes: {
40+
name: "qux",
41+
replacementSpan: {
42+
fileName: test6.fileName,
43+
pos: test6.position - 2,
44+
end: test6.position,
45+
},
46+
},
47+
});

0 commit comments

Comments
 (0)