nicer let-scoping switch

This commit is contained in:
Sebastian McKenzie
2014-11-11 15:25:37 +11:00
parent f9d14fa2ed
commit 7d0dae129c

View File

@@ -133,7 +133,7 @@ var checkFor = function (forParent, block) {
} else if (t.isReturnStatement(node)) {
has.hasReturn = true;
replace = t.returnStatement(t.objectExpression([
t.property("init", t.identifier("v"), node.argument)
t.property("init", t.identifier("v"), node.argument || t.identifier("undefined"))
]));
}
@@ -286,16 +286,13 @@ var run = function (forParent, block, parent, file, scope) {
var retCheck;
var cases = [];
if (has.hasReturn) {
// typeof ret === "object"
retCheck = util.template("let-scoping-return", {
RETURN: ret,
});
// there's no `break` or `continue` so we can just push in the `if`
if (!has.hasBreak && !has.hasContinue) {
body.push(retCheck);
}
}
if (has.hasBreak || has.hasContinue) {
@@ -303,8 +300,6 @@ var run = function (forParent, block, parent, file, scope) {
// need to be able to access it
var label = forParent.label = forParent.label || t.identifier(file.generateUid("loop", scope));
var cases = [];
if (has.hasBreak) {
cases.push(t.switchCase(t.literal("break"), [t.breakStatement(label)]));
}
@@ -317,7 +312,17 @@ var run = function (forParent, block, parent, file, scope) {
cases.push(t.switchCase(null, [retCheck]));
}
body.push(t.switchStatement(ret, cases));
if (cases.length === 1) {
var single = cases[0];
body.push(t.ifStatement(
t.binaryExpression("===", ret, single.test),
single.consequent[0]
));
} else {
body.push(t.switchStatement(ret, cases));
}
} else {
if (has.hasReturn) body.push(retCheck);
}
} else {
body.push(t.expressionStatement(call));