diff --git a/packages/babel-plugin-transform-es2015-block-scoping/src/index.js b/packages/babel-plugin-transform-es2015-block-scoping/src/index.js index 2e26b9ab1d..6349483192 100644 --- a/packages/babel-plugin-transform-es2015-block-scoping/src/index.js +++ b/packages/babel-plugin-transform-es2015-block-scoping/src/index.js @@ -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]; diff --git a/packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/switch/actual.js b/packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/switch/actual.js index 32ec8488aa..7eeaba85ae 100644 --- a/packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/switch/actual.js +++ b/packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/switch/actual.js @@ -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() {} +} diff --git a/packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/switch/expected.js b/packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/switch/expected.js index 6f3fd7fdfd..5678afcb5f 100644 --- a/packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/switch/expected.js +++ b/packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/switch/expected.js @@ -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 () {}; + +}