Skip to content

Commit

Permalink
Transform do-expressions on exit (babel#5694)
Browse files Browse the repository at this point in the history
* Transform do-expressions on exit

This allows other transforms (notably, block scoping) to do their magic
first, possibly saving closures.

Also fixes a bug with declaring duplicate bindings (de-opts now).

* Use strict in exec test

* lint
  • Loading branch information
jridgewell authored and hzoo committed May 19, 2017
1 parent c474fd4 commit 8cd4a62
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
16 changes: 9 additions & 7 deletions packages/babel-plugin-transform-do-expressions/src/index.js
Expand Up @@ -5,13 +5,15 @@ export default function () {
inherits: syntaxDoExpressions,

visitor: {
DoExpression(path) {
const body = path.node.body.body;
if (body.length) {
path.replaceWithMultiple(body);
} else {
path.replaceWith(path.scope.buildUndefinedNode());
}
DoExpression: {
exit(path) {
const body = path.node.body.body;
if (body.length) {
path.replaceExpressionWithStatements(body);
} else {
path.replaceWith(path.scope.buildUndefinedNode());
}
},
},
},
};
Expand Down
@@ -0,0 +1,10 @@
"use strict";

let a = 1;

(do {
let a = 2;
assert.equal(a, 2);
});
assert.equal(a, 1);

@@ -0,0 +1,7 @@
let a = 1;

(do {
let a = 2;
a;
});

@@ -0,0 +1,5 @@
var _a;

var a = 1;

_a = 2, _a;
@@ -0,0 +1,3 @@
{
"plugins": ["transform-es2015-block-scoping", "transform-do-expressions"]
}

0 comments on commit 8cd4a62

Please sign in to comment.