-
Notifications
You must be signed in to change notification settings - Fork 13k
Handle indexed access types in getSymbolAtLocation and findAllReferences #18149
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,20 @@ 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 = (() => { | ||
switch (node.parent.kind) { | ||
case SyntaxKind.ElementAccessExpression: | ||
const { argumentExpression, expression } = node.parent as ElementAccessExpression; | ||
return argumentExpression === node ? getTypeOfExpression(expression) : undefined; | ||
case SyntaxKind.LiteralType: | ||
return isIndexedAccessTypeNode(node.parent.parent) ? getTypeFromTypeNode(node.parent.parent.objectType) : undefined; | ||
} | ||
})(); | ||
return objectType && getPropertyOfType(objectType, escapeLeadingUnderscores((node as StringLiteral | NumericLiteral).text)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you construct a test case where the lack of escapeLeadingUnderscores makes a string literal element access fail? It would be something like x["__foo"], right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Turns out I needed to update a few baselines related to this anyway. |
||
|
||
default: | ||
return undefined; | ||
} | ||
return undefined; | ||
} | ||
|
||
function getShorthandAssignmentValueSymbol(location: Node): Symbol { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the IIFE seems like overkill for what could be
let objectType; switch ...
with some assignments toobjectType
inside. Or does it introduce some names that conflict with the outside scope?