Skip to content

Commit c1a291d

Browse files
author
Andy Hanson
committed
Get the type of a constructor as the type of the class
Fixes a bug when constructor overloads are improper and no signatures can be found.
1 parent 3f126a5 commit c1a291d

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

src/services/services.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4628,7 +4628,8 @@ namespace ts {
46284628
const symbolFlags = symbol.flags;
46294629
let symbolKind = getSymbolKindOfConstructorPropertyMethodAccessorFunctionOrVar(symbol, symbolFlags, location);
46304630
let hasAddedSymbolInfo: boolean;
4631-
const isThisExpression: boolean = location.kind === SyntaxKind.ThisKeyword && isExpression(location);
4631+
const isThisExpression = location.kind === SyntaxKind.ThisKeyword && isExpression(location);
4632+
const isConstructor = location.kind === SyntaxKind.ConstructorKeyword;
46324633
let type: Type;
46334634

46344635
// Class at constructor site need to be shown as constructor apart from property,method, vars
@@ -4639,7 +4640,12 @@ namespace ts {
46394640
}
46404641

46414642
let signature: Signature;
4642-
type = isThisExpression ? typeChecker.getTypeAtLocation(location) : typeChecker.getTypeOfSymbolAtLocation(symbol, location);
4643+
type = isThisExpression
4644+
? typeChecker.getTypeAtLocation(location)
4645+
: isConstructor
4646+
// For constructor, get type of the class.
4647+
? typeChecker.getTypeOfSymbolAtLocation(symbol.parent, location)
4648+
: typeChecker.getTypeOfSymbolAtLocation(symbol, location);
46434649
if (type) {
46444650
if (location.parent && location.parent.kind === SyntaxKind.PropertyAccessExpression) {
46454651
const right = (<PropertyAccessExpression>location.parent).name;
@@ -4737,9 +4743,7 @@ namespace ts {
47374743
if (functionDeclaration.kind === SyntaxKind.Constructor) {
47384744
// show (constructor) Type(...) signature
47394745
symbolKind = ScriptElementKind.constructorImplementationElement;
4740-
// For a constructor, `type` will be unknown.
4741-
const showSymbol = symbol.declarations[0].kind === SyntaxKind.Constructor ? symbol.parent : type.symbol;
4742-
addPrefixForAnyFunctionOrVar(showSymbol, symbolKind);
4746+
addPrefixForAnyFunctionOrVar(type.symbol, symbolKind);
47434747
}
47444748
else {
47454749
// (function/method) symbol(..signature)

tests/cases/fourslash/findAllReferencesOfConstructor.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,6 @@
3939
////class d extends a.C { constructor() { [|super|](); }
4040

4141
const ranges = test.ranges();
42-
verify.referencesOf(ranges[0], ranges);
43-
verify.referencesOf(ranges[1], ranges);
42+
for (const ctr of ranges.slice(0, 3)) {
43+
verify.referencesOf(ctr, ranges);
44+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
////class C {
4+
//// [|constructor|](n: number);
5+
//// [|constructor|](){}
6+
////}
7+
8+
verify.rangesReferenceEachOther();

0 commit comments

Comments
 (0)