Fix block scoping transform for declarations in labeled statements (#4669)
* Fix block scoping transform for declarations in labeled statements (#4122) * DRY block-scoping
This commit is contained in:
committed by
Daniel Tschinder
parent
a62905c61d
commit
7a7704fea0
@@ -488,17 +488,24 @@ class BlockScoping {
|
||||
}
|
||||
}
|
||||
|
||||
const addDeclarationsFromChild = (path, node) => {
|
||||
node = node || path.node;
|
||||
if (t.isClassDeclaration(node) || t.isFunctionDeclaration(node) || isBlockScoped(node)) {
|
||||
if (isBlockScoped(node)) {
|
||||
convertBlockScopedToVar(path, node, block, this.scope);
|
||||
}
|
||||
declarators = declarators.concat(node.declarations || node);
|
||||
}
|
||||
if (t.isLabeledStatement(node)) {
|
||||
addDeclarationsFromChild(path.get("body"), node.body);
|
||||
}
|
||||
};
|
||||
|
||||
//
|
||||
if (block.body) {
|
||||
for (let i = 0; i < block.body.length; i++) {
|
||||
let declar = block.body[i];
|
||||
if (t.isClassDeclaration(declar) || t.isFunctionDeclaration(declar) || isBlockScoped(declar)) {
|
||||
let declarPath = this.blockPath.get("body")[i];
|
||||
if (isBlockScoped(declar)) {
|
||||
convertBlockScopedToVar(declarPath, null, block, this.scope);
|
||||
}
|
||||
declarators = declarators.concat(declar.declarations || declar);
|
||||
}
|
||||
let declarPath = this.blockPath.get("body")[i];
|
||||
addDeclarationsFromChild(declarPath);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -507,14 +514,9 @@ class BlockScoping {
|
||||
let consequents = block.cases[i].consequent;
|
||||
|
||||
for (let j = 0; j < consequents.length; j++) {
|
||||
let declarPath = this.blockPath.get("cases")[i];
|
||||
let declar = consequents[j];
|
||||
if (t.isClassDeclaration(declar) || t.isFunctionDeclaration(declar) || isBlockScoped(declar)) {
|
||||
let declarPath = this.blockPath.get("cases")[i];
|
||||
if (isBlockScoped(declar)) {
|
||||
convertBlockScopedToVar(declarPath, declar, block, this.scope);
|
||||
}
|
||||
declarators = declarators.concat(declar.declarations || declar);
|
||||
}
|
||||
addDeclarationsFromChild(declarPath, declar);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
9
packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/label/actual.js
vendored
Normal file
9
packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/label/actual.js
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
let x, y;
|
||||
{
|
||||
a: let x;
|
||||
let y;
|
||||
}
|
||||
|
||||
switch (0) {
|
||||
case 0: a: let x=0;
|
||||
}
|
||||
11
packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/label/expected.js
vendored
Normal file
11
packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/label/expected.js
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
var x = void 0,
|
||||
y = void 0;
|
||||
{
|
||||
a: var _x = void 0;
|
||||
var _y = void 0;
|
||||
}
|
||||
|
||||
switch (0) {
|
||||
case 0:
|
||||
a: var _x2 = 0;
|
||||
}
|
||||
Reference in New Issue
Block a user