Avoid adding unnecessary closure for block scoping (#5246)

When you write

```
for (const x of l) {
  setTimeout(() => x);
}
```

we need to add a closure because the variable is meant to be block-scoped and recreated each time the block runs. We do this.

However, we also add the closure when no loop is present. This isn't necessary, because if no loop is present then each piece of code runs at most once. I changed the transform to only add a closure if a variable is referenced from within a loop.
This commit is contained in:
Ben Alpert
2017-02-13 13:46:00 -08:00
committed by Logan Smyth
parent 2985597d40
commit 14d3c2e256
16 changed files with 217 additions and 103 deletions

View File

@@ -1,17 +1,11 @@
function render(flag) {
if (flag) {
var _ret = function () {
var bar = "bar";
var bar = "bar";
[].map(() => bar);
[].map(() => bar);
return {
v: <foo bar={bar} />
};
}();
if (typeof _ret === "object") return _ret.v;
return <foo bar={bar} />;
}
return null;
}
}