Merge pull request #3490 from jayphelps/T7324

create new lexical env inside switch statement blocks, fixes #T7324
This commit is contained in:
Logan Smyth 2016-05-30 14:42:29 -07:00
commit 61b3a6314b
3 changed files with 58 additions and 5 deletions

View File

@ -16,7 +16,7 @@ export default function () {
VariableDeclaration(path, file) {
let { node, parent, scope } = path;
if (!isBlockScoped(node)) return;
convertBlockScopedToVar(path, parent, scope, true);
convertBlockScopedToVar(path, null, parent, scope, true);
if (node._tdzThis) {
let nodes = [node];
@ -51,7 +51,7 @@ export default function () {
if (replace) path.replaceWith(replace);
},
"BlockStatement|Program"(path, file) {
"BlockStatement|SwitchStatement|Program"(path, file) {
if (!t.isLoop(path.parent)) {
let blockScoping = new BlockScoping(null, path, path.parent, path.scope, file);
blockScoping.run();
@ -72,8 +72,10 @@ function isBlockScoped(node) {
return true;
}
function convertBlockScopedToVar(path, parent, scope, moveBindingsToParent = false) {
const { node } = path;
function convertBlockScopedToVar(path, node, parent, scope, moveBindingsToParent = false) {
if (!node) {
node = path.node;
}
// https://github.com/babel/babel/issues/255
if (!t.isFor(parent)) {
for (let i = 0; i < node.declarations.length; i++) {
@ -536,13 +538,30 @@ class BlockScoping {
if (t.isClassDeclaration(declar) || t.isFunctionDeclaration(declar) || isBlockScoped(declar)) {
let declarPath = this.blockPath.get("body")[i];
if (isBlockScoped(declar)) {
convertBlockScopedToVar(declarPath, block, this.scope);
convertBlockScopedToVar(declarPath, null, block, this.scope);
}
declarators = declarators.concat(declar.declarations || declar);
}
}
}
if (block.cases) {
for (let i = 0; i < block.cases.length; i++) {
let consequents = block.cases[i].consequent;
for (let j = 0; j < consequents.length; j++) {
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);
}
}
}
}
//
for (let i = 0; i < declarators.length; i++) {
let declar = declarators[i];

View File

@ -1,11 +1,27 @@
let a = true;
let b = false;
class e {}
function f() {}
switch (a) {
case true:
let c = 2;
let foo = true;
break;
case false:
let d = 3;
foo = false;
break;
}
switch (true) {
case true:
let a = false;
let b = true;
let c = 4;
let d = 5;
case false:
class e {}
function f() {}
}

View File

@ -1,11 +1,29 @@
var a = true;
var b = false;
class e {}
function f() {}
switch (a) {
case true:
var c = 2;
var foo = true;
break;
case false:
var d = 3;
foo = false;
break;
}
switch (true) {
case true:
var _a = false;
var _b = true;
var _c = 4;
var _d = 5;
case false:
class _e {}
var _f = function () {};
}