Skip to content

Commit 4221fb6

Browse files
authored
Check for initializer before using it (#18708)
1 parent b2ec333 commit 4221fb6

File tree

4 files changed

+57
-2
lines changed

4 files changed

+57
-2
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16491,7 +16491,7 @@ namespace ts {
1649116491

1649216492
// If the symbol of the node has members, treat it like a constructor.
1649316493
const symbol = isFunctionDeclaration(node) || isFunctionExpression(node) ? getSymbolOfNode(node) :
16494-
isVariableDeclaration(node) && isFunctionExpression(node.initializer) ? getSymbolOfNode(node.initializer) :
16494+
isVariableDeclaration(node) && node.initializer && isFunctionExpression(node.initializer) ? getSymbolOfNode(node.initializer) :
1649516495
undefined;
1649616496

1649716497
return symbol && symbol.members !== undefined;

tests/baselines/reference/constructorFunctions.symbols

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,23 @@ const c4_v2 = new C4();
7474
>c4_v2 : Symbol(c4_v2, Decl(index.js, 30, 5))
7575
>C4 : Symbol(C4, Decl(index.js, 25, 3))
7676

77+
var c5_v1;
78+
>c5_v1 : Symbol(c5_v1, Decl(index.js, 32, 3))
79+
80+
c5_v1 = function f() { };
81+
>c5_v1 : Symbol(c5_v1, Decl(index.js, 32, 3))
82+
>f : Symbol(f, Decl(index.js, 33, 7))
83+
84+
new c5_v1();
85+
>c5_v1 : Symbol(c5_v1, Decl(index.js, 32, 3))
86+
87+
var c5_v2;
88+
>c5_v2 : Symbol(c5_v2, Decl(index.js, 36, 3))
89+
90+
c5_v2 = class { };
91+
>c5_v2 : Symbol(c5_v2, Decl(index.js, 36, 3))
92+
93+
new c5_v2();
94+
>c5_v2 : Symbol(c5_v2, Decl(index.js, 36, 3))
95+
96+

tests/baselines/reference/constructorFunctions.types

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,29 @@ const c4_v2 = new C4();
112112
>new C4() : {}
113113
>C4 : () => any
114114

115+
var c5_v1;
116+
>c5_v1 : any
117+
118+
c5_v1 = function f() { };
119+
>c5_v1 = function f() { } : () => void
120+
>c5_v1 : any
121+
>function f() { } : () => void
122+
>f : () => void
123+
124+
new c5_v1();
125+
>new c5_v1() : any
126+
>c5_v1 : () => void
127+
128+
var c5_v2;
129+
>c5_v2 : any
130+
131+
c5_v2 = class { };
132+
>c5_v2 = class { } : typeof (Anonymous class)
133+
>c5_v2 : any
134+
>class { } : typeof (Anonymous class)
135+
136+
new c5_v2();
137+
>new c5_v2() : (Anonymous class)
138+
>c5_v2 : typeof (Anonymous class)
139+
140+

tests/cases/conformance/salsa/constructorFunctions.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,13 @@ var C4 = function () {
3333
};
3434

3535
const c4_v1 = C4();
36-
const c4_v2 = new C4();
36+
const c4_v2 = new C4();
37+
38+
var c5_v1;
39+
c5_v1 = function f() { };
40+
new c5_v1();
41+
42+
var c5_v2;
43+
c5_v2 = class { };
44+
new c5_v2();
45+

0 commit comments

Comments
 (0)