Skip to content

Commit c9c6d82

Browse files
Merge pull request #4480 from Microsoft/goToDefinitionOnConstructors
Fix issue with go-to-definition when first declaration of symbol is not specifically a class declaration
2 parents cb1d582 + 9f3c99e commit c9c6d82

File tree

4 files changed

+41
-5
lines changed

4 files changed

+41
-5
lines changed

src/compiler/utilities.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -611,11 +611,11 @@ namespace ts {
611611
return false;
612612
}
613613

614-
export function isAccessor(node: Node): boolean {
614+
export function isAccessor(node: Node): node is AccessorDeclaration {
615615
return node && (node.kind === SyntaxKind.GetAccessor || node.kind === SyntaxKind.SetAccessor);
616616
}
617617

618-
export function isClassLike(node: Node): boolean {
618+
export function isClassLike(node: Node): node is ClassLikeDeclaration {
619619
return node && (node.kind === SyntaxKind.ClassDeclaration || node.kind === SyntaxKind.ClassExpression);
620620
}
621621

src/services/services.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4480,10 +4480,19 @@ namespace ts {
44804480
// and in either case the symbol has a construct signature definition, i.e. class
44814481
if (isNewExpressionTarget(location) || location.kind === SyntaxKind.ConstructorKeyword) {
44824482
if (symbol.flags & SymbolFlags.Class) {
4483-
let classDeclaration = <ClassDeclaration>symbol.getDeclarations()[0];
4484-
Debug.assert(classDeclaration && classDeclaration.kind === SyntaxKind.ClassDeclaration);
4483+
// Find the first class-like declaration and try to get the construct signature.
4484+
for (let declaration of symbol.getDeclarations()) {
4485+
if (isClassLike(declaration)) {
4486+
return tryAddSignature(declaration.members,
4487+
/*selectConstructors*/ true,
4488+
symbolKind,
4489+
symbolName,
4490+
containerName,
4491+
result);
4492+
}
4493+
}
44854494

4486-
return tryAddSignature(classDeclaration.members, /*selectConstructors*/ true, symbolKind, symbolName, containerName, result);
4495+
Debug.fail("Expected declaration to have at least one class-like declaration");
44874496
}
44884497
}
44894498
return false;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////var x = class C {
4+
//// /*definition*/constructor() {
5+
//// var other = new /*usage*/C;
6+
//// }
7+
////}
8+
9+
goTo.marker("usage");
10+
goTo.definition();
11+
verify.caretAtMarker("definition");
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////namespace Foo {
4+
//// export var x;
5+
////}
6+
////
7+
////class Foo {
8+
//// /*definition*/constructor() {
9+
//// }
10+
////}
11+
////
12+
////var x = new /*usage*/Foo();
13+
14+
goTo.marker("usage");
15+
goTo.definition();
16+
verify.caretAtMarker("definition");

0 commit comments

Comments
 (0)