lift multiple statements into block if necessary (#12965)

This commit is contained in:
Vladimir Matveev 2016-12-15 16:31:50 -08:00 committed by GitHub
parent b624d47817
commit f034022e25
5 changed files with 174 additions and 2 deletions

View file

@ -2290,7 +2290,7 @@ namespace ts {
}
startLexicalEnvironment();
let loopBody = visitNode(node.statement, visitor, isStatement);
let loopBody = visitNode(node.statement, visitor, isStatement, /*optional*/ false, liftToBlock);
const lexicalEnvironment = endLexicalEnvironment();
const currentState = convertedLoopState;
@ -2305,7 +2305,10 @@ namespace ts {
loopBody = createBlock(statements, /*location*/ undefined, /*multiline*/ true);
}
if (!isBlock(loopBody)) {
if (isBlock(loopBody)) {
loopBody.multiLine = true;
}
else {
loopBody = createBlock([loopBody], /*location*/ undefined, /*multiline*/ true);
}

View file

@ -0,0 +1,47 @@
//// [nestedLoops.ts]
export class Test {
constructor() {
let outerArray: Array<number> = [1, 2, 3];
let innerArray: Array<number> = [1, 2, 3];
for (let outer of outerArray)
for (let inner of innerArray) {
this.aFunction((newValue, oldValue) => {
let x = outer + inner + newValue;
});
}
}
public aFunction(func: (newValue: any, oldValue: any) => void): void {
}
}
//// [nestedLoops.js]
"use strict";
var Test = (function () {
function Test() {
var outerArray = [1, 2, 3];
var innerArray = [1, 2, 3];
var _loop_1 = function (outer) {
var _loop_2 = function (inner) {
this_1.aFunction(function (newValue, oldValue) {
var x = outer + inner + newValue;
});
};
for (var _i = 0, innerArray_1 = innerArray; _i < innerArray_1.length; _i++) {
var inner = innerArray_1[_i];
_loop_2(inner);
}
};
var this_1 = this;
for (var _i = 0, outerArray_1 = outerArray; _i < outerArray_1.length; _i++) {
var outer = outerArray_1[_i];
_loop_1(outer);
}
}
Test.prototype.aFunction = function (func) {
};
return Test;
}());
exports.Test = Test;

View file

@ -0,0 +1,46 @@
=== tests/cases/compiler/nestedLoops.ts ===
export class Test {
>Test : Symbol(Test, Decl(nestedLoops.ts, 0, 0))
constructor() {
let outerArray: Array<number> = [1, 2, 3];
>outerArray : Symbol(outerArray, Decl(nestedLoops.ts, 3, 11))
>Array : Symbol(Array, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
let innerArray: Array<number> = [1, 2, 3];
>innerArray : Symbol(innerArray, Decl(nestedLoops.ts, 4, 11))
>Array : Symbol(Array, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
for (let outer of outerArray)
>outer : Symbol(outer, Decl(nestedLoops.ts, 6, 16))
>outerArray : Symbol(outerArray, Decl(nestedLoops.ts, 3, 11))
for (let inner of innerArray) {
>inner : Symbol(inner, Decl(nestedLoops.ts, 7, 20))
>innerArray : Symbol(innerArray, Decl(nestedLoops.ts, 4, 11))
this.aFunction((newValue, oldValue) => {
>this.aFunction : Symbol(Test.aFunction, Decl(nestedLoops.ts, 12, 5))
>this : Symbol(Test, Decl(nestedLoops.ts, 0, 0))
>aFunction : Symbol(Test.aFunction, Decl(nestedLoops.ts, 12, 5))
>newValue : Symbol(newValue, Decl(nestedLoops.ts, 8, 32))
>oldValue : Symbol(oldValue, Decl(nestedLoops.ts, 8, 41))
let x = outer + inner + newValue;
>x : Symbol(x, Decl(nestedLoops.ts, 9, 23))
>outer : Symbol(outer, Decl(nestedLoops.ts, 6, 16))
>inner : Symbol(inner, Decl(nestedLoops.ts, 7, 20))
>newValue : Symbol(newValue, Decl(nestedLoops.ts, 8, 32))
});
}
}
public aFunction(func: (newValue: any, oldValue: any) => void): void {
>aFunction : Symbol(Test.aFunction, Decl(nestedLoops.ts, 12, 5))
>func : Symbol(func, Decl(nestedLoops.ts, 14, 21))
>newValue : Symbol(newValue, Decl(nestedLoops.ts, 14, 28))
>oldValue : Symbol(oldValue, Decl(nestedLoops.ts, 14, 42))
}
}

View file

@ -0,0 +1,58 @@
=== tests/cases/compiler/nestedLoops.ts ===
export class Test {
>Test : Test
constructor() {
let outerArray: Array<number> = [1, 2, 3];
>outerArray : number[]
>Array : T[]
>[1, 2, 3] : number[]
>1 : 1
>2 : 2
>3 : 3
let innerArray: Array<number> = [1, 2, 3];
>innerArray : number[]
>Array : T[]
>[1, 2, 3] : number[]
>1 : 1
>2 : 2
>3 : 3
for (let outer of outerArray)
>outer : number
>outerArray : number[]
for (let inner of innerArray) {
>inner : number
>innerArray : number[]
this.aFunction((newValue, oldValue) => {
>this.aFunction((newValue, oldValue) => { let x = outer + inner + newValue; }) : void
>this.aFunction : (func: (newValue: any, oldValue: any) => void) => void
>this : this
>aFunction : (func: (newValue: any, oldValue: any) => void) => void
>(newValue, oldValue) => { let x = outer + inner + newValue; } : (newValue: any, oldValue: any) => void
>newValue : any
>oldValue : any
let x = outer + inner + newValue;
>x : any
>outer + inner + newValue : any
>outer + inner : number
>outer : number
>inner : number
>newValue : any
});
}
}
public aFunction(func: (newValue: any, oldValue: any) => void): void {
>aFunction : (func: (newValue: any, oldValue: any) => void) => void
>func : (newValue: any, oldValue: any) => void
>newValue : any
>oldValue : any
}
}

View file

@ -0,0 +1,18 @@
// @target: es5
export class Test {
constructor() {
let outerArray: Array<number> = [1, 2, 3];
let innerArray: Array<number> = [1, 2, 3];
for (let outer of outerArray)
for (let inner of innerArray) {
this.aFunction((newValue, oldValue) => {
let x = outer + inner + newValue;
});
}
}
public aFunction(func: (newValue: any, oldValue: any) => void): void {
}
}