Transform do-expressions on exit (#5694)
* 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
This commit is contained in:
committed by
Henry Zhu
parent
c474fd48e1
commit
8cd4a62c02
@@ -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());
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
10
packages/babel-plugin-transform-do-expressions/test/fixtures/do-expressions/block-scope.js
vendored
Normal file
10
packages/babel-plugin-transform-do-expressions/test/fixtures/do-expressions/block-scope.js
vendored
Normal file
@@ -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"]
|
||||
}
|
||||
Reference in New Issue
Block a user