Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix break/continue when switch is nested inside loop #11802

Merged
merged 2 commits into from Jul 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 11 additions & 40 deletions packages/babel-plugin-transform-block-scoping/src/index.js
Expand Up @@ -844,55 +844,26 @@ class BlockScoping {

buildHas(ret: string) {
const body = this.body;

let retCheck;
const has = this.has;
const cases = [];

if (has.hasReturn) {
// typeof ret === "object"
retCheck = buildRetCheck({
RETURN: t.identifier(ret),
});
}

if (has.hasBreakContinue) {
for (const key of Object.keys(has.map)) {
cases.push(t.switchCase(t.stringLiteral(key), [has.map[key]]));
}

if (has.hasReturn) {
cases.push(t.switchCase(null, [retCheck]));
}

if (cases.length === 1) {
const single = cases[0];
body.push(
t.ifStatement(
t.binaryExpression("===", t.identifier(ret), single.test),
single.consequent[0],
t.binaryExpression("===", t.identifier(ret), t.stringLiteral(key)),
has.map[key],
),
);
} else {
if (this.loop) {
// https://github.com/babel/babel/issues/998
for (let i = 0; i < cases.length; i++) {
const caseConsequent = cases[i].consequent[0];
if (t.isBreakStatement(caseConsequent) && !caseConsequent.label) {
if (!this.loopLabel) {
this.loopLabel = this.scope.generateUidIdentifier("loop");
}
caseConsequent.label = t.cloneNode(this.loopLabel);
}
}
}

body.push(t.switchStatement(t.identifier(ret), cases));
}
} else {
if (has.hasReturn) {
body.push(retCheck);
}
}

// typeof ret === "object"
if (has.hasReturn) {
body.push(
buildRetCheck({
RETURN: t.identifier(ret),
}),
);
}
}
}
@@ -0,0 +1,15 @@
expect(() => {
for (const a of [1]) {
switch (true) {
case true: {
const b = 1;
() => b;
if (true) break;
continue;
}
case false: {
throw new Error("unreachable");
}
}
}
}).not.toThrow();
@@ -0,0 +1,13 @@
for (const a of [1]) {
switch (true) {
case true: {
const b = 1;
() => b;
if (true) break;
continue;
}
case false: {
throw new Error("unreachable");
}
}
}
@@ -0,0 +1,25 @@
for (var a of [1]) {
switch (true) {
case true:
{
var _ret = function () {
var b = 1;

(function () {
return b;
});

if (true) return "break";
return "continue";
}();

if (_ret === "break") break;
if (_ret === "continue") continue;
}

case false:
{
throw new Error("unreachable");
}
}
}
@@ -1,5 +1,5 @@
(function () {
var _loop2 = function (i) {
var _loop = function (i) {
fns.push(function () {
return i;
});
Expand All @@ -15,18 +15,11 @@
}
};

_loop: for (var i in nums) {
var _ret = _loop2(i);
for (var i in nums) {
var _ret = _loop(i);

switch (_ret) {
case "continue":
continue;

case "break":
break _loop;

default:
if (typeof _ret === "object") return _ret.v;
}
if (_ret === "continue") continue;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not related to this PR: the string literal here is not minimizer-friendly. We should revise

function loopNodeTo(node) {
if (t.isBreakStatement(node)) {
return "break";
} else if (t.isContinueStatement(node)) {
return "continue";
}
}

later, e.g. change it to "b"/"c".

if (_ret === "break") break;
if (typeof _ret === "object") return _ret.v;
}
})();
Expand Up @@ -18,13 +18,8 @@ function foo() {
return "break";
}();

switch (_ret) {
case "break":
break;

default:
if (typeof _ret === "object") return _ret.v;
}
if (_ret === "break") break;
if (typeof _ret === "object") return _ret.v;
}
}
}
Expand Down