Properly convert continue statements in switch

Ignore just non-label break statements in a switch, and allow continue
statments and lablled break statements.

Fixes #5725
This commit is contained in:
Peeyush Kushwaha
2017-05-25 13:49:29 +05:30
parent cfe0a84e2b
commit 72d83acc07
6 changed files with 209 additions and 3 deletions

View File

@@ -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;
}

View File

@@ -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;
}
}

View File

@@ -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;
}

View File

@@ -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);

View File

@@ -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);

View File

@@ -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);