Skip to content

Commit db8755a

Browse files
author
Andy Hanson
committed
Handle indexed access types in getSymbolAtLocation and findAllReferences
1 parent 80a7ed9 commit db8755a

File tree

5 files changed

+60
-33
lines changed

5 files changed

+60
-33
lines changed

src/compiler/checker.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22830,7 +22830,7 @@ namespace ts {
2283022830
return undefined;
2283122831
}
2283222832

22833-
function getSymbolAtLocation(node: Node) {
22833+
function getSymbolAtLocation(node: Node): Symbol | undefined {
2283422834
if (node.kind === SyntaxKind.SourceFile) {
2283522835
return isExternalModule(<SourceFile>node) ? getMergedSymbol(node.symbol) : undefined;
2283622836
}
@@ -22908,12 +22908,21 @@ namespace ts {
2290822908

2290922909
case SyntaxKind.NumericLiteral:
2291022910
// index access
22911-
if (node.parent.kind === SyntaxKind.ElementAccessExpression && (<ElementAccessExpression>node.parent).argumentExpression === node) {
22912-
const objectType = getTypeOfExpression((<ElementAccessExpression>node.parent).expression);
22913-
if (objectType === unknownType) return undefined;
22914-
const apparentType = getApparentType(objectType);
22915-
if (apparentType === unknownType) return undefined;
22916-
return getPropertyOfType(apparentType, (<NumericLiteral>node).text as __String);
22911+
switch (node.parent.kind) {
22912+
case SyntaxKind.ElementAccessExpression: {
22913+
if ((<ElementAccessExpression>node.parent).argumentExpression !== node) return undefined;
22914+
const objectType = getTypeOfExpression((<ElementAccessExpression>node.parent).expression);
22915+
if (objectType === unknownType) return undefined;
22916+
const apparentType = getApparentType(objectType);
22917+
if (apparentType === unknownType) return undefined;
22918+
return getPropertyOfType(apparentType, (<NumericLiteral>node).text as __String);
22919+
}
22920+
case SyntaxKind.LiteralType: {
22921+
if (!isIndexedAccessTypeNode(node.parent.parent)) return undefined;
22922+
const objectType = getApparentType(getTypeFromTypeNode(node.parent.parent.objectType));
22923+
if (objectType === unknownType) return undefined;
22924+
return getPropertyOfType(objectType, escapeLeadingUnderscores((node as StringLiteral | NumericLiteral).text));
22925+
}
2291722926
}
2291822927
break;
2291922928
}

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) {
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)