create new lexical environment for the body of converted loop (#12831)

This commit is contained in:
Vladimir Matveev 2016-12-12 10:18:30 -08:00 committed by GitHub
parent a604d84f5c
commit 496a14a021
6 changed files with 128 additions and 3 deletions

View File

@ -2286,14 +2286,19 @@ namespace ts {
}
}
startLexicalEnvironment();
let loopBody = visitNode(node.statement, visitor, isStatement);
const lexicalEnvironment = endLexicalEnvironment();
const currentState = convertedLoopState;
convertedLoopState = outerConvertedLoopState;
if (loopOutParameters.length) {
if (loopOutParameters.length || lexicalEnvironment) {
const statements = isBlock(loopBody) ? (<Block>loopBody).statements.slice() : [loopBody];
copyOutParameters(loopOutParameters, CopyDirection.ToOutParameter, statements);
if (loopOutParameters.length) {
copyOutParameters(loopOutParameters, CopyDirection.ToOutParameter, statements);
}
addRange(statements, lexicalEnvironment)
loopBody = createBlock(statements, /*location*/ undefined, /*multiline*/ true);
}

View File

@ -208,6 +208,7 @@ function foo() {
}
(function () { return b; });
return { value: 100 };
var _a;
};
for (var _c = 0, _d = []; _c < _d.length; _c++) {
var b = _d[_c];
@ -221,6 +222,7 @@ function foo() {
}
}
(function () { return a; });
var _b;
};
var arguments_1 = arguments, x, z, x1, z1;
l0: for (var _i = 0, _a = []; _i < _a.length; _i++) {
@ -238,7 +240,6 @@ function foo() {
use(z);
use(x1);
use(z1);
var _b, _a;
}
function foo2() {
for (var _i = 0, _a = []; _i < _a.length; _i++) {

View File

@ -0,0 +1,31 @@
//// [newLexicalEnvironmentForConvertedLoop.ts]
function baz(x: any) {
return [[x, x]];
}
function foo(set: any) {
for (const [value, i] of baz(set.values)) {
const bar: any = [];
(() => bar);
set.values.push(...[]);
}
};
//// [newLexicalEnvironmentForConvertedLoop.js]
function baz(x) {
return [[x, x]];
}
function foo(set) {
var _loop_1 = function (value, i) {
var bar = [];
(function () { return bar; });
(_a = set.values).push.apply(_a, []);
var _a;
};
for (var _i = 0, _a = baz(set.values); _i < _a.length; _i++) {
var _b = _a[_i], value = _b[0], i = _b[1];
_loop_1(value, i);
}
}
;

View File

@ -0,0 +1,30 @@
=== tests/cases/compiler/newLexicalEnvironmentForConvertedLoop.ts ===
function baz(x: any) {
>baz : Symbol(baz, Decl(newLexicalEnvironmentForConvertedLoop.ts, 0, 0))
>x : Symbol(x, Decl(newLexicalEnvironmentForConvertedLoop.ts, 0, 13))
return [[x, x]];
>x : Symbol(x, Decl(newLexicalEnvironmentForConvertedLoop.ts, 0, 13))
>x : Symbol(x, Decl(newLexicalEnvironmentForConvertedLoop.ts, 0, 13))
}
function foo(set: any) {
>foo : Symbol(foo, Decl(newLexicalEnvironmentForConvertedLoop.ts, 2, 1))
>set : Symbol(set, Decl(newLexicalEnvironmentForConvertedLoop.ts, 4, 13))
for (const [value, i] of baz(set.values)) {
>value : Symbol(value, Decl(newLexicalEnvironmentForConvertedLoop.ts, 5, 14))
>i : Symbol(i, Decl(newLexicalEnvironmentForConvertedLoop.ts, 5, 20))
>baz : Symbol(baz, Decl(newLexicalEnvironmentForConvertedLoop.ts, 0, 0))
>set : Symbol(set, Decl(newLexicalEnvironmentForConvertedLoop.ts, 4, 13))
const bar: any = [];
>bar : Symbol(bar, Decl(newLexicalEnvironmentForConvertedLoop.ts, 6, 9))
(() => bar);
>bar : Symbol(bar, Decl(newLexicalEnvironmentForConvertedLoop.ts, 6, 9))
set.values.push(...[]);
>set : Symbol(set, Decl(newLexicalEnvironmentForConvertedLoop.ts, 4, 13))
}
};

View File

@ -0,0 +1,45 @@
=== tests/cases/compiler/newLexicalEnvironmentForConvertedLoop.ts ===
function baz(x: any) {
>baz : (x: any) => any[][]
>x : any
return [[x, x]];
>[[x, x]] : any[][]
>[x, x] : any[]
>x : any
>x : any
}
function foo(set: any) {
>foo : (set: any) => void
>set : any
for (const [value, i] of baz(set.values)) {
>value : any
>i : any
>baz(set.values) : any[][]
>baz : (x: any) => any[][]
>set.values : any
>set : any
>values : any
const bar: any = [];
>bar : any
>[] : undefined[]
(() => bar);
>(() => bar) : () => any
>() => bar : () => any
>bar : any
set.values.push(...[]);
>set.values.push(...[]) : any
>set.values.push : any
>set.values : any
>set : any
>values : any
>push : any
>...[] : undefined
>[] : undefined[]
}
};

View File

@ -0,0 +1,13 @@
// @target: es5
function baz(x: any) {
return [[x, x]];
}
function foo(set: any) {
for (const [value, i] of baz(set.values)) {
const bar: any = [];
(() => bar);
set.values.push(...[]);
}
};