Skip to content

Fixed goto when global declarations are on multiple files #33124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 29, 2019
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
16 changes: 11 additions & 5 deletions src/services/goToDefinition.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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"]);