Skip to content

Commit e5c7e67

Browse files
author
Andy Hanson
committed
goToDefinition: Don't add duplicate definitions for VariableDeclaration and ArrowFunction at f = () => {}
1 parent c437404 commit e5c7e67

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/services/goToDefinition.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ namespace ts.GoToDefinition {
3232
const sigInfo = createDefinitionFromSignatureDeclaration(typeChecker, calledDeclaration);
3333
// For a function, if this is the original function definition, return just sigInfo.
3434
// If this is the original constructor definition, parent is the class.
35-
if (typeChecker.getRootSymbols(symbol).some(s => calledDeclaration.symbol === s || calledDeclaration.symbol.parent === s) ||
35+
if (typeChecker.getRootSymbols(symbol).some(s => symbolMatchesSignature(s, calledDeclaration)) ||
3636
// TODO: GH#23742 Following check shouldn't be necessary if 'require' is an alias
3737
symbol.declarations.some(d => isVariableDeclaration(d) && !!d.initializer && isRequireCall(d.initializer, /*checkArgumentIsStringLiteralLike*/ false))) {
3838
return [sigInfo];
@@ -93,6 +93,15 @@ namespace ts.GoToDefinition {
9393
return getDefinitionFromSymbol(typeChecker, symbol, node);
9494
}
9595

96+
/**
97+
* True if we should not add definitions for both the signature symbol and the definition symbol.
98+
* True for `const |f = |() => 0`, false for `function |f() {} const |g = f;`.
99+
*/
100+
function symbolMatchesSignature(s: Symbol, calledDeclaration: SignatureDeclaration) {
101+
return s === calledDeclaration.symbol || s === calledDeclaration.symbol.parent ||
102+
isVariableDeclaration(calledDeclaration.parent) && s === calledDeclaration.parent.symbol
103+
}
104+
96105
export function getReferenceAtPosition(sourceFile: SourceFile, position: number, program: Program): { fileName: string, file: SourceFile } | undefined {
97106
const referencePath = findReferenceInPosition(sourceFile.referencedFiles, position);
98107
if (referencePath) {

tests/cases/fourslash/goToDefinitionSignatureAlias.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,17 @@
88
////[|/*useG*/g|]();
99
////[|/*useH*/h|]();
1010

11+
////const i = /*i*/() => 0;
12+
////const /*j*/j = i;
13+
14+
////[|/*useI*/i|]();
15+
////[|/*useJ*/j|]();
16+
1117
verify.goToDefinition({
1218
useF: "f",
1319
useG: ["g", "f"],
1420
useH: ["h", "f"],
21+
22+
useI: "i",
23+
useJ: ["j", "i"],
1524
});

0 commit comments

Comments
 (0)