Skip to content

Commit efc573f

Browse files
committed
Merge pull request #6498 from Microsoft/convertLoopWithMissingInitializer
do not crash if initializer in For-statement is missing
2 parents d1aa01c + 0a0c3e0 commit efc573f

9 files changed

+198
-1
lines changed

src/compiler/emitter.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2886,7 +2886,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
28862886
case SyntaxKind.ForStatement:
28872887
case SyntaxKind.ForInStatement:
28882888
case SyntaxKind.ForOfStatement:
2889-
if ((<ForStatement | ForInStatement | ForOfStatement>node).initializer.kind === SyntaxKind.VariableDeclarationList) {
2889+
const initializer = (<ForStatement | ForInStatement | ForOfStatement>node).initializer;
2890+
if (initializer && initializer.kind === SyntaxKind.VariableDeclarationList) {
28902891
loopInitializer = <VariableDeclarationList>(<ForStatement | ForInStatement | ForOfStatement>node).initializer;
28912892
}
28922893
break;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//// [capturedLetConstInLoop11.ts]
2+
for (;;) {
3+
let x = 1;
4+
() => x;
5+
}
6+
7+
function foo() {
8+
for (;;) {
9+
const a = 0;
10+
switch(a) {
11+
case 0: return () => a;
12+
}
13+
}
14+
}
15+
16+
//// [capturedLetConstInLoop11.js]
17+
var _loop_1 = function() {
18+
var x = 1;
19+
(function () { return x; });
20+
};
21+
for (;;) {
22+
_loop_1();
23+
}
24+
function foo() {
25+
var _loop_2 = function() {
26+
var a = 0;
27+
switch (a) {
28+
case 0: return { value: function () { return a; } };
29+
}
30+
};
31+
for (;;) {
32+
var state_2 = _loop_2();
33+
if (typeof state_2 === "object") return state_2.value
34+
}
35+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
=== tests/cases/compiler/capturedLetConstInLoop11.ts ===
2+
for (;;) {
3+
let x = 1;
4+
>x : Symbol(x, Decl(capturedLetConstInLoop11.ts, 1, 7))
5+
6+
() => x;
7+
>x : Symbol(x, Decl(capturedLetConstInLoop11.ts, 1, 7))
8+
}
9+
10+
function foo() {
11+
>foo : Symbol(foo, Decl(capturedLetConstInLoop11.ts, 3, 1))
12+
13+
for (;;) {
14+
const a = 0;
15+
>a : Symbol(a, Decl(capturedLetConstInLoop11.ts, 7, 13))
16+
17+
switch(a) {
18+
>a : Symbol(a, Decl(capturedLetConstInLoop11.ts, 7, 13))
19+
20+
case 0: return () => a;
21+
>a : Symbol(a, Decl(capturedLetConstInLoop11.ts, 7, 13))
22+
}
23+
}
24+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
=== tests/cases/compiler/capturedLetConstInLoop11.ts ===
2+
for (;;) {
3+
let x = 1;
4+
>x : number
5+
>1 : number
6+
7+
() => x;
8+
>() => x : () => number
9+
>x : number
10+
}
11+
12+
function foo() {
13+
>foo : () => () => number
14+
15+
for (;;) {
16+
const a = 0;
17+
>a : number
18+
>0 : number
19+
20+
switch(a) {
21+
>a : number
22+
23+
case 0: return () => a;
24+
>0 : number
25+
>() => a : () => number
26+
>a : number
27+
}
28+
}
29+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//// [capturedLetConstInLoop11_ES6.ts]
2+
for (;;) {
3+
let x = 1;
4+
() => x;
5+
}
6+
7+
function foo() {
8+
for (;;) {
9+
const a = 0;
10+
switch(a) {
11+
case 0: return () => a;
12+
}
13+
}
14+
}
15+
16+
//// [capturedLetConstInLoop11_ES6.js]
17+
for (;;) {
18+
let x = 1;
19+
(() => x);
20+
}
21+
function foo() {
22+
for (;;) {
23+
const a = 0;
24+
switch (a) {
25+
case 0: return () => a;
26+
}
27+
}
28+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
=== tests/cases/compiler/capturedLetConstInLoop11_ES6.ts ===
2+
for (;;) {
3+
let x = 1;
4+
>x : Symbol(x, Decl(capturedLetConstInLoop11_ES6.ts, 1, 7))
5+
6+
() => x;
7+
>x : Symbol(x, Decl(capturedLetConstInLoop11_ES6.ts, 1, 7))
8+
}
9+
10+
function foo() {
11+
>foo : Symbol(foo, Decl(capturedLetConstInLoop11_ES6.ts, 3, 1))
12+
13+
for (;;) {
14+
const a = 0;
15+
>a : Symbol(a, Decl(capturedLetConstInLoop11_ES6.ts, 7, 13))
16+
17+
switch(a) {
18+
>a : Symbol(a, Decl(capturedLetConstInLoop11_ES6.ts, 7, 13))
19+
20+
case 0: return () => a;
21+
>a : Symbol(a, Decl(capturedLetConstInLoop11_ES6.ts, 7, 13))
22+
}
23+
}
24+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
=== tests/cases/compiler/capturedLetConstInLoop11_ES6.ts ===
2+
for (;;) {
3+
let x = 1;
4+
>x : number
5+
>1 : number
6+
7+
() => x;
8+
>() => x : () => number
9+
>x : number
10+
}
11+
12+
function foo() {
13+
>foo : () => () => number
14+
15+
for (;;) {
16+
const a = 0;
17+
>a : number
18+
>0 : number
19+
20+
switch(a) {
21+
>a : number
22+
23+
case 0: return () => a;
24+
>0 : number
25+
>() => a : () => number
26+
>a : number
27+
}
28+
}
29+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
for (;;) {
2+
let x = 1;
3+
() => x;
4+
}
5+
6+
function foo() {
7+
for (;;) {
8+
const a = 0;
9+
switch(a) {
10+
case 0: return () => a;
11+
}
12+
}
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// @target: ES6
2+
for (;;) {
3+
let x = 1;
4+
() => x;
5+
}
6+
7+
function foo() {
8+
for (;;) {
9+
const a = 0;
10+
switch(a) {
11+
case 0: return () => a;
12+
}
13+
}
14+
}

0 commit comments

Comments
 (0)