Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid adding unnecessary closure for block scoping
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.
- Loading branch information
1 parent
53e694c
commit 3d73fca
Showing
16 changed files
with
215 additions
and
101 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
packages/babel-core/test/fixtures/transformation/misc/regression-2364/actual.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 6 additions & 8 deletions
14
.../babel-plugin-transform-es2015-block-scoping/test/fixtures/general/issue-2174/expected.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,9 @@ | ||
if (true) { | ||
var x; | ||
var foo = function () {}; | ||
|
||
(function () { | ||
function foo() {} | ||
function bar() { | ||
return foo; | ||
} | ||
for (x in {}) {} | ||
})(); | ||
var bar = function () { | ||
return foo; | ||
}; | ||
|
||
for (var x in {}) {} | ||
} |
34 changes: 34 additions & 0 deletions
34
...-plugin-transform-es2015-block-scoping/test/fixtures/general/loops-and-no-loops/actual.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
function foo() { | ||
const x = 5; | ||
console.log(x); | ||
|
||
{ | ||
const x = 7; | ||
setTimeout(() => x, 0); | ||
} | ||
} | ||
|
||
function bar() { | ||
const x = 5; | ||
console.log(x); | ||
|
||
for (let i = 0; i < 7; i++) { | ||
{ | ||
const x = i; | ||
setTimeout(() => x, 0); | ||
} | ||
} | ||
} | ||
|
||
function baz() { | ||
const x = 5; | ||
console.log(x); | ||
|
||
for (let i = 0; i < 7; i++) { | ||
var qux = function qux(y) { | ||
const x = y; | ||
setTimeout(() => x, 0); | ||
}; | ||
qux(i); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...lugin-transform-es2015-block-scoping/test/fixtures/general/loops-and-no-loops/expected.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
function foo() { | ||
var x = 5; | ||
console.log(x); | ||
|
||
{ | ||
var _x = 7; | ||
setTimeout(function () { | ||
return _x; | ||
}, 0); | ||
} | ||
} | ||
|
||
function bar() { | ||
var x = 5; | ||
console.log(x); | ||
|
||
for (var i = 0; i < 7; i++) { | ||
{ | ||
(function () { | ||
var x = i; | ||
setTimeout(function () { | ||
return x; | ||
}, 0); | ||
})(); | ||
} | ||
} | ||
} | ||
|
||
function baz() { | ||
var x = 5; | ||
console.log(x); | ||
|
||
for (var i = 0; i < 7; i++) { | ||
var qux = function qux(y) { | ||
var x = y; | ||
setTimeout(function () { | ||
return x; | ||
}, 0); | ||
}; | ||
qux(i); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...abel-plugin-transform-es2015-block-scoping/test/fixtures/general/sibling-scopes/actual.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
var f1, f2; | ||
{ | ||
let z = 'z1 value'; | ||
f1 = function() { return z; }; | ||
} | ||
{ | ||
let z = 'z2 value'; | ||
f2 = function() { return z; }; | ||
} | ||
f1(); | ||
f2(); |
15 changes: 15 additions & 0 deletions
15
...el-plugin-transform-es2015-block-scoping/test/fixtures/general/sibling-scopes/expected.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
var f1, f2; | ||
{ | ||
var z = 'z1 value'; | ||
f1 = function () { | ||
return z; | ||
}; | ||
} | ||
{ | ||
var _z = 'z2 value'; | ||
f2 = function () { | ||
return _z; | ||
}; | ||
} | ||
f1(); | ||
f2(); |
24 changes: 13 additions & 11 deletions
24
...s/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/superswitch/actual.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,18 @@ | ||
function foo() { | ||
switch (2) { | ||
case 0: { | ||
if (true) { | ||
return; | ||
} | ||
while (true) { | ||
switch (2) { | ||
case 0: { | ||
if (true) { | ||
return; | ||
} | ||
|
||
const stuff = new Map(); | ||
const data = 0; | ||
stuff.forEach(() => { | ||
const d = data; | ||
}); | ||
break; | ||
const stuff = new Map(); | ||
const data = 0; | ||
stuff.forEach(() => { | ||
const d = data; | ||
}); | ||
break; | ||
} | ||
} | ||
} | ||
} |
46 changes: 24 additions & 22 deletions
46
...babel-plugin-transform-es2015-block-scoping/test/fixtures/general/superswitch/expected.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,31 @@ | ||
function foo() { | ||
switch (2) { | ||
case 0: | ||
{ | ||
var _ret = function () { | ||
if (true) { | ||
return { | ||
v: void 0 | ||
}; | ||
} | ||
while (true) { | ||
switch (2) { | ||
case 0: | ||
{ | ||
var _ret = function () { | ||
if (true) { | ||
return { | ||
v: void 0 | ||
}; | ||
} | ||
|
||
var stuff = new Map(); | ||
var data = 0; | ||
stuff.forEach(function () { | ||
var d = data; | ||
}); | ||
return "break"; | ||
}(); | ||
var stuff = new Map(); | ||
var data = 0; | ||
stuff.forEach(function () { | ||
var d = data; | ||
}); | ||
return "break"; | ||
}(); | ||
|
||
switch (_ret) { | ||
case "break": | ||
break; | ||
switch (_ret) { | ||
case "break": | ||
break; | ||
|
||
default: | ||
if (typeof _ret === "object") return _ret.v; | ||
default: | ||
if (typeof _ret === "object") return _ret.v; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
14 changes: 8 additions & 6 deletions
14
...el-plugin-transform-es2015-block-scoping/test/fixtures/general/switch-callbacks/actual.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
function fn() { | ||
switch (true) { | ||
default: | ||
let foo = 4; | ||
if (true) { | ||
let bar = () => foo; | ||
console.log(bar()); | ||
while (true) { | ||
switch (true) { | ||
default: | ||
let foo = 4; | ||
if (true) { | ||
let bar = () => foo; | ||
console.log(bar()); | ||
} | ||
} | ||
} | ||
} |
26 changes: 14 additions & 12 deletions
26
...-plugin-transform-es2015-block-scoping/test/fixtures/general/switch-callbacks/expected.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,16 @@ | ||
function fn() { | ||
(function () { | ||
switch (true) { | ||
default: | ||
var foo = 4; | ||
if (true) { | ||
var bar = function () { | ||
return foo; | ||
}; | ||
console.log(bar()); | ||
} | ||
} | ||
})(); | ||
while (true) { | ||
(function () { | ||
switch (true) { | ||
default: | ||
var foo = 4; | ||
if (true) { | ||
var bar = function () { | ||
return foo; | ||
}; | ||
console.log(bar()); | ||
} | ||
} | ||
})(); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...m-es2015-block-scoping/test/fixtures/throwIfClosureRequired/for-const-closure/expected.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
for (var i = 0; i < 5; i++) { | ||
var l = i; | ||
setTimeout(function () { | ||
console.log(l); | ||
}, 1); | ||
} |
16 changes: 0 additions & 16 deletions
16
...transform-es2015-block-scoping/test/fixtures/throwIfClosureRequired/superswitch/actual.js
This file was deleted.
Oops, something went wrong.
3 changes: 0 additions & 3 deletions
3
...nsform-es2015-block-scoping/test/fixtures/throwIfClosureRequired/superswitch/options.json
This file was deleted.
Oops, something went wrong.
14 changes: 4 additions & 10 deletions
14
...nt-elements/test/fixtures/constant-elements/dont-hoist-block-scoped-variables/expected.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |