Skip to content

Commit f034022

Browse files
authored
lift multiple statements into block if necessary (#12965)
1 parent b624d47 commit f034022

File tree

5 files changed

+174
-2
lines changed

5 files changed

+174
-2
lines changed

src/compiler/transformers/es2015.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2290,7 +2290,7 @@ namespace ts {
22902290
}
22912291

22922292
startLexicalEnvironment();
2293-
let loopBody = visitNode(node.statement, visitor, isStatement);
2293+
let loopBody = visitNode(node.statement, visitor, isStatement, /*optional*/ false, liftToBlock);
22942294
const lexicalEnvironment = endLexicalEnvironment();
22952295

22962296
const currentState = convertedLoopState;
@@ -2305,7 +2305,10 @@ namespace ts {
23052305
loopBody = createBlock(statements, /*location*/ undefined, /*multiline*/ true);
23062306
}
23072307

2308-
if (!isBlock(loopBody)) {
2308+
if (isBlock(loopBody)) {
2309+
loopBody.multiLine = true;
2310+
}
2311+
else {
23092312
loopBody = createBlock([loopBody], /*location*/ undefined, /*multiline*/ true);
23102313
}
23112314

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//// [nestedLoops.ts]
2+
export class Test {
3+
constructor() {
4+
5+
let outerArray: Array<number> = [1, 2, 3];
6+
let innerArray: Array<number> = [1, 2, 3];
7+
8+
for (let outer of outerArray)
9+
for (let inner of innerArray) {
10+
this.aFunction((newValue, oldValue) => {
11+
let x = outer + inner + newValue;
12+
});
13+
}
14+
}
15+
16+
public aFunction(func: (newValue: any, oldValue: any) => void): void {
17+
}
18+
}
19+
20+
//// [nestedLoops.js]
21+
"use strict";
22+
var Test = (function () {
23+
function Test() {
24+
var outerArray = [1, 2, 3];
25+
var innerArray = [1, 2, 3];
26+
var _loop_1 = function (outer) {
27+
var _loop_2 = function (inner) {
28+
this_1.aFunction(function (newValue, oldValue) {
29+
var x = outer + inner + newValue;
30+
});
31+
};
32+
for (var _i = 0, innerArray_1 = innerArray; _i < innerArray_1.length; _i++) {
33+
var inner = innerArray_1[_i];
34+
_loop_2(inner);
35+
}
36+
};
37+
var this_1 = this;
38+
for (var _i = 0, outerArray_1 = outerArray; _i < outerArray_1.length; _i++) {
39+
var outer = outerArray_1[_i];
40+
_loop_1(outer);
41+
}
42+
}
43+
Test.prototype.aFunction = function (func) {
44+
};
45+
return Test;
46+
}());
47+
exports.Test = Test;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
=== tests/cases/compiler/nestedLoops.ts ===
2+
export class Test {
3+
>Test : Symbol(Test, Decl(nestedLoops.ts, 0, 0))
4+
5+
constructor() {
6+
7+
let outerArray: Array<number> = [1, 2, 3];
8+
>outerArray : Symbol(outerArray, Decl(nestedLoops.ts, 3, 11))
9+
>Array : Symbol(Array, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
10+
11+
let innerArray: Array<number> = [1, 2, 3];
12+
>innerArray : Symbol(innerArray, Decl(nestedLoops.ts, 4, 11))
13+
>Array : Symbol(Array, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
14+
15+
for (let outer of outerArray)
16+
>outer : Symbol(outer, Decl(nestedLoops.ts, 6, 16))
17+
>outerArray : Symbol(outerArray, Decl(nestedLoops.ts, 3, 11))
18+
19+
for (let inner of innerArray) {
20+
>inner : Symbol(inner, Decl(nestedLoops.ts, 7, 20))
21+
>innerArray : Symbol(innerArray, Decl(nestedLoops.ts, 4, 11))
22+
23+
this.aFunction((newValue, oldValue) => {
24+
>this.aFunction : Symbol(Test.aFunction, Decl(nestedLoops.ts, 12, 5))
25+
>this : Symbol(Test, Decl(nestedLoops.ts, 0, 0))
26+
>aFunction : Symbol(Test.aFunction, Decl(nestedLoops.ts, 12, 5))
27+
>newValue : Symbol(newValue, Decl(nestedLoops.ts, 8, 32))
28+
>oldValue : Symbol(oldValue, Decl(nestedLoops.ts, 8, 41))
29+
30+
let x = outer + inner + newValue;
31+
>x : Symbol(x, Decl(nestedLoops.ts, 9, 23))
32+
>outer : Symbol(outer, Decl(nestedLoops.ts, 6, 16))
33+
>inner : Symbol(inner, Decl(nestedLoops.ts, 7, 20))
34+
>newValue : Symbol(newValue, Decl(nestedLoops.ts, 8, 32))
35+
36+
});
37+
}
38+
}
39+
40+
public aFunction(func: (newValue: any, oldValue: any) => void): void {
41+
>aFunction : Symbol(Test.aFunction, Decl(nestedLoops.ts, 12, 5))
42+
>func : Symbol(func, Decl(nestedLoops.ts, 14, 21))
43+
>newValue : Symbol(newValue, Decl(nestedLoops.ts, 14, 28))
44+
>oldValue : Symbol(oldValue, Decl(nestedLoops.ts, 14, 42))
45+
}
46+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
=== tests/cases/compiler/nestedLoops.ts ===
2+
export class Test {
3+
>Test : Test
4+
5+
constructor() {
6+
7+
let outerArray: Array<number> = [1, 2, 3];
8+
>outerArray : number[]
9+
>Array : T[]
10+
>[1, 2, 3] : number[]
11+
>1 : 1
12+
>2 : 2
13+
>3 : 3
14+
15+
let innerArray: Array<number> = [1, 2, 3];
16+
>innerArray : number[]
17+
>Array : T[]
18+
>[1, 2, 3] : number[]
19+
>1 : 1
20+
>2 : 2
21+
>3 : 3
22+
23+
for (let outer of outerArray)
24+
>outer : number
25+
>outerArray : number[]
26+
27+
for (let inner of innerArray) {
28+
>inner : number
29+
>innerArray : number[]
30+
31+
this.aFunction((newValue, oldValue) => {
32+
>this.aFunction((newValue, oldValue) => { let x = outer + inner + newValue; }) : void
33+
>this.aFunction : (func: (newValue: any, oldValue: any) => void) => void
34+
>this : this
35+
>aFunction : (func: (newValue: any, oldValue: any) => void) => void
36+
>(newValue, oldValue) => { let x = outer + inner + newValue; } : (newValue: any, oldValue: any) => void
37+
>newValue : any
38+
>oldValue : any
39+
40+
let x = outer + inner + newValue;
41+
>x : any
42+
>outer + inner + newValue : any
43+
>outer + inner : number
44+
>outer : number
45+
>inner : number
46+
>newValue : any
47+
48+
});
49+
}
50+
}
51+
52+
public aFunction(func: (newValue: any, oldValue: any) => void): void {
53+
>aFunction : (func: (newValue: any, oldValue: any) => void) => void
54+
>func : (newValue: any, oldValue: any) => void
55+
>newValue : any
56+
>oldValue : any
57+
}
58+
}

tests/cases/compiler/nestedLoops.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// @target: es5
2+
export class Test {
3+
constructor() {
4+
5+
let outerArray: Array<number> = [1, 2, 3];
6+
let innerArray: Array<number> = [1, 2, 3];
7+
8+
for (let outer of outerArray)
9+
for (let inner of innerArray) {
10+
this.aFunction((newValue, oldValue) => {
11+
let x = outer + inner + newValue;
12+
});
13+
}
14+
}
15+
16+
public aFunction(func: (newValue: any, oldValue: any) => void): void {
17+
}
18+
}

0 commit comments

Comments
 (0)