Skip to content

Commit

Permalink
Merge pull request #33124 from armanio123/FixGotoMultipleFile
Browse files Browse the repository at this point in the history
Fixed goto when global declarations are on multiple files
  • Loading branch information
armanio123 committed Aug 29, 2019
2 parents fa9e0fa + 09a5b68 commit 029f7a3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
16 changes: 11 additions & 5 deletions src/services/goToDefinition.ts
Expand Up @@ -39,7 +39,7 @@ namespace ts.GoToDefinition {
return [sigInfo];
}
else {
const defs = getDefinitionFromSymbol(typeChecker, symbol, node) || emptyArray;
const defs = getDefinitionFromSymbol(typeChecker, symbol, node, calledDeclaration) || emptyArray;
// For a 'super()' call, put the signature first, else put the variable first.
return node.kind === SyntaxKind.SuperKeyword ? [sigInfo, ...defs] : [...defs, sigInfo];
}
Expand Down Expand Up @@ -232,10 +232,11 @@ namespace ts.GoToDefinition {
}
}

function getDefinitionFromSymbol(typeChecker: TypeChecker, symbol: Symbol, node: Node): DefinitionInfo[] | undefined {
function getDefinitionFromSymbol(typeChecker: TypeChecker, symbol: Symbol, node: Node, declarationNode?: Node): DefinitionInfo[] | undefined {
// There are cases when you extend a function by adding properties to it afterwards,
// we want to strip those extra properties
const filteredDeclarations = filter(symbol.declarations, d => !isAssignmentDeclaration(d) || d === symbol.valueDeclaration) || undefined;
// we want to strip those extra properties.
// For deduping purposes, we also want to exclude any declarationNodes if provided.
const filteredDeclarations = filter(symbol.declarations, d => d !== declarationNode && (!isAssignmentDeclaration(d) || d === symbol.valueDeclaration)) || undefined;
return getConstructSignatureDefinition() || getCallSignatureDefinition() || map(filteredDeclarations, declaration => createDefinitionInfo(declaration, typeChecker, symbol, node));

function getConstructSignatureDefinition(): DefinitionInfo[] | undefined {
Expand All @@ -258,8 +259,13 @@ namespace ts.GoToDefinition {
return undefined;
}
const declarations = signatureDeclarations.filter(selectConstructors ? isConstructorDeclaration : isFunctionLike);
const declarationsWithBody = declarations.filter(d => !!(<FunctionLikeDeclaration>d).body);

// declarations defined on the global scope can be defined on multiple files. Get all of them.
return declarations.length
? [createDefinitionInfo(find(declarations, d => !!(<FunctionLikeDeclaration>d).body) || last(declarations), typeChecker, symbol, node)]
? declarationsWithBody.length !== 0
? declarationsWithBody.map(x => createDefinitionInfo(x, typeChecker, symbol, node))
: [createDefinitionInfo(last(declarations), typeChecker, symbol, node)]
: undefined;
}
}
Expand Down
10 changes: 9 additions & 1 deletion tests/cases/fourslash/goToDefinitionAcrossMultipleProjects.ts
Expand Up @@ -7,8 +7,16 @@
////var /*def2*/x: number;

//@Filename: c.ts
////var /*def3*/x: number;

//@Filename: d.ts
////var /*def4*/x: number;

//@Filename: e.ts
/////// <reference path="a.ts" />
/////// <reference path="b.ts" />
/////// <reference path="c.ts" />
/////// <reference path="d.ts" />
////[|/*use*/x|]++;

verify.goToDefinition("use", ["def1", "def2"]);
verify.goToDefinition("use", ["def1", "def2", "def3", "def4"]);

0 comments on commit 029f7a3

Please sign in to comment.