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:
parent
562abf333a
commit
601c113d93
|
@ -22929,7 +22929,7 @@ namespace ts {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getSymbolAtLocation(node: Node) {
|
function getSymbolAtLocation(node: Node): Symbol | undefined {
|
||||||
if (node.kind === SyntaxKind.SourceFile) {
|
if (node.kind === SyntaxKind.SourceFile) {
|
||||||
return isExternalModule(<SourceFile>node) ? getMergedSymbol(node.symbol) : undefined;
|
return isExternalModule(<SourceFile>node) ? getMergedSymbol(node.symbol) : undefined;
|
||||||
}
|
}
|
||||||
|
@ -23007,14 +23007,17 @@ namespace ts {
|
||||||
|
|
||||||
case SyntaxKind.NumericLiteral:
|
case SyntaxKind.NumericLiteral:
|
||||||
// index access
|
// index access
|
||||||
if (node.parent.kind === SyntaxKind.ElementAccessExpression && (<ElementAccessExpression>node.parent).argumentExpression === node) {
|
const objectType = isElementAccessExpression(node.parent)
|
||||||
const objectType = getTypeOfExpression((<ElementAccessExpression>node.parent).expression);
|
? node.parent.argumentExpression === node ? getTypeOfExpression(node.parent.expression) : undefined
|
||||||
return getPropertyOfType(objectType, (<NumericLiteral>node).text as __String);
|
: isLiteralTypeNode(node.parent) && isIndexedAccessTypeNode(node.parent.parent)
|
||||||
}
|
? getTypeFromTypeNode(node.parent.parent.objectType)
|
||||||
break;
|
: undefined;
|
||||||
}
|
return objectType && getPropertyOfType(objectType, escapeLeadingUnderscores((node as StringLiteral | NumericLiteral).text));
|
||||||
|
|
||||||
|
default:
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function getShorthandAssignmentValueSymbol(location: Node): Symbol {
|
function getShorthandAssignmentValueSymbol(location: Node): Symbol {
|
||||||
// The function returns a value symbol of an identifier in the short-hand property assignment.
|
// The function returns a value symbol of an identifier in the short-hand property assignment.
|
||||||
|
|
|
@ -748,11 +748,11 @@ namespace ts.FindAllReferences.Core {
|
||||||
return (node as Identifier).text.length === searchSymbolName.length;
|
return (node as Identifier).text.length === searchSymbolName.length;
|
||||||
|
|
||||||
case SyntaxKind.StringLiteral:
|
case SyntaxKind.StringLiteral:
|
||||||
return (isLiteralNameOfPropertyDeclarationOrIndexAccess(node) || isNameOfExternalModuleImportOrDeclaration(node)) &&
|
return (isLiteralNameOfPropertyDeclarationOrIndexAccess(node as StringLiteral) || isNameOfExternalModuleImportOrDeclaration(node)) &&
|
||||||
(node as StringLiteral).text.length === searchSymbolName.length;
|
(node as StringLiteral).text.length === searchSymbolName.length;
|
||||||
|
|
||||||
case SyntaxKind.NumericLiteral:
|
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:
|
default:
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -89,9 +89,15 @@ namespace ts.Rename {
|
||||||
}
|
}
|
||||||
|
|
||||||
function nodeIsEligibleForRename(node: Node): boolean {
|
function nodeIsEligibleForRename(node: Node): boolean {
|
||||||
return node.kind === ts.SyntaxKind.Identifier ||
|
switch (node.kind) {
|
||||||
node.kind === SyntaxKind.StringLiteral ||
|
case SyntaxKind.Identifier:
|
||||||
isLiteralNameOfPropertyDeclarationOrIndexAccess(node) ||
|
case SyntaxKind.StringLiteral:
|
||||||
isThis(node);
|
case SyntaxKind.ThisKeyword:
|
||||||
|
return true;
|
||||||
|
case SyntaxKind.NumericLiteral:
|
||||||
|
return isLiteralNameOfPropertyDeclarationOrIndexAccess(node as NumericLiteral);
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -244,8 +244,7 @@ namespace ts {
|
||||||
isFunctionLike(node.parent) && (<FunctionLikeDeclaration>node.parent).name === node;
|
isFunctionLike(node.parent) && (<FunctionLikeDeclaration>node.parent).name === node;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isLiteralNameOfPropertyDeclarationOrIndexAccess(node: Node): boolean {
|
export function isLiteralNameOfPropertyDeclarationOrIndexAccess(node: StringLiteral | NumericLiteral): boolean {
|
||||||
if (node.kind === SyntaxKind.StringLiteral || node.kind === SyntaxKind.NumericLiteral) {
|
|
||||||
switch (node.parent.kind) {
|
switch (node.parent.kind) {
|
||||||
case SyntaxKind.PropertyDeclaration:
|
case SyntaxKind.PropertyDeclaration:
|
||||||
case SyntaxKind.PropertySignature:
|
case SyntaxKind.PropertySignature:
|
||||||
|
@ -261,12 +260,11 @@ namespace ts {
|
||||||
return (<ElementAccessExpression>node.parent).argumentExpression === node;
|
return (<ElementAccessExpression>node.parent).argumentExpression === node;
|
||||||
case SyntaxKind.ComputedPropertyName:
|
case SyntaxKind.ComputedPropertyName:
|
||||||
return true;
|
return true;
|
||||||
|
case SyntaxKind.LiteralType:
|
||||||
|
return node.parent.parent.kind === SyntaxKind.IndexedAccessType;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function isExpressionOfExternalModuleImportEqualsDeclaration(node: Node) {
|
export function isExpressionOfExternalModuleImportEqualsDeclaration(node: Node) {
|
||||||
return isExternalModuleImportEqualsDeclaration(node.parent.parent) &&
|
return isExternalModuleImportEqualsDeclaration(node.parent.parent) &&
|
||||||
getExternalModuleImportEqualsDeclarationExpression(node.parent.parent) === node;
|
getExternalModuleImportEqualsDeclarationExpression(node.parent.parent) === node;
|
||||||
|
|
|
@ -11,6 +11,7 @@ var o = {
|
||||||
var b = o["__proto__"];
|
var b = o["__proto__"];
|
||||||
>b : Symbol(b, Decl(escapedReservedCompilerNamedIdentifier.ts, 5, 3))
|
>b : Symbol(b, Decl(escapedReservedCompilerNamedIdentifier.ts, 5, 3))
|
||||||
>o : Symbol(o, Decl(escapedReservedCompilerNamedIdentifier.ts, 2, 3))
|
>o : Symbol(o, Decl(escapedReservedCompilerNamedIdentifier.ts, 2, 3))
|
||||||
|
>"__proto__" : Symbol("__proto__", Decl(escapedReservedCompilerNamedIdentifier.ts, 2, 9))
|
||||||
|
|
||||||
var o1 = {
|
var o1 = {
|
||||||
>o1 : Symbol(o1, Decl(escapedReservedCompilerNamedIdentifier.ts, 6, 3))
|
>o1 : Symbol(o1, Decl(escapedReservedCompilerNamedIdentifier.ts, 6, 3))
|
||||||
|
@ -22,6 +23,7 @@ var o1 = {
|
||||||
var b1 = o1["__proto__"];
|
var b1 = o1["__proto__"];
|
||||||
>b1 : Symbol(b1, Decl(escapedReservedCompilerNamedIdentifier.ts, 9, 3))
|
>b1 : Symbol(b1, Decl(escapedReservedCompilerNamedIdentifier.ts, 9, 3))
|
||||||
>o1 : Symbol(o1, Decl(escapedReservedCompilerNamedIdentifier.ts, 6, 3))
|
>o1 : Symbol(o1, Decl(escapedReservedCompilerNamedIdentifier.ts, 6, 3))
|
||||||
|
>"__proto__" : Symbol(__proto__, Decl(escapedReservedCompilerNamedIdentifier.ts, 6, 10))
|
||||||
|
|
||||||
// Triple underscores
|
// Triple underscores
|
||||||
var ___proto__ = 10;
|
var ___proto__ = 10;
|
||||||
|
@ -35,6 +37,7 @@ var o2 = {
|
||||||
var b2 = o2["___proto__"];
|
var b2 = o2["___proto__"];
|
||||||
>b2 : Symbol(b2, Decl(escapedReservedCompilerNamedIdentifier.ts, 15, 3))
|
>b2 : Symbol(b2, Decl(escapedReservedCompilerNamedIdentifier.ts, 15, 3))
|
||||||
>o2 : Symbol(o2, Decl(escapedReservedCompilerNamedIdentifier.ts, 12, 3))
|
>o2 : Symbol(o2, Decl(escapedReservedCompilerNamedIdentifier.ts, 12, 3))
|
||||||
|
>"___proto__" : Symbol("___proto__", Decl(escapedReservedCompilerNamedIdentifier.ts, 12, 10))
|
||||||
|
|
||||||
var o3 = {
|
var o3 = {
|
||||||
>o3 : Symbol(o3, Decl(escapedReservedCompilerNamedIdentifier.ts, 16, 3))
|
>o3 : Symbol(o3, Decl(escapedReservedCompilerNamedIdentifier.ts, 16, 3))
|
||||||
|
@ -46,6 +49,7 @@ var o3 = {
|
||||||
var b3 = o3["___proto__"];
|
var b3 = o3["___proto__"];
|
||||||
>b3 : Symbol(b3, Decl(escapedReservedCompilerNamedIdentifier.ts, 19, 3))
|
>b3 : Symbol(b3, Decl(escapedReservedCompilerNamedIdentifier.ts, 19, 3))
|
||||||
>o3 : Symbol(o3, Decl(escapedReservedCompilerNamedIdentifier.ts, 16, 3))
|
>o3 : Symbol(o3, Decl(escapedReservedCompilerNamedIdentifier.ts, 16, 3))
|
||||||
|
>"___proto__" : Symbol(___proto__, Decl(escapedReservedCompilerNamedIdentifier.ts, 16, 10))
|
||||||
|
|
||||||
// One underscore
|
// One underscore
|
||||||
var _proto__ = 10;
|
var _proto__ = 10;
|
||||||
|
|
|
@ -6,5 +6,6 @@ enum E {
|
||||||
bar = E["__foo"] + 1
|
bar = E["__foo"] + 1
|
||||||
>bar : Symbol(E.bar, Decl(underscoreEscapedNameInEnum.ts, 1, 16))
|
>bar : Symbol(E.bar, Decl(underscoreEscapedNameInEnum.ts, 1, 16))
|
||||||
>E : Symbol(E, Decl(underscoreEscapedNameInEnum.ts, 0, 0))
|
>E : Symbol(E, Decl(underscoreEscapedNameInEnum.ts, 0, 0))
|
||||||
|
>"__foo" : Symbol(E["__foo"], Decl(underscoreEscapedNameInEnum.ts, 0, 8))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
14
tests/cases/fourslash/findAllRefsIndexedAccessTypes.ts
Normal file
14
tests/cases/fourslash/findAllRefsIndexedAccessTypes.ts
Normal 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]);
|
Loading…
Reference in a new issue