Improve contextual completions (#53554)

This commit is contained in:
Mateusz Burzyński
2023-04-20 18:22:14 +02:00
committed by GitHub
parent 4849947357
commit 40787a7076
7 changed files with 46 additions and 1 deletions

View File

@@ -2938,7 +2938,7 @@ function getContextualType(previousToken: Node, position: number, sourceFile: So
isEqualityOperatorKind(previousToken.kind) && isBinaryExpression(parent) && isEqualityOperatorKind(parent.operatorToken.kind) ?
// completion at `x ===/**/` should be for the right side
checker.getTypeAtLocation(parent.left) :
checker.getContextualType(previousToken as Expression);
checker.getContextualType(previousToken as Expression, ContextFlags.Completions) || checker.getContextualType(previousToken as Expression);
}
}

View File

@@ -0,0 +1,8 @@
/// <reference path="fourslash.ts" />
//// declare function test<P extends "a" | "b">(p: P): void;
////
//// test(/*ts*/)
////
verify.completions({ marker: ["ts"], includes: ['"a"', '"b"'], isNewIdentifierLocation: true });

View File

@@ -0,0 +1,7 @@
/// <reference path="fourslash.ts" />
//// declare function test<T extends 'a' | 'b'>(a: { foo: T[] }): void
////
//// test({ foo: [/*ts*/] })
verify.completions({ marker: ["ts"], includes: ['"a"', '"b"'], isNewIdentifierLocation: true });

View File

@@ -0,0 +1,7 @@
/// <reference path="fourslash.ts" />
//// declare function test<T extends 'a' | 'b'>(a: { foo: T[] }): void
////
//// test({ foo: ['a', /*ts*/] })
verify.completions({ marker: ["ts"], includes: ['"a"', '"b"'], isNewIdentifierLocation: true });

View File

@@ -0,0 +1,7 @@
/// <reference path="fourslash.ts" />
//// declare function test<P extends "a" | "b">(p: { type: P }): void;
////
//// test({ type: /*ts*/ })
verify.completions({ marker: ["ts"], includes: ['"a"', '"b"'], isNewIdentifierLocation: false });

View File

@@ -13,5 +13,13 @@
//// b: "/*ts*/",
//// },
//// });
////
//// test({
//// foo: {},
//// bar: {
//// b: /*ts2*/,
//// },
//// });
verify.completions({ marker: ["ts"], exact: ["foo", "bar"] });
verify.completions({ marker: ["ts2"], includes: ['"foo"', '"bar"'], isNewIdentifierLocation: false });

View File

@@ -12,5 +12,13 @@
//// b: ["/*ts*/"],
//// },
//// });
////
//// test({
//// foo: {},
//// bar: {
//// b: [/*ts2*/],
//// },
//// });
verify.completions({ marker: ["ts"], exact: ["foo", "bar"] });
verify.completions({ marker: ["ts2"], includes: ['"foo"', '"bar"'], isNewIdentifierLocation: true });