Skip to content

Commit

Permalink
Minor improvements to block-scoping/tdz (#6782)
Browse files Browse the repository at this point in the history
* Add test case for simple reference in tdz

* Add more examples from old issues as test cases

* Fix two testcases by excluding function declarations from being tdz checked

* Document the  option for block-scoping

* Add test cases with destructuring assignments

* Remove failing test cases

* [skip ci] Include type and default value for options
  • Loading branch information
maurobringolf authored and loganfsmyth committed Jan 9, 2018
1 parent e9ed687 commit 0a9f136
Show file tree
Hide file tree
Showing 16 changed files with 57 additions and 1 deletion.
17 changes: 16 additions & 1 deletion packages/babel-plugin-transform-block-scoping/README.md
Expand Up @@ -70,7 +70,10 @@ require("@babel/core").transform("code", {
});
```

## Options `throwIfClosureRequired`
## Options

### `throwIfClosureRequired`
`boolean`, defaults to `false`.

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

Expand All @@ -81,3 +84,15 @@ for (let i = 0; i < 5; i++) {
```

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.

### `tdz`
`boolean`, defaults to `false`.

By default this plugin will ignore the *temporal dead zone (TDZ)* for block-scoped variables. The following code will **not throw an error when transpiled with Babel, which is not spec compliant**:

```javascript
i
let i;
```

If you need these errors you can tell Babel to try and find them by setting `"tdz": true` for this plugin. However, the current implementation might not get all edge cases right and its best to just avoid code like this in the first place.
2 changes: 2 additions & 0 deletions packages/babel-plugin-transform-block-scoping/src/tdz.js
Expand Up @@ -38,6 +38,8 @@ export const visitor = {

const bindingPath = scope.getBinding(node.name).path;

if (bindingPath.isFunctionDeclaration()) return;

const status = getTDZStatus(path, bindingPath);
if (status === "inside") return;

Expand Down
@@ -0,0 +1,3 @@
f();

const f = function f() {}
@@ -0,0 +1,3 @@
{
"throws": "f is not defined - temporal dead zone"
}
@@ -0,0 +1 @@
let {b: d }= {聽d }
@@ -0,0 +1,3 @@
{
"throws": "d is not defined - temporal dead zone"
}
@@ -0,0 +1,5 @@
function f() {
x;
}
let x;
f();
@@ -0,0 +1,3 @@
f();

function f() {}
@@ -0,0 +1,3 @@
x = 3;

var x;
@@ -0,0 +1,3 @@
{
"plugins": [["transform-block-scoping",{"tdz": true}]]
}
@@ -0,0 +1 @@
let x = x;
@@ -0,0 +1,3 @@
{
"throws": "x is not defined - temporal dead zone"
}
@@ -0,0 +1,3 @@
var a = 5;
if (a){ console.log(a); let a = 2; }
console.log(a);
@@ -0,0 +1,3 @@
{
"throws": "a is not defined - temporal dead zone"
}
@@ -0,0 +1,2 @@
i
let i
@@ -0,0 +1,3 @@
{
"throws": "i is not defined - temporal dead zone"
}

0 comments on commit 0a9f136

Please sign in to comment.