Merge pull request #3490 from jayphelps/T7324
create new lexical env inside switch statement blocks, fixes #T7324
This commit is contained in:
commit
61b3a6314b
@ -16,7 +16,7 @@ export default function () {
|
|||||||
VariableDeclaration(path, file) {
|
VariableDeclaration(path, file) {
|
||||||
let { node, parent, scope } = path;
|
let { node, parent, scope } = path;
|
||||||
if (!isBlockScoped(node)) return;
|
if (!isBlockScoped(node)) return;
|
||||||
convertBlockScopedToVar(path, parent, scope, true);
|
convertBlockScopedToVar(path, null, parent, scope, true);
|
||||||
|
|
||||||
if (node._tdzThis) {
|
if (node._tdzThis) {
|
||||||
let nodes = [node];
|
let nodes = [node];
|
||||||
@ -51,7 +51,7 @@ export default function () {
|
|||||||
if (replace) path.replaceWith(replace);
|
if (replace) path.replaceWith(replace);
|
||||||
},
|
},
|
||||||
|
|
||||||
"BlockStatement|Program"(path, file) {
|
"BlockStatement|SwitchStatement|Program"(path, file) {
|
||||||
if (!t.isLoop(path.parent)) {
|
if (!t.isLoop(path.parent)) {
|
||||||
let blockScoping = new BlockScoping(null, path, path.parent, path.scope, file);
|
let blockScoping = new BlockScoping(null, path, path.parent, path.scope, file);
|
||||||
blockScoping.run();
|
blockScoping.run();
|
||||||
@ -72,8 +72,10 @@ function isBlockScoped(node) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function convertBlockScopedToVar(path, parent, scope, moveBindingsToParent = false) {
|
function convertBlockScopedToVar(path, node, parent, scope, moveBindingsToParent = false) {
|
||||||
const { node } = path;
|
if (!node) {
|
||||||
|
node = path.node;
|
||||||
|
}
|
||||||
// https://github.com/babel/babel/issues/255
|
// https://github.com/babel/babel/issues/255
|
||||||
if (!t.isFor(parent)) {
|
if (!t.isFor(parent)) {
|
||||||
for (let i = 0; i < node.declarations.length; i++) {
|
for (let i = 0; i < node.declarations.length; i++) {
|
||||||
@ -536,13 +538,30 @@ class BlockScoping {
|
|||||||
if (t.isClassDeclaration(declar) || t.isFunctionDeclaration(declar) || isBlockScoped(declar)) {
|
if (t.isClassDeclaration(declar) || t.isFunctionDeclaration(declar) || isBlockScoped(declar)) {
|
||||||
let declarPath = this.blockPath.get("body")[i];
|
let declarPath = this.blockPath.get("body")[i];
|
||||||
if (isBlockScoped(declar)) {
|
if (isBlockScoped(declar)) {
|
||||||
convertBlockScopedToVar(declarPath, block, this.scope);
|
convertBlockScopedToVar(declarPath, null, block, this.scope);
|
||||||
}
|
}
|
||||||
declarators = declarators.concat(declar.declarations || declar);
|
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++) {
|
for (let i = 0; i < declarators.length; i++) {
|
||||||
let declar = declarators[i];
|
let declar = declarators[i];
|
||||||
|
|||||||
@ -1,11 +1,27 @@
|
|||||||
let a = true;
|
let a = true;
|
||||||
let b = false;
|
let b = false;
|
||||||
|
class e {}
|
||||||
|
function f() {}
|
||||||
|
|
||||||
switch (a) {
|
switch (a) {
|
||||||
case true:
|
case true:
|
||||||
let c = 2;
|
let c = 2;
|
||||||
|
let foo = true;
|
||||||
break;
|
break;
|
||||||
case false:
|
case false:
|
||||||
let d = 3;
|
let d = 3;
|
||||||
|
foo = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
switch (true) {
|
||||||
|
case true:
|
||||||
|
let a = false;
|
||||||
|
let b = true;
|
||||||
|
let c = 4;
|
||||||
|
let d = 5;
|
||||||
|
|
||||||
|
case false:
|
||||||
|
class e {}
|
||||||
|
function f() {}
|
||||||
|
}
|
||||||
|
|||||||
@ -1,11 +1,29 @@
|
|||||||
var a = true;
|
var a = true;
|
||||||
var b = false;
|
var b = false;
|
||||||
|
class e {}
|
||||||
|
function f() {}
|
||||||
|
|
||||||
switch (a) {
|
switch (a) {
|
||||||
case true:
|
case true:
|
||||||
var c = 2;
|
var c = 2;
|
||||||
|
var foo = true;
|
||||||
break;
|
break;
|
||||||
case false:
|
case false:
|
||||||
var d = 3;
|
var d = 3;
|
||||||
|
foo = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
switch (true) {
|
||||||
|
case true:
|
||||||
|
var _a = false;
|
||||||
|
var _b = true;
|
||||||
|
var _c = 4;
|
||||||
|
var _d = 5;
|
||||||
|
|
||||||
|
case false:
|
||||||
|
class _e {}
|
||||||
|
|
||||||
|
var _f = function () {};
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user