Add option to block-scoping to slow on throw code (#5236)

The let/const plugin can add closures where you don't expect them. This is undesirable in some perf-sensitive projects (ex: React). I added an option that throws whenever the plugin adds a function (as opposed to simply renaming variables when converting to var).
This commit is contained in:
Ben Alpert
2017-02-06 10:50:56 -08:00
committed by Henry Zhu
parent 6ee7bf6df5
commit ff8a10e52f
9 changed files with 69 additions and 0 deletions

View File

@@ -0,0 +1,6 @@
for (let i = 0; i < 5; i++) {
const l = i;
setTimeout(function() {
console.log(l);
}, 1);
}

View File

@@ -0,0 +1,3 @@
{
"throws": "Compiling let/const in this block would add a closure (throwIfClosureRequired)."
}

View File

@@ -0,0 +1,3 @@
function test() {
let foo = "bar";
}

View File

@@ -0,0 +1,3 @@
function test() {
var foo = "bar";
}

View File

@@ -0,0 +1,3 @@
{
"plugins": [["transform-es2015-block-scoping", { "throwIfClosureRequired": true }], "syntax-jsx", "transform-react-jsx", "transform-es2015-block-scoped-functions", "transform-es2015-arrow-functions"]
}

View File

@@ -0,0 +1,16 @@
function foo() {
switch (2) {
case 0: {
if (true) {
return;
}
const stuff = new Map();
const data = 0;
stuff.forEach(() => {
const d = data;
});
break;
}
}
}

View File

@@ -0,0 +1,3 @@
{
"throws": "Compiling let/const in this block would add a closure (throwIfClosureRequired)."
}