nicer let-scoping switch
This commit is contained in:
@@ -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));
|
||||
|
||||
Reference in New Issue
Block a user