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:
Moti Zilberman
2016-10-05 22:47:21 +03:00
committed by Daniel Tschinder
parent a62905c61d
commit 7a7704fea0
3 changed files with 37 additions and 15 deletions

View File

@@ -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);
}
}
}

View File

@@ -0,0 +1,9 @@
let x, y;
{
a: let x;
let y;
}
switch (0) {
case 0: a: let x=0;
}

View 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;
}