Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't add completion for method if the this parameter doesn't match #20616

Merged
1 commit merged into from
Dec 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,11 @@ namespace ts {
},
isValidPropertyAccess: (node, propertyName) => {
node = getParseTreeNode(node, isPropertyAccessOrQualifiedName);
return node ? isValidPropertyAccess(node, escapeLeadingUnderscores(propertyName)) : false;
return !!node && isValidPropertyAccess(node, escapeLeadingUnderscores(propertyName));
},
isValidPropertyAccessForCompletions: (node, type, property) => {
node = getParseTreeNode(node, isPropertyAccessExpression);
return !!node && isValidPropertyAccessForCompletions(node, type, property);
},
getSignatureFromDeclaration: declaration => {
declaration = getParseTreeNode(declaration, isFunctionLike);
Expand Down Expand Up @@ -16006,13 +16010,24 @@ namespace ts {
}

function isValidPropertyAccess(node: PropertyAccessExpression | QualifiedName, propertyName: __String): boolean {
const left = node.kind === SyntaxKind.PropertyAccessExpression
? (<PropertyAccessExpression>node).expression
: (<QualifiedName>node).left;

const left = node.kind === SyntaxKind.PropertyAccessExpression ? node.expression : node.left;
return isValidPropertyAccessWithType(node, left, propertyName, getWidenedType(checkExpression(left)));
}

function isValidPropertyAccessForCompletions(node: PropertyAccessExpression, type: Type, property: Symbol): boolean {
return isValidPropertyAccessWithType(node, node.expression, property.escapedName, type)
&& (!(property.flags & ts.SymbolFlags.Method) || isValidMethodAccess(property, type));
}
function isValidMethodAccess(method: Symbol, type: Type) {
const propType = getTypeOfFuncClassEnumModule(method);
const signatures = getSignaturesOfType(propType, SignatureKind.Call);
Debug.assert(signatures.length !== 0);
return signatures.some(sig => {
const thisType = getThisTypeOfSignature(sig);
return !thisType || isTypeAssignableTo(type, thisType);
});
}

function isValidPropertyAccessWithType(
node: PropertyAccessExpression | QualifiedName,
left: LeftHandSideExpression | QualifiedName,
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2753,6 +2753,8 @@ namespace ts {

getConstantValue(node: EnumMember | PropertyAccessExpression | ElementAccessExpression): string | number | undefined;
isValidPropertyAccess(node: PropertyAccessExpression | QualifiedName, propertyName: string): boolean;
/** Exclude accesses to private properties or methods with a `this` parameter that `type` doesn't satisfy. */
/* @internal */ isValidPropertyAccessForCompletions(node: PropertyAccessExpression, type: Type, property: Symbol): boolean;
/** Follow all aliases to get the original symbol. */
getAliasedSymbol(symbol: Symbol): Symbol;
/** Follow a *single* alias to get the immediately aliased symbol. */
Expand Down
6 changes: 2 additions & 4 deletions src/services/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -918,9 +918,8 @@ namespace ts.Completions {
symbols.push(...getPropertiesForCompletion(type, typeChecker, /*isForAccess*/ true));
}
else {
// Filter private properties
for (const symbol of type.getApparentProperties()) {
if (typeChecker.isValidPropertyAccess(<PropertyAccessExpression>(node.parent), symbol.name)) {
if (typeChecker.isValidPropertyAccessForCompletions(<PropertyAccessExpression>(node.parent), type, symbol)) {
symbols.push(symbol);
}
}
Expand Down Expand Up @@ -2119,8 +2118,7 @@ namespace ts.Completions {

/**
* Gets all properties on a type, but if that type is a union of several types,
* tries to only include those types which declare properties, not methods.
* This ensures that we don't try providing completions for all the methods on e.g. Array.
* excludes array-like types or callable/constructable types.
*/
function getPropertiesForCompletion(type: Type, checker: TypeChecker, isForAccess: boolean): Symbol[] {
if (!(type.flags & TypeFlags.Union)) {
Expand Down
15 changes: 15 additions & 0 deletions tests/cases/fourslash/completionsMethodWithThisParameter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/// <reference path='fourslash.ts'/>

////class A<T> {
//// value: T; // Make the type parameter actually matter
//// ms(this: A<string>) {}
//// mo(this: A<{}>) {}
////}
////
////const s = new A<string>();
////const n = new A<number>();
////s./*s*/;
////n./*n*/;

verify.completionsAt("s", ["value", "ms", "mo"]);
verify.completionsAt("n", ["value", "mo"]);