Skip to content

Commit 496a14a

Browse files
authored
create new lexical environment for the body of converted loop (#12831)
1 parent a604d84 commit 496a14a

6 files changed

+128
-3
lines changed

src/compiler/transformers/es2015.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2286,14 +2286,19 @@ namespace ts {
22862286
}
22872287
}
22882288

2289+
startLexicalEnvironment();
22892290
let loopBody = visitNode(node.statement, visitor, isStatement);
2291+
const lexicalEnvironment = endLexicalEnvironment();
22902292

22912293
const currentState = convertedLoopState;
22922294
convertedLoopState = outerConvertedLoopState;
22932295

2294-
if (loopOutParameters.length) {
2296+
if (loopOutParameters.length || lexicalEnvironment) {
22952297
const statements = isBlock(loopBody) ? (<Block>loopBody).statements.slice() : [loopBody];
2296-
copyOutParameters(loopOutParameters, CopyDirection.ToOutParameter, statements);
2298+
if (loopOutParameters.length) {
2299+
copyOutParameters(loopOutParameters, CopyDirection.ToOutParameter, statements);
2300+
}
2301+
addRange(statements, lexicalEnvironment)
22972302
loopBody = createBlock(statements, /*location*/ undefined, /*multiline*/ true);
22982303
}
22992304

tests/baselines/reference/capturedLetConstInLoop9.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ function foo() {
208208
}
209209
(function () { return b; });
210210
return { value: 100 };
211+
var _a;
211212
};
212213
for (var _c = 0, _d = []; _c < _d.length; _c++) {
213214
var b = _d[_c];
@@ -221,6 +222,7 @@ function foo() {
221222
}
222223
}
223224
(function () { return a; });
225+
var _b;
224226
};
225227
var arguments_1 = arguments, x, z, x1, z1;
226228
l0: for (var _i = 0, _a = []; _i < _a.length; _i++) {
@@ -238,7 +240,6 @@ function foo() {
238240
use(z);
239241
use(x1);
240242
use(z1);
241-
var _b, _a;
242243
}
243244
function foo2() {
244245
for (var _i = 0, _a = []; _i < _a.length; _i++) {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//// [newLexicalEnvironmentForConvertedLoop.ts]
2+
function baz(x: any) {
3+
return [[x, x]];
4+
}
5+
6+
function foo(set: any) {
7+
for (const [value, i] of baz(set.values)) {
8+
const bar: any = [];
9+
(() => bar);
10+
11+
set.values.push(...[]);
12+
}
13+
};
14+
15+
//// [newLexicalEnvironmentForConvertedLoop.js]
16+
function baz(x) {
17+
return [[x, x]];
18+
}
19+
function foo(set) {
20+
var _loop_1 = function (value, i) {
21+
var bar = [];
22+
(function () { return bar; });
23+
(_a = set.values).push.apply(_a, []);
24+
var _a;
25+
};
26+
for (var _i = 0, _a = baz(set.values); _i < _a.length; _i++) {
27+
var _b = _a[_i], value = _b[0], i = _b[1];
28+
_loop_1(value, i);
29+
}
30+
}
31+
;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
=== tests/cases/compiler/newLexicalEnvironmentForConvertedLoop.ts ===
2+
function baz(x: any) {
3+
>baz : Symbol(baz, Decl(newLexicalEnvironmentForConvertedLoop.ts, 0, 0))
4+
>x : Symbol(x, Decl(newLexicalEnvironmentForConvertedLoop.ts, 0, 13))
5+
6+
return [[x, x]];
7+
>x : Symbol(x, Decl(newLexicalEnvironmentForConvertedLoop.ts, 0, 13))
8+
>x : Symbol(x, Decl(newLexicalEnvironmentForConvertedLoop.ts, 0, 13))
9+
}
10+
11+
function foo(set: any) {
12+
>foo : Symbol(foo, Decl(newLexicalEnvironmentForConvertedLoop.ts, 2, 1))
13+
>set : Symbol(set, Decl(newLexicalEnvironmentForConvertedLoop.ts, 4, 13))
14+
15+
for (const [value, i] of baz(set.values)) {
16+
>value : Symbol(value, Decl(newLexicalEnvironmentForConvertedLoop.ts, 5, 14))
17+
>i : Symbol(i, Decl(newLexicalEnvironmentForConvertedLoop.ts, 5, 20))
18+
>baz : Symbol(baz, Decl(newLexicalEnvironmentForConvertedLoop.ts, 0, 0))
19+
>set : Symbol(set, Decl(newLexicalEnvironmentForConvertedLoop.ts, 4, 13))
20+
21+
const bar: any = [];
22+
>bar : Symbol(bar, Decl(newLexicalEnvironmentForConvertedLoop.ts, 6, 9))
23+
24+
(() => bar);
25+
>bar : Symbol(bar, Decl(newLexicalEnvironmentForConvertedLoop.ts, 6, 9))
26+
27+
set.values.push(...[]);
28+
>set : Symbol(set, Decl(newLexicalEnvironmentForConvertedLoop.ts, 4, 13))
29+
}
30+
};
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
=== tests/cases/compiler/newLexicalEnvironmentForConvertedLoop.ts ===
2+
function baz(x: any) {
3+
>baz : (x: any) => any[][]
4+
>x : any
5+
6+
return [[x, x]];
7+
>[[x, x]] : any[][]
8+
>[x, x] : any[]
9+
>x : any
10+
>x : any
11+
}
12+
13+
function foo(set: any) {
14+
>foo : (set: any) => void
15+
>set : any
16+
17+
for (const [value, i] of baz(set.values)) {
18+
>value : any
19+
>i : any
20+
>baz(set.values) : any[][]
21+
>baz : (x: any) => any[][]
22+
>set.values : any
23+
>set : any
24+
>values : any
25+
26+
const bar: any = [];
27+
>bar : any
28+
>[] : undefined[]
29+
30+
(() => bar);
31+
>(() => bar) : () => any
32+
>() => bar : () => any
33+
>bar : any
34+
35+
set.values.push(...[]);
36+
>set.values.push(...[]) : any
37+
>set.values.push : any
38+
>set.values : any
39+
>set : any
40+
>values : any
41+
>push : any
42+
>...[] : undefined
43+
>[] : undefined[]
44+
}
45+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// @target: es5
2+
function baz(x: any) {
3+
return [[x, x]];
4+
}
5+
6+
function foo(set: any) {
7+
for (const [value, i] of baz(set.values)) {
8+
const bar: any = [];
9+
(() => bar);
10+
11+
set.values.push(...[]);
12+
}
13+
};

0 commit comments

Comments
 (0)