mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-18 07:29:16 -05:00
Support goto-definition for index signatures (#23220)
* Support goto-definition for index signatures * Use checker.getIndexInfoOfType * Handle undefined info.declaration
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
/* @internal */
|
||||
namespace ts.GoToDefinition {
|
||||
export function getDefinitionAtPosition(program: Program, sourceFile: SourceFile, position: number): DefinitionInfo[] {
|
||||
export function getDefinitionAtPosition(program: Program, sourceFile: SourceFile, position: number): DefinitionInfo[] | undefined {
|
||||
const reference = getReferenceAtPosition(sourceFile, position, program);
|
||||
if (reference) {
|
||||
return [getDefinitionInfoForFileReference(reference.fileName, reference.file.fileName)];
|
||||
@@ -29,7 +29,7 @@ namespace ts.GoToDefinition {
|
||||
// Could not find a symbol e.g. node is string or number keyword,
|
||||
// or the symbol was an internal symbol and does not have a declaration e.g. undefined symbol
|
||||
if (!symbol) {
|
||||
return undefined;
|
||||
return getDefinitionInfoForIndexSignatures(node, typeChecker);
|
||||
}
|
||||
|
||||
// If this is an alias, and the request came at the declaration location
|
||||
@@ -157,6 +157,16 @@ namespace ts.GoToDefinition {
|
||||
return { definitions, textSpan };
|
||||
}
|
||||
|
||||
// At 'x.foo', see if the type of 'x' has an index signature, and if so find its declarations.
|
||||
function getDefinitionInfoForIndexSignatures(node: Node, checker: TypeChecker): DefinitionInfo[] | undefined {
|
||||
if (!isPropertyAccessExpression(node.parent) || node.parent.name !== node) return;
|
||||
const type = checker.getTypeAtLocation(node.parent.expression);
|
||||
return mapDefined(type.isUnionOrIntersection() ? type.types : [type], nonUnionType => {
|
||||
const info = checker.getIndexInfoOfType(nonUnionType, IndexKind.String);
|
||||
return info && info.declaration && createDefinitionFromSignatureDeclaration(checker, info.declaration);
|
||||
});
|
||||
}
|
||||
|
||||
// Go to the original declaration for cases:
|
||||
//
|
||||
// (1) when the aliased symbol was declared in the location(parent).
|
||||
|
||||
Reference in New Issue
Block a user