Merge pull request #6498 from Microsoft/convertLoopWithMissingInitializer

do not crash if initializer in For-statement is missing
This commit is contained in:
Vladimir Matveev 2016-01-15 07:47:32 -08:00
commit efc573f263
9 changed files with 198 additions and 1 deletions

View File

@ -2886,7 +2886,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
case SyntaxKind.ForStatement:
case SyntaxKind.ForInStatement:
case SyntaxKind.ForOfStatement:
if ((<ForStatement | ForInStatement | ForOfStatement>node).initializer.kind === SyntaxKind.VariableDeclarationList) {
const initializer = (<ForStatement | ForInStatement | ForOfStatement>node).initializer;
if (initializer && initializer.kind === SyntaxKind.VariableDeclarationList) {
loopInitializer = <VariableDeclarationList>(<ForStatement | ForInStatement | ForOfStatement>node).initializer;
}
break;

View File

@ -0,0 +1,35 @@
//// [capturedLetConstInLoop11.ts]
for (;;) {
let x = 1;
() => x;
}
function foo() {
for (;;) {
const a = 0;
switch(a) {
case 0: return () => a;
}
}
}
//// [capturedLetConstInLoop11.js]
var _loop_1 = function() {
var x = 1;
(function () { return x; });
};
for (;;) {
_loop_1();
}
function foo() {
var _loop_2 = function() {
var a = 0;
switch (a) {
case 0: return { value: function () { return a; } };
}
};
for (;;) {
var state_2 = _loop_2();
if (typeof state_2 === "object") return state_2.value
}
}

View File

@ -0,0 +1,24 @@
=== tests/cases/compiler/capturedLetConstInLoop11.ts ===
for (;;) {
let x = 1;
>x : Symbol(x, Decl(capturedLetConstInLoop11.ts, 1, 7))
() => x;
>x : Symbol(x, Decl(capturedLetConstInLoop11.ts, 1, 7))
}
function foo() {
>foo : Symbol(foo, Decl(capturedLetConstInLoop11.ts, 3, 1))
for (;;) {
const a = 0;
>a : Symbol(a, Decl(capturedLetConstInLoop11.ts, 7, 13))
switch(a) {
>a : Symbol(a, Decl(capturedLetConstInLoop11.ts, 7, 13))
case 0: return () => a;
>a : Symbol(a, Decl(capturedLetConstInLoop11.ts, 7, 13))
}
}
}

View File

@ -0,0 +1,29 @@
=== tests/cases/compiler/capturedLetConstInLoop11.ts ===
for (;;) {
let x = 1;
>x : number
>1 : number
() => x;
>() => x : () => number
>x : number
}
function foo() {
>foo : () => () => number
for (;;) {
const a = 0;
>a : number
>0 : number
switch(a) {
>a : number
case 0: return () => a;
>0 : number
>() => a : () => number
>a : number
}
}
}

View File

@ -0,0 +1,28 @@
//// [capturedLetConstInLoop11_ES6.ts]
for (;;) {
let x = 1;
() => x;
}
function foo() {
for (;;) {
const a = 0;
switch(a) {
case 0: return () => a;
}
}
}
//// [capturedLetConstInLoop11_ES6.js]
for (;;) {
let x = 1;
(() => x);
}
function foo() {
for (;;) {
const a = 0;
switch (a) {
case 0: return () => a;
}
}
}

View File

@ -0,0 +1,24 @@
=== tests/cases/compiler/capturedLetConstInLoop11_ES6.ts ===
for (;;) {
let x = 1;
>x : Symbol(x, Decl(capturedLetConstInLoop11_ES6.ts, 1, 7))
() => x;
>x : Symbol(x, Decl(capturedLetConstInLoop11_ES6.ts, 1, 7))
}
function foo() {
>foo : Symbol(foo, Decl(capturedLetConstInLoop11_ES6.ts, 3, 1))
for (;;) {
const a = 0;
>a : Symbol(a, Decl(capturedLetConstInLoop11_ES6.ts, 7, 13))
switch(a) {
>a : Symbol(a, Decl(capturedLetConstInLoop11_ES6.ts, 7, 13))
case 0: return () => a;
>a : Symbol(a, Decl(capturedLetConstInLoop11_ES6.ts, 7, 13))
}
}
}

View File

@ -0,0 +1,29 @@
=== tests/cases/compiler/capturedLetConstInLoop11_ES6.ts ===
for (;;) {
let x = 1;
>x : number
>1 : number
() => x;
>() => x : () => number
>x : number
}
function foo() {
>foo : () => () => number
for (;;) {
const a = 0;
>a : number
>0 : number
switch(a) {
>a : number
case 0: return () => a;
>0 : number
>() => a : () => number
>a : number
}
}
}

View File

@ -0,0 +1,13 @@
for (;;) {
let x = 1;
() => x;
}
function foo() {
for (;;) {
const a = 0;
switch(a) {
case 0: return () => a;
}
}
}

View File

@ -0,0 +1,14 @@
// @target: ES6
for (;;) {
let x = 1;
() => x;
}
function foo() {
for (;;) {
const a = 0;
switch(a) {
case 0: return () => a;
}
}
}