Skip to content

Commit

Permalink
Fix block scoping transform for declarations in labeled statements (#…
Browse files Browse the repository at this point in the history
…4669)

* Fix block scoping transform for declarations in labeled statements (#4122)

* DRY block-scoping
  • Loading branch information
motiz88 authored and danez committed Oct 5, 2016
1 parent a62905c commit 7a7704f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
32 changes: 17 additions & 15 deletions packages/babel-plugin-transform-es2015-block-scoping/src/index.js
Expand Up @@ -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);
}
}

Expand All @@ -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);
}
}
}
Expand Down
@@ -0,0 +1,9 @@
let x, y;
{
a: let x;
let y;
}

switch (0) {
case 0: a: let x=0;
}
@@ -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;
}

0 comments on commit 7a7704f

Please sign in to comment.