Skip to content

Commit 601c113

Browse files
author
Andy
authored
Handle indexed access types in getSymbolAtLocation and findAllReferences (#18149)
* Handle indexed access types in getSymbolAtLocation and findAllReferences * Update baselines, simplify `const objectType`
1 parent 562abf3 commit 601c113

File tree

7 files changed

+59
-33
lines changed

7 files changed

+59
-33
lines changed

src/compiler/checker.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22929,7 +22929,7 @@ namespace ts {
2292922929
return undefined;
2293022930
}
2293122931

22932-
function getSymbolAtLocation(node: Node) {
22932+
function getSymbolAtLocation(node: Node): Symbol | undefined {
2293322933
if (node.kind === SyntaxKind.SourceFile) {
2293422934
return isExternalModule(<SourceFile>node) ? getMergedSymbol(node.symbol) : undefined;
2293522935
}
@@ -23007,13 +23007,16 @@ namespace ts {
2300723007

2300823008
case SyntaxKind.NumericLiteral:
2300923009
// index access
23010-
if (node.parent.kind === SyntaxKind.ElementAccessExpression && (<ElementAccessExpression>node.parent).argumentExpression === node) {
23011-
const objectType = getTypeOfExpression((<ElementAccessExpression>node.parent).expression);
23012-
return getPropertyOfType(objectType, (<NumericLiteral>node).text as __String);
23013-
}
23014-
break;
23010+
const objectType = isElementAccessExpression(node.parent)
23011+
? node.parent.argumentExpression === node ? getTypeOfExpression(node.parent.expression) : undefined
23012+
: isLiteralTypeNode(node.parent) && isIndexedAccessTypeNode(node.parent.parent)
23013+
? getTypeFromTypeNode(node.parent.parent.objectType)
23014+
: undefined;
23015+
return objectType && getPropertyOfType(objectType, escapeLeadingUnderscores((node as StringLiteral | NumericLiteral).text));
23016+
23017+
default:
23018+
return undefined;
2301523019
}
23016-
return undefined;
2301723020
}
2301823021

2301923022
function getShorthandAssignmentValueSymbol(location: Node): Symbol {

src/services/findAllReferences.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -748,11 +748,11 @@ namespace ts.FindAllReferences.Core {
748748
return (node as Identifier).text.length === searchSymbolName.length;
749749

750750
case SyntaxKind.StringLiteral:
751-
return (isLiteralNameOfPropertyDeclarationOrIndexAccess(node) || isNameOfExternalModuleImportOrDeclaration(node)) &&
751+
return (isLiteralNameOfPropertyDeclarationOrIndexAccess(node as StringLiteral) || isNameOfExternalModuleImportOrDeclaration(node)) &&
752752
(node as StringLiteral).text.length === searchSymbolName.length;
753753

754754
case SyntaxKind.NumericLiteral:
755-
return isLiteralNameOfPropertyDeclarationOrIndexAccess(node) && (node as NumericLiteral).text.length === searchSymbolName.length;
755+
return isLiteralNameOfPropertyDeclarationOrIndexAccess(node as NumericLiteral) && (node as NumericLiteral).text.length === searchSymbolName.length;
756756

757757
default:
758758
return false;

src/services/rename.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,15 @@ namespace ts.Rename {
8989
}
9090

9191
function nodeIsEligibleForRename(node: Node): boolean {
92-
return node.kind === ts.SyntaxKind.Identifier ||
93-
node.kind === SyntaxKind.StringLiteral ||
94-
isLiteralNameOfPropertyDeclarationOrIndexAccess(node) ||
95-
isThis(node);
92+
switch (node.kind) {
93+
case SyntaxKind.Identifier:
94+
case SyntaxKind.StringLiteral:
95+
case SyntaxKind.ThisKeyword:
96+
return true;
97+
case SyntaxKind.NumericLiteral:
98+
return isLiteralNameOfPropertyDeclarationOrIndexAccess(node as NumericLiteral);
99+
default:
100+
return false;
101+
}
96102
}
97103
}

src/services/utilities.ts

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -244,27 +244,25 @@ namespace ts {
244244
isFunctionLike(node.parent) && (<FunctionLikeDeclaration>node.parent).name === node;
245245
}
246246

247-
export function isLiteralNameOfPropertyDeclarationOrIndexAccess(node: Node): boolean {
248-
if (node.kind === SyntaxKind.StringLiteral || node.kind === SyntaxKind.NumericLiteral) {
249-
switch (node.parent.kind) {
250-
case SyntaxKind.PropertyDeclaration:
251-
case SyntaxKind.PropertySignature:
252-
case SyntaxKind.PropertyAssignment:
253-
case SyntaxKind.EnumMember:
254-
case SyntaxKind.MethodDeclaration:
255-
case SyntaxKind.MethodSignature:
256-
case SyntaxKind.GetAccessor:
257-
case SyntaxKind.SetAccessor:
258-
case SyntaxKind.ModuleDeclaration:
259-
return getNameOfDeclaration(<Declaration>node.parent) === node;
260-
case SyntaxKind.ElementAccessExpression:
261-
return (<ElementAccessExpression>node.parent).argumentExpression === node;
262-
case SyntaxKind.ComputedPropertyName:
263-
return true;
264-
}
247+
export function isLiteralNameOfPropertyDeclarationOrIndexAccess(node: StringLiteral | NumericLiteral): boolean {
248+
switch (node.parent.kind) {
249+
case SyntaxKind.PropertyDeclaration:
250+
case SyntaxKind.PropertySignature:
251+
case SyntaxKind.PropertyAssignment:
252+
case SyntaxKind.EnumMember:
253+
case SyntaxKind.MethodDeclaration:
254+
case SyntaxKind.MethodSignature:
255+
case SyntaxKind.GetAccessor:
256+
case SyntaxKind.SetAccessor:
257+
case SyntaxKind.ModuleDeclaration:
258+
return getNameOfDeclaration(<Declaration>node.parent) === node;
259+
case SyntaxKind.ElementAccessExpression:
260+
return (<ElementAccessExpression>node.parent).argumentExpression === node;
261+
case SyntaxKind.ComputedPropertyName:
262+
return true;
263+
case SyntaxKind.LiteralType:
264+
return node.parent.parent.kind === SyntaxKind.IndexedAccessType;
265265
}
266-
267-
return false;
268266
}
269267

270268
export function isExpressionOfExternalModuleImportEqualsDeclaration(node: Node) {

tests/baselines/reference/escapedReservedCompilerNamedIdentifier.symbols

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var o = {
1111
var b = o["__proto__"];
1212
>b : Symbol(b, Decl(escapedReservedCompilerNamedIdentifier.ts, 5, 3))
1313
>o : Symbol(o, Decl(escapedReservedCompilerNamedIdentifier.ts, 2, 3))
14+
>"__proto__" : Symbol("__proto__", Decl(escapedReservedCompilerNamedIdentifier.ts, 2, 9))
1415

1516
var o1 = {
1617
>o1 : Symbol(o1, Decl(escapedReservedCompilerNamedIdentifier.ts, 6, 3))
@@ -22,6 +23,7 @@ var o1 = {
2223
var b1 = o1["__proto__"];
2324
>b1 : Symbol(b1, Decl(escapedReservedCompilerNamedIdentifier.ts, 9, 3))
2425
>o1 : Symbol(o1, Decl(escapedReservedCompilerNamedIdentifier.ts, 6, 3))
26+
>"__proto__" : Symbol(__proto__, Decl(escapedReservedCompilerNamedIdentifier.ts, 6, 10))
2527

2628
// Triple underscores
2729
var ___proto__ = 10;
@@ -35,6 +37,7 @@ var o2 = {
3537
var b2 = o2["___proto__"];
3638
>b2 : Symbol(b2, Decl(escapedReservedCompilerNamedIdentifier.ts, 15, 3))
3739
>o2 : Symbol(o2, Decl(escapedReservedCompilerNamedIdentifier.ts, 12, 3))
40+
>"___proto__" : Symbol("___proto__", Decl(escapedReservedCompilerNamedIdentifier.ts, 12, 10))
3841

3942
var o3 = {
4043
>o3 : Symbol(o3, Decl(escapedReservedCompilerNamedIdentifier.ts, 16, 3))
@@ -46,6 +49,7 @@ var o3 = {
4649
var b3 = o3["___proto__"];
4750
>b3 : Symbol(b3, Decl(escapedReservedCompilerNamedIdentifier.ts, 19, 3))
4851
>o3 : Symbol(o3, Decl(escapedReservedCompilerNamedIdentifier.ts, 16, 3))
52+
>"___proto__" : Symbol(___proto__, Decl(escapedReservedCompilerNamedIdentifier.ts, 16, 10))
4953

5054
// One underscore
5155
var _proto__ = 10;

tests/baselines/reference/underscoreEscapedNameInEnum.symbols

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ enum E {
66
bar = E["__foo"] + 1
77
>bar : Symbol(E.bar, Decl(underscoreEscapedNameInEnum.ts, 1, 16))
88
>E : Symbol(E, Decl(underscoreEscapedNameInEnum.ts, 0, 0))
9+
>"__foo" : Symbol(E["__foo"], Decl(underscoreEscapedNameInEnum.ts, 0, 8))
910
}
1011

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////interface I {
4+
//// [|{| "isDefinition": true, "isWriteAccess": true |}0|]: number;
5+
//// [|{| "isDefinition": true, "isWriteAccess": true |}s|]: string;
6+
////}
7+
////interface J {
8+
//// a: I[[|0|]],
9+
//// b: I["[|s|]"],
10+
////}
11+
12+
const [n0, s0, n1, s1] = test.ranges();
13+
verify.singleReferenceGroup("(property) I[0]: number", [n0, n1]);
14+
verify.singleReferenceGroup("(property) I.s: string", [s0, s1]);

0 commit comments

Comments
 (0)