Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Feb 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 26 additions & 0 deletions packages/babel-plugin-transform-es2015-block-scoping/README.md
Expand Up @@ -14,12 +14,26 @@ npm install --save-dev babel-plugin-transform-es2015-block-scoping

**.babelrc**

Without options:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a new line before the code block, otherwise it won't be formatted correctly on the website.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just copying https://github.com/babel/babel/pull/5194/files like I was asked. :) Updated and cut the error message down a bit.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, sorry. I think GH collapsed that comment and I only saw the newer one. 😞

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@spicyj yeah. I'm sure we had issue with it.

Thanks for the update :)


```json
{
"plugins": ["transform-es2015-block-scoping"]
}
```

With options:

```json
{
"plugins": [
["transform-es2015-block-scoping", {
"throwIfClosureRequired": true
}]
]
}
```

### Via CLI

```sh
Expand All @@ -33,3 +47,15 @@ require("babel-core").transform("code", {
plugins: ["transform-es2015-block-scoping"]
});
```

## Options `throwIfClosureRequired`

In cases such as the following it's impossible to rewrite let/const without adding an additional function and closure while transforming:

```javascript
for (let i = 0; i < 5; i++) {
setTimeout(() => console.log(i), 1);
}
```

In extremely performance-sensitive code, this can be undesirable. If `"throwIfClosureRequired": true` is set, Babel throws when transforming these patterns instead of automatically adding an additional function.
Expand Up @@ -362,6 +362,12 @@ class BlockScoping {
}

wrapClosure() {
if (this.file.opts.throwIfClosureRequired) {
throw this.blockPath.buildCodeFrameError(
"Compiling let/const in this block would add a closure " +
"(throwIfClosureRequired)."
);
}
const block = this.block;

const outsideRefs = this.outsideLetReferences;
Expand Down
@@ -0,0 +1,6 @@
for (let i = 0; i < 5; i++) {
const l = i;
setTimeout(function() {
console.log(l);
}, 1);
}
@@ -0,0 +1,3 @@
{
"throws": "Compiling let/const in this block would add a closure (throwIfClosureRequired)."
}
@@ -0,0 +1,3 @@
function test() {
let foo = "bar";
}
@@ -0,0 +1,3 @@
function test() {
var foo = "bar";
}
@@ -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"]
}
@@ -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;
}
}
}
@@ -0,0 +1,3 @@
{
"throws": "Compiling let/const in this block would add a closure (throwIfClosureRequired)."
}