Skip to content

Commit 0c17a2b

Browse files
author
Andy
authored
goToDefinition: Reduce duplicate code around createDefinitionInfo (#23473)
* goToDefinition: Reduce duplicate code around createDefinitionInfo * Use '[]' instead of 'emptyArray' * Remove comment
1 parent db68075 commit 0c17a2b

File tree

1 file changed

+12
-32
lines changed

1 file changed

+12
-32
lines changed

src/services/goToDefinition.ts

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,7 @@ namespace ts.GoToDefinition {
5050
// assignment. This case and others are handled by the following code.
5151
if (node.parent.kind === SyntaxKind.ShorthandPropertyAssignment) {
5252
const shorthandSymbol = typeChecker.getShorthandAssignmentValueSymbol(symbol.valueDeclaration);
53-
if (!shorthandSymbol) {
54-
return [];
55-
}
56-
57-
const shorthandDeclarations = shorthandSymbol.getDeclarations();
58-
const shorthandSymbolKind = SymbolDisplay.getSymbolKind(typeChecker, shorthandSymbol, node);
59-
const shorthandSymbolName = typeChecker.symbolToString(shorthandSymbol);
60-
const shorthandContainerName = typeChecker.symbolToString(symbol.parent, node);
61-
return map(shorthandDeclarations,
62-
declaration => createDefinitionInfo(declaration, shorthandSymbolKind, shorthandSymbolName, shorthandContainerName));
53+
return shorthandSymbol ? shorthandSymbol.declarations.map(decl => createDefinitionInfo(decl, typeChecker, shorthandSymbol, node)) : [];
6354
}
6455

6556
// If the node is the name of a BindingElement within an ObjectBindingPattern instead of just returning the
@@ -191,8 +182,7 @@ namespace ts.GoToDefinition {
191182
}
192183

193184
function getDefinitionFromSymbol(typeChecker: TypeChecker, symbol: Symbol, node: Node): DefinitionInfo[] {
194-
const { symbolName, symbolKind, containerName } = getSymbolInfo(typeChecker, symbol, node);
195-
return getConstructSignatureDefinition() || getCallSignatureDefinition() || map(symbol.declarations, declaration => createDefinitionInfo(declaration, symbolKind, symbolName, containerName));
185+
return getConstructSignatureDefinition() || getCallSignatureDefinition() || map(symbol.declarations, declaration => createDefinitionInfo(declaration, typeChecker, symbol, node));
196186

197187
function getConstructSignatureDefinition(): DefinitionInfo[] | undefined {
198188
// Applicable only if we are in a new expression, or we are on a constructor declaration
@@ -215,7 +205,7 @@ namespace ts.GoToDefinition {
215205
}
216206
const declarations = signatureDeclarations.filter(selectConstructors ? isConstructorDeclaration : isSignatureDeclaration);
217207
return declarations.length
218-
? [createDefinitionInfo(find(declarations, d => !!(<FunctionLikeDeclaration>d).body) || last(declarations), symbolKind, symbolName, containerName)]
208+
? [createDefinitionInfo(find(declarations, d => !!(<FunctionLikeDeclaration>d).body) || last(declarations), typeChecker, symbol, node)]
219209
: undefined;
220210
}
221211
}
@@ -234,12 +224,16 @@ namespace ts.GoToDefinition {
234224
}
235225

236226
/** Creates a DefinitionInfo from a Declaration, using the declaration's name if possible. */
237-
function createDefinitionInfo(node: Declaration, symbolKind: ScriptElementKind, symbolName: string, containerName: string): DefinitionInfo {
238-
return createDefinitionInfoFromName(getNameOfDeclaration(node) || node, symbolKind, symbolName, containerName);
227+
function createDefinitionInfo(declaration: Declaration, checker: TypeChecker, symbol: Symbol, node: Node): DefinitionInfo {
228+
const symbolName = checker.symbolToString(symbol); // Do not get scoped name, just the name of the symbol
229+
const symbolKind = SymbolDisplay.getSymbolKind(checker, symbol, node);
230+
const containerName = symbol.parent ? checker.symbolToString(symbol.parent, node) : "";
231+
return createDefinitionInfoFromName(declaration, symbolKind, symbolName, containerName);
239232
}
240233

241234
/** Creates a DefinitionInfo directly from the name of a declaration. */
242-
function createDefinitionInfoFromName(name: Node, symbolKind: ScriptElementKind, symbolName: string, containerName: string): DefinitionInfo {
235+
function createDefinitionInfoFromName(declaration: Declaration, symbolKind: ScriptElementKind, symbolName: string, containerName: string): DefinitionInfo {
236+
const name = getNameOfDeclaration(declaration) || declaration;
243237
const sourceFile = name.getSourceFile();
244238
return {
245239
fileName: sourceFile.fileName,
@@ -251,26 +245,12 @@ namespace ts.GoToDefinition {
251245
};
252246
}
253247

254-
function getSymbolInfo(typeChecker: TypeChecker, symbol: Symbol, node: Node) {
255-
return {
256-
symbolName: typeChecker.symbolToString(symbol), // Do not get scoped name, just the name of the symbol
257-
symbolKind: SymbolDisplay.getSymbolKind(typeChecker, symbol, node),
258-
containerName: symbol.parent ? typeChecker.symbolToString(symbol.parent, node) : ""
259-
};
260-
}
261-
262248
function createDefinitionFromSignatureDeclaration(typeChecker: TypeChecker, decl: SignatureDeclaration): DefinitionInfo {
263-
const { symbolName, symbolKind, containerName } = getSymbolInfo(typeChecker, decl.symbol, decl);
264-
return createDefinitionInfo(decl, symbolKind, symbolName, containerName);
249+
return createDefinitionInfo(decl, typeChecker, decl.symbol, decl);
265250
}
266251

267252
export function findReferenceInPosition(refs: ReadonlyArray<FileReference>, pos: number): FileReference | undefined {
268-
for (const ref of refs) {
269-
if (ref.pos <= pos && pos <= ref.end) {
270-
return ref;
271-
}
272-
}
273-
return undefined;
253+
return find(refs, ref => ref.pos <= pos && pos <= ref.end);
274254
}
275255

276256
function getDefinitionInfoForFileReference(name: string, targetFileName: string): DefinitionInfo {

0 commit comments

Comments
 (0)