fix(46563): show completions at this type (#46581)

This commit is contained in:
Oleksandr T 2021-12-04 00:29:45 +02:00 committed by GitHub
parent c5d9200ec6
commit 407edc95c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 45 additions and 5 deletions

View File

@ -432,6 +432,7 @@ namespace ts {
getIndexInfosOfType,
getSignaturesOfType,
getIndexTypeOfType: (type, kind) => getIndexTypeOfType(type, kind === IndexKind.String ? stringType : numberType),
getIndexType: type => getIndexType(type),
getBaseTypes,
getBaseTypeOfLiteralType,
getWidenedType,
@ -15344,10 +15345,6 @@ namespace ts {
(type.flags & (TypeFlags.InstantiableNonPrimitive | TypeFlags.Index | TypeFlags.TemplateLiteral | TypeFlags.StringMapping) && !isPatternLiteralType(type) ? ObjectFlags.IsGenericIndexType : 0);
}
function isThisTypeParameter(type: Type): boolean {
return !!(type.flags & TypeFlags.TypeParameter && (type as TypeParameter).isThisType);
}
function getSimplifiedType(type: Type, writing: boolean): Type {
return type.flags & TypeFlags.IndexedAccess ? getSimplifiedIndexedAccessType(type as IndexedAccessType, writing) :
type.flags & TypeFlags.Conditional ? getSimplifiedConditionalType(type as ConditionalType, writing) :

View File

@ -4162,6 +4162,7 @@ namespace ts {
getIndexInfosOfType(type: Type): readonly IndexInfo[];
getSignaturesOfType(type: Type, kind: SignatureKind): readonly Signature[];
getIndexTypeOfType(type: Type, kind: IndexKind): Type | undefined;
/* @internal */ getIndexType(type: Type): Type;
getBaseTypes(type: InterfaceType): BaseType[];
getBaseTypeOfLiteralType(type: Type): Type;
getWidenedType(type: Type): Type;

View File

@ -7414,4 +7414,8 @@ namespace ts {
export function escapeSnippetText(text: string): string {
return text.replace(/\$/gm, "\\$");
}
export function isThisTypeParameter(type: Type): boolean {
return !!(type.flags & TypeFlags.TypeParameter && (type as TypeParameter).isThisType);
}
}

View File

@ -500,6 +500,9 @@ namespace ts {
isClass(): this is InterfaceType {
return !!(getObjectFlags(this) & ObjectFlags.Class);
}
isIndexType(): this is IndexType {
return !!(this.flags & TypeFlags.Index);
}
/**
* This polyfills `referenceType.typeArguments` for API consumers
*/
@ -545,6 +548,16 @@ namespace ts {
getReturnType(): Type {
return this.checker.getReturnTypeOfSignature(this);
}
getTypeParameterAtPosition(pos: number): Type {
const type = this.checker.getParameterType(this, pos);
if (type.isIndexType() && isThisTypeParameter(type.type)) {
const constraint = type.type.getConstraint();
if (constraint) {
return this.checker.getIndexType(constraint);
}
}
return type;
}
getDocumentationComment(): SymbolDisplayPart[] {
return this.documentationComment || (this.documentationComment = getDocumentationComment(singleElementArray(this.declaration), this.checker));

View File

@ -264,7 +264,7 @@ namespace ts.Completions.StringCompletions {
checker.getResolvedSignature(argumentInfo.invocation, candidates, argumentInfo.argumentCount);
const types = flatMap(candidates, candidate => {
if (!signatureHasRestParameter(candidate) && argumentInfo.argumentCount > candidate.parameters.length) return;
const type = checker.getParameterType(candidate, argumentInfo.argumentIndex);
const type = candidate.getTypeParameterAtPosition(argumentInfo.argumentIndex);
isNewIdentifier = isNewIdentifier || !!(type.flags & TypeFlags.String);
return getStringLiteralTypes(type, uniques);
});

View File

@ -72,6 +72,7 @@ namespace ts {
isTypeParameter(): this is TypeParameter;
isClassOrInterface(): this is InterfaceType;
isClass(): this is InterfaceType;
isIndexType(): this is IndexType;
}
export interface TypeReference {
@ -82,6 +83,7 @@ namespace ts {
getDeclaration(): SignatureDeclaration;
getTypeParameters(): TypeParameter[] | undefined;
getParameters(): Symbol[];
getTypeParameterAtPosition(pos: number): Type;
getReturnType(): Type;
getDocumentationComment(typeChecker: TypeChecker | undefined): SymbolDisplayPart[];
getJsDocTags(): JSDocTagInfo[];

View File

@ -5560,6 +5560,7 @@ declare namespace ts {
isTypeParameter(): this is TypeParameter;
isClassOrInterface(): this is InterfaceType;
isClass(): this is InterfaceType;
isIndexType(): this is IndexType;
}
interface TypeReference {
typeArguments?: readonly Type[];
@ -5568,6 +5569,7 @@ declare namespace ts {
getDeclaration(): SignatureDeclaration;
getTypeParameters(): TypeParameter[] | undefined;
getParameters(): Symbol[];
getTypeParameterAtPosition(pos: number): Type;
getReturnType(): Type;
getDocumentationComment(typeChecker: TypeChecker | undefined): SymbolDisplayPart[];
getJsDocTags(): JSDocTagInfo[];

View File

@ -5560,6 +5560,7 @@ declare namespace ts {
isTypeParameter(): this is TypeParameter;
isClassOrInterface(): this is InterfaceType;
isClass(): this is InterfaceType;
isIndexType(): this is IndexType;
}
interface TypeReference {
typeArguments?: readonly Type[];
@ -5568,6 +5569,7 @@ declare namespace ts {
getDeclaration(): SignatureDeclaration;
getTypeParameters(): TypeParameter[] | undefined;
getParameters(): Symbol[];
getTypeParameterAtPosition(pos: number): Type;
getReturnType(): Type;
getDocumentationComment(typeChecker: TypeChecker | undefined): SymbolDisplayPart[];
getJsDocTags(): JSDocTagInfo[];

View File

@ -0,0 +1,19 @@
/// <reference path='fourslash.ts'/>
////class Test {
//// foo() {}
////
//// bar() {
//// this.baz(this, "/*1*/");
////
//// const t = new Test()
//// this.baz(t, "/*2*/");
//// }
////
//// baz<T>(a: T, k: keyof T) {}
////}
verify.completions({
marker: ["1", "2"],
exact: ["foo", "bar", "baz"]
});