fix(40322): bad completions for string literal type as indexed access type object (#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
This commit is contained in:
Vincent Boivin 2020-09-09 18:26:35 -04:00 committed by GitHub
parent ee5f51bc0f
commit 96b0832cf6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 1 deletions

View File

@ -130,7 +130,11 @@ namespace ts.Completions.StringCompletions {
// bar: string;
// }
// let x: Foo["/*completion position*/"]
return stringLiteralCompletionsFromProperties(typeChecker.getTypeFromTypeNode((grandParent as IndexedAccessTypeNode).objectType));
const { indexType, objectType } = grandParent as IndexedAccessTypeNode;
if (!rangeContainsPosition(indexType, position)) {
return undefined;
}
return stringLiteralCompletionsFromProperties(typeChecker.getTypeFromTypeNode(objectType));
case SyntaxKind.ImportType:
return { kind: StringLiteralCompletionKind.Paths, paths: getStringLiteralCompletionsFromModuleNames(sourceFile, node, compilerOptions, host, typeChecker) };
case SyntaxKind.UnionType: {

View File

@ -0,0 +1,47 @@
/// <reference path="fourslash.ts" />
//// let firstCase: "a/*case_1*/"["foo"]
//// let secondCase: "b/*case_2*/"["bar"]
//// let thirdCase: "c/*case_3*/"["baz"]
//// let fourthCase: "en/*case_4*/"["qux"]
//// interface Foo {
//// bar: string;
//// qux: string;
//// }
//// let fifthCase: Foo["b/*case_5*/"]
//// let sixthCase: Foo["qu/*case_6*/"]
// fourslash tests for issue 40322
verify.completions({
marker: ["case_1", "case_2", "case_3", "case_4"],
exact: undefined,
isNewIdentifierLocation: true,
});
// regression tests
const test5 = test.marker("case_5");
const test6 = test.marker("case_6");
verify.completions({
marker: "case_5",
includes: {
name: "bar",
replacementSpan: {
fileName: test5.fileName,
pos: test5.position - 1,
end: test5.position,
},
},
});
verify.completions({
marker: "case_6",
includes: {
name: "qux",
replacementSpan: {
fileName: test6.fileName,
pos: test6.position - 2,
end: test6.position,
},
},
});