Handle indexed access types in getSymbolAtLocation and findAllReferences (#18149)

* Handle indexed access types in getSymbolAtLocation and findAllReferences

* Update baselines, simplify `const objectType`
This commit is contained in:
Andy 2017-08-30 14:19:49 -07:00 committed by GitHub
parent 562abf333a
commit 601c113d93
7 changed files with 59 additions and 33 deletions

View file

@ -22929,7 +22929,7 @@ namespace ts {
return undefined;
}
function getSymbolAtLocation(node: Node) {
function getSymbolAtLocation(node: Node): Symbol | undefined {
if (node.kind === SyntaxKind.SourceFile) {
return isExternalModule(<SourceFile>node) ? getMergedSymbol(node.symbol) : undefined;
}
@ -23007,13 +23007,16 @@ namespace ts {
case SyntaxKind.NumericLiteral:
// index access
if (node.parent.kind === SyntaxKind.ElementAccessExpression && (<ElementAccessExpression>node.parent).argumentExpression === node) {
const objectType = getTypeOfExpression((<ElementAccessExpression>node.parent).expression);
return getPropertyOfType(objectType, (<NumericLiteral>node).text as __String);
}
break;
const objectType = isElementAccessExpression(node.parent)
? node.parent.argumentExpression === node ? getTypeOfExpression(node.parent.expression) : undefined
: isLiteralTypeNode(node.parent) && isIndexedAccessTypeNode(node.parent.parent)
? getTypeFromTypeNode(node.parent.parent.objectType)
: undefined;
return objectType && getPropertyOfType(objectType, escapeLeadingUnderscores((node as StringLiteral | NumericLiteral).text));
default:
return undefined;
}
return undefined;
}
function getShorthandAssignmentValueSymbol(location: Node): Symbol {

View file

@ -748,11 +748,11 @@ namespace ts.FindAllReferences.Core {
return (node as Identifier).text.length === searchSymbolName.length;
case SyntaxKind.StringLiteral:
return (isLiteralNameOfPropertyDeclarationOrIndexAccess(node) || isNameOfExternalModuleImportOrDeclaration(node)) &&
return (isLiteralNameOfPropertyDeclarationOrIndexAccess(node as StringLiteral) || isNameOfExternalModuleImportOrDeclaration(node)) &&
(node as StringLiteral).text.length === searchSymbolName.length;
case SyntaxKind.NumericLiteral:
return isLiteralNameOfPropertyDeclarationOrIndexAccess(node) && (node as NumericLiteral).text.length === searchSymbolName.length;
return isLiteralNameOfPropertyDeclarationOrIndexAccess(node as NumericLiteral) && (node as NumericLiteral).text.length === searchSymbolName.length;
default:
return false;

View file

@ -89,9 +89,15 @@ namespace ts.Rename {
}
function nodeIsEligibleForRename(node: Node): boolean {
return node.kind === ts.SyntaxKind.Identifier ||
node.kind === SyntaxKind.StringLiteral ||
isLiteralNameOfPropertyDeclarationOrIndexAccess(node) ||
isThis(node);
switch (node.kind) {
case SyntaxKind.Identifier:
case SyntaxKind.StringLiteral:
case SyntaxKind.ThisKeyword:
return true;
case SyntaxKind.NumericLiteral:
return isLiteralNameOfPropertyDeclarationOrIndexAccess(node as NumericLiteral);
default:
return false;
}
}
}

View file

@ -244,27 +244,25 @@ namespace ts {
isFunctionLike(node.parent) && (<FunctionLikeDeclaration>node.parent).name === node;
}
export function isLiteralNameOfPropertyDeclarationOrIndexAccess(node: Node): boolean {
if (node.kind === SyntaxKind.StringLiteral || node.kind === SyntaxKind.NumericLiteral) {
switch (node.parent.kind) {
case SyntaxKind.PropertyDeclaration:
case SyntaxKind.PropertySignature:
case SyntaxKind.PropertyAssignment:
case SyntaxKind.EnumMember:
case SyntaxKind.MethodDeclaration:
case SyntaxKind.MethodSignature:
case SyntaxKind.GetAccessor:
case SyntaxKind.SetAccessor:
case SyntaxKind.ModuleDeclaration:
return getNameOfDeclaration(<Declaration>node.parent) === node;
case SyntaxKind.ElementAccessExpression:
return (<ElementAccessExpression>node.parent).argumentExpression === node;
case SyntaxKind.ComputedPropertyName:
return true;
}
export function isLiteralNameOfPropertyDeclarationOrIndexAccess(node: StringLiteral | NumericLiteral): boolean {
switch (node.parent.kind) {
case SyntaxKind.PropertyDeclaration:
case SyntaxKind.PropertySignature:
case SyntaxKind.PropertyAssignment:
case SyntaxKind.EnumMember:
case SyntaxKind.MethodDeclaration:
case SyntaxKind.MethodSignature:
case SyntaxKind.GetAccessor:
case SyntaxKind.SetAccessor:
case SyntaxKind.ModuleDeclaration:
return getNameOfDeclaration(<Declaration>node.parent) === node;
case SyntaxKind.ElementAccessExpression:
return (<ElementAccessExpression>node.parent).argumentExpression === node;
case SyntaxKind.ComputedPropertyName:
return true;
case SyntaxKind.LiteralType:
return node.parent.parent.kind === SyntaxKind.IndexedAccessType;
}
return false;
}
export function isExpressionOfExternalModuleImportEqualsDeclaration(node: Node) {

View file

@ -11,6 +11,7 @@ var o = {
var b = o["__proto__"];
>b : Symbol(b, Decl(escapedReservedCompilerNamedIdentifier.ts, 5, 3))
>o : Symbol(o, Decl(escapedReservedCompilerNamedIdentifier.ts, 2, 3))
>"__proto__" : Symbol("__proto__", Decl(escapedReservedCompilerNamedIdentifier.ts, 2, 9))
var o1 = {
>o1 : Symbol(o1, Decl(escapedReservedCompilerNamedIdentifier.ts, 6, 3))
@ -22,6 +23,7 @@ var o1 = {
var b1 = o1["__proto__"];
>b1 : Symbol(b1, Decl(escapedReservedCompilerNamedIdentifier.ts, 9, 3))
>o1 : Symbol(o1, Decl(escapedReservedCompilerNamedIdentifier.ts, 6, 3))
>"__proto__" : Symbol(__proto__, Decl(escapedReservedCompilerNamedIdentifier.ts, 6, 10))
// Triple underscores
var ___proto__ = 10;
@ -35,6 +37,7 @@ var o2 = {
var b2 = o2["___proto__"];
>b2 : Symbol(b2, Decl(escapedReservedCompilerNamedIdentifier.ts, 15, 3))
>o2 : Symbol(o2, Decl(escapedReservedCompilerNamedIdentifier.ts, 12, 3))
>"___proto__" : Symbol("___proto__", Decl(escapedReservedCompilerNamedIdentifier.ts, 12, 10))
var o3 = {
>o3 : Symbol(o3, Decl(escapedReservedCompilerNamedIdentifier.ts, 16, 3))
@ -46,6 +49,7 @@ var o3 = {
var b3 = o3["___proto__"];
>b3 : Symbol(b3, Decl(escapedReservedCompilerNamedIdentifier.ts, 19, 3))
>o3 : Symbol(o3, Decl(escapedReservedCompilerNamedIdentifier.ts, 16, 3))
>"___proto__" : Symbol(___proto__, Decl(escapedReservedCompilerNamedIdentifier.ts, 16, 10))
// One underscore
var _proto__ = 10;

View file

@ -6,5 +6,6 @@ enum E {
bar = E["__foo"] + 1
>bar : Symbol(E.bar, Decl(underscoreEscapedNameInEnum.ts, 1, 16))
>E : Symbol(E, Decl(underscoreEscapedNameInEnum.ts, 0, 0))
>"__foo" : Symbol(E["__foo"], Decl(underscoreEscapedNameInEnum.ts, 0, 8))
}

View file

@ -0,0 +1,14 @@
/// <reference path='fourslash.ts' />
////interface I {
//// [|{| "isDefinition": true, "isWriteAccess": true |}0|]: number;
//// [|{| "isDefinition": true, "isWriteAccess": true |}s|]: string;
////}
////interface J {
//// a: I[[|0|]],
//// b: I["[|s|]"],
////}
const [n0, s0, n1, s1] = test.ranges();
verify.singleReferenceGroup("(property) I[0]: number", [n0, n1]);
verify.singleReferenceGroup("(property) I.s: string", [s0, s1]);