Skip to content

Commit abc30b2

Browse files
authored
handle cases when body of for-of statement is expanded after loop conversion (microsoft#13677)
1 parent e679b2e commit abc30b2

File tree

5 files changed

+60
-1
lines changed

5 files changed

+60
-1
lines changed

src/compiler/transformers/es2015.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2316,7 +2316,7 @@ namespace ts {
23162316
addRange(statements, convertedLoopBodyStatements);
23172317
}
23182318
else {
2319-
const statement = visitNode(node.statement, visitor, isStatement);
2319+
const statement = visitNode(node.statement, visitor, isStatement, /*optional*/ false, liftToBlock);
23202320
if (isBlock(statement)) {
23212321
addRange(statements, statement.statements);
23222322
bodyLocation = statement;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//// [nestedLoopWithOnlyInnerLetCaptured.ts]
2+
declare let doSomething;
3+
4+
for (let a1 of [])
5+
for (let a2 of a1.someArray)
6+
doSomething(() => a2);
7+
8+
//// [nestedLoopWithOnlyInnerLetCaptured.js]
9+
for (var _i = 0, _a = []; _i < _a.length; _i++) {
10+
var a1 = _a[_i];
11+
var _loop_1 = function (a2) {
12+
doSomething(function () { return a2; });
13+
};
14+
for (var _b = 0, _c = a1.someArray; _b < _c.length; _b++) {
15+
var a2 = _c[_b];
16+
_loop_1(a2);
17+
}
18+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
=== tests/cases/compiler/nestedLoopWithOnlyInnerLetCaptured.ts ===
2+
declare let doSomething;
3+
>doSomething : Symbol(doSomething, Decl(nestedLoopWithOnlyInnerLetCaptured.ts, 0, 11))
4+
5+
for (let a1 of [])
6+
>a1 : Symbol(a1, Decl(nestedLoopWithOnlyInnerLetCaptured.ts, 2, 8))
7+
8+
for (let a2 of a1.someArray)
9+
>a2 : Symbol(a2, Decl(nestedLoopWithOnlyInnerLetCaptured.ts, 3, 12))
10+
>a1 : Symbol(a1, Decl(nestedLoopWithOnlyInnerLetCaptured.ts, 2, 8))
11+
12+
doSomething(() => a2);
13+
>doSomething : Symbol(doSomething, Decl(nestedLoopWithOnlyInnerLetCaptured.ts, 0, 11))
14+
>a2 : Symbol(a2, Decl(nestedLoopWithOnlyInnerLetCaptured.ts, 3, 12))
15+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
=== tests/cases/compiler/nestedLoopWithOnlyInnerLetCaptured.ts ===
2+
declare let doSomething;
3+
>doSomething : any
4+
5+
for (let a1 of [])
6+
>a1 : any
7+
>[] : undefined[]
8+
9+
for (let a2 of a1.someArray)
10+
>a2 : any
11+
>a1.someArray : any
12+
>a1 : any
13+
>someArray : any
14+
15+
doSomething(() => a2);
16+
>doSomething(() => a2) : any
17+
>doSomething : any
18+
>() => a2 : () => any
19+
>a2 : any
20+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// @target: es5
2+
declare let doSomething;
3+
4+
for (let a1 of [])
5+
for (let a2 of a1.someArray)
6+
doSomething(() => a2);

0 commit comments

Comments
 (0)