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 3916769c47..0eaf8a2666 100644 --- a/packages/babel-plugin-transform-es2015-block-scoping/src/index.js +++ b/packages/babel-plugin-transform-es2015-block-scoping/src/index.js @@ -240,9 +240,6 @@ const loopVisitor = { // they don't refer to the actual loop we're scopifying if (state.ignoreLabeless) return; - // - if (state.inSwitchCase) return; - // break statements mean something different in this context if (t.isBreakStatement(node) && t.isSwitchCase(parent)) return; } diff --git a/packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/label/actual.js b/packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/label/actual.js index 8d657e984d..b962ff26a3 100644 --- a/packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/label/actual.js +++ b/packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/label/actual.js @@ -7,3 +7,16 @@ let x, y; switch (0) { case 0: a: let x=0; } + +// it should break from inside of switch statements using labels + +loop: +for (var i = 0; i < 10; i++) { + const z = 3; // force loop function + () => z; + switch(true) { + case true: + break loop; + } +} + diff --git a/packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/label/expected.js b/packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/label/expected.js index a89cb68476..acb9d87d7d 100644 --- a/packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/label/expected.js +++ b/packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/label/expected.js @@ -9,3 +9,22 @@ switch (0) { case 0: a: var _x2 = 0; } + +// it should break from inside of switch statements using labels + +var _loop = function () { + var z = 3; // force loop function + (function () { + return z; + }); + switch (true) { + case true: + return "break|loop"; + } +}; + +loop: for (var i = 0; i < 10; i++) { + var _ret = _loop(); + + if (_ret === "break|loop") break loop; +} \ No newline at end of file diff --git a/packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/switch-inside-loop/actual.js b/packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/switch-inside-loop/actual.js new file mode 100644 index 0000000000..27e7187d5f --- /dev/null +++ b/packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/switch-inside-loop/actual.js @@ -0,0 +1,48 @@ +// it shouldn't break on a case-break statement +var i; +for (i = 0; i < 10; i++) { + switch (i) { + case 1: + break; + } + + const z = 3; // to force the plugin to convert to loop function call + () => z; +} + +assert.equal(i, 10); + +// it should continue on continue statements within switch +var j = 0; +for (i = 0; i < 10; i++) { + switch (i) { + case 0: + continue; + } + j++; + + const z = 3; + () => z; +} + +assert.equal(j, 9); + +// it should work with loops nested within switch +j = 0; +for (i = 0; i < 10; i++) { + switch (i) { + case 0: + for (var k = 0; k < 10; k++) { + const z = 3; + () => z; + j++; + break; + } + break; + } + + const z = 3; + () => z; +} + +assert.equal(j, 1); diff --git a/packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/switch-inside-loop/exec.js b/packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/switch-inside-loop/exec.js new file mode 100644 index 0000000000..27e7187d5f --- /dev/null +++ b/packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/switch-inside-loop/exec.js @@ -0,0 +1,48 @@ +// it shouldn't break on a case-break statement +var i; +for (i = 0; i < 10; i++) { + switch (i) { + case 1: + break; + } + + const z = 3; // to force the plugin to convert to loop function call + () => z; +} + +assert.equal(i, 10); + +// it should continue on continue statements within switch +var j = 0; +for (i = 0; i < 10; i++) { + switch (i) { + case 0: + continue; + } + j++; + + const z = 3; + () => z; +} + +assert.equal(j, 9); + +// it should work with loops nested within switch +j = 0; +for (i = 0; i < 10; i++) { + switch (i) { + case 0: + for (var k = 0; k < 10; k++) { + const z = 3; + () => z; + j++; + break; + } + break; + } + + const z = 3; + () => z; +} + +assert.equal(j, 1); diff --git a/packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/switch-inside-loop/expected.js b/packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/switch-inside-loop/expected.js new file mode 100644 index 0000000000..4c0cea11b8 --- /dev/null +++ b/packages/babel-plugin-transform-es2015-block-scoping/test/fixtures/general/switch-inside-loop/expected.js @@ -0,0 +1,81 @@ +// it shouldn't break on a case-break statement +var i; + +var _loop = function () { + switch (i) { + case 1: + break; + } + + var z = 3; // to force the plugin to convert to loop function call + (function () { + return z; + }); +}; + +for (i = 0; i < 10; i++) { + _loop(); +} + +assert.equal(i, 10); + +// it should continue on continue statements within switch +var j = 0; + +var _loop2 = function () { + switch (i) { + case 0: + return "continue"; + } + j++; + + var z = 3; + (function () { + return z; + }); +}; + +for (i = 0; i < 10; i++) { + var _ret = _loop2(); + + if (_ret === "continue") continue; +} + +assert.equal(j, 9); + +// it should work with loops nested within switch +j = 0; + +var _loop3 = function () { + switch (i) { + case 0: + var _loop4 = function () { + var z = 3; + (function () { + return z; + }); + j++; + return "break"; + }; + + for (k = 0; k < 10; k++) { + var _ret2 = _loop4(); + + if (_ret2 === "break") break; + } + break; + } + + var z = 3; + (function () { + return z; + }); +}; + +for (i = 0; i < 10; i++) { + var k; + + _loop3(); +} + +assert.equal(j, 1); \ No newline at end of file