Skip to content

Commit

Permalink
fix(46563): show completions at this type (#46581)
Browse files Browse the repository at this point in the history
  • Loading branch information
a-tarasyuk committed Dec 3, 2021
1 parent c5d9200 commit 407edc9
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 5 deletions.
5 changes: 1 addition & 4 deletions src/compiler/checker.ts
Expand Up @@ -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,
Expand Down Expand Up @@ -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) :
Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/utilities.ts
Expand Up @@ -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);
}
}
13 changes: 13 additions & 0 deletions src/services/services.ts
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion src/services/stringCompletions.ts
Expand Up @@ -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);
});
Expand Down
2 changes: 2 additions & 0 deletions src/services/types.ts
Expand Up @@ -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 {
Expand All @@ -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[];
Expand Down
2 changes: 2 additions & 0 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Expand Up @@ -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[];
Expand All @@ -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[];
Expand Down
2 changes: 2 additions & 0 deletions tests/baselines/reference/api/typescript.d.ts
Expand Up @@ -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[];
Expand All @@ -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[];
Expand Down
19 changes: 19 additions & 0 deletions tests/cases/fourslash/completionListAtThisType.ts
@@ -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"]
});

0 comments on commit 407edc9

Please sign in to comment.