Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/services/goToDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace ts.GoToDefinition {
const sigInfo = createDefinitionFromSignatureDeclaration(typeChecker, calledDeclaration);
// For a function, if this is the original function definition, return just sigInfo.
// If this is the original constructor definition, parent is the class.
if (typeChecker.getRootSymbols(symbol).some(s => calledDeclaration.symbol === s || calledDeclaration.symbol.parent === s) ||
if (typeChecker.getRootSymbols(symbol).some(s => symbolMatchesSignature(s, calledDeclaration)) ||
// TODO: GH#23742 Following check shouldn't be necessary if 'require' is an alias
symbol.declarations.some(d => isVariableDeclaration(d) && !!d.initializer && isRequireCall(d.initializer, /*checkArgumentIsStringLiteralLike*/ false))) {
return [sigInfo];
Expand Down Expand Up @@ -93,6 +93,15 @@ namespace ts.GoToDefinition {
return getDefinitionFromSymbol(typeChecker, symbol, node);
}

/**
* True if we should not add definitions for both the signature symbol and the definition symbol.
* True for `const |f = |() => 0`, false for `function |f() {} const |g = f;`.
*/
function symbolMatchesSignature(s: Symbol, calledDeclaration: SignatureDeclaration) {
return s === calledDeclaration.symbol || s === calledDeclaration.symbol.parent ||
isVariableDeclaration(calledDeclaration.parent) && s === calledDeclaration.parent.symbol;
}

export function getReferenceAtPosition(sourceFile: SourceFile, position: number, program: Program): { fileName: string, file: SourceFile } | undefined {
const referencePath = findReferenceInPosition(sourceFile.referencedFiles, position);
if (referencePath) {
Expand Down
9 changes: 9 additions & 0 deletions tests/cases/fourslash/goToDefinitionSignatureAlias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,17 @@
////[|/*useG*/g|]();
////[|/*useH*/h|]();

////const i = /*i*/() => 0;
////const /*j*/j = i;

////[|/*useI*/i|]();
////[|/*useJ*/j|]();

verify.goToDefinition({
useF: "f",
useG: ["g", "f"],
useH: ["h", "f"],

useI: "i",
useJ: ["j", "i"],
});