add generator support let scoping - fixes #178

This commit is contained in:
Sebastian McKenzie
2014-11-17 04:09:48 +11:00
parent e6baac1003
commit d091793077
2 changed files with 29 additions and 16 deletions

View File

@@ -128,6 +128,12 @@ LetScoping.prototype.run = function () {
var call = t.callExpression(fn, params);
var ret = t.identifier(this.file.generateUid("ret", this.scope));
var hasYield = traverse.hasType(fn.body, "YieldExpression", t.FUNCTION_TYPES);
if (hasYield) {
fn.generator = true;
call = t.yieldExpression(call, true);
}
this.build(ret, call);
};
@@ -245,28 +251,34 @@ LetScoping.prototype.checkFor = function () {
hasBreak: false,
};
if (this.forParent) {
traverse(this.block, function (node) {
var replace;
var forParent = this.forParent;
if (t.isFunction(node) || t.isFor(node)) {
return false;
} else if (t.isBreakStatement(node) && !node.label) {
traverse(this.block, function (node) {
var replace;
if (t.isFunction(node) || t.isFor(node)) {
return false;
}
if (forParent && !node.label) {
if (t.isBreakStatement(node)) {
has.hasBreak = true;
replace = t.returnStatement(t.literal("break"));
} else if (t.isContinueStatement(node) && !node.label) {
} else if (t.isContinueStatement(node)) {
has.hasContinue = true;
replace = t.returnStatement(t.literal("continue"));
} else if (t.isReturnStatement(node)) {
has.hasReturn = true;
replace = t.returnStatement(t.objectExpression([
t.property("init", t.identifier("v"), node.argument || t.identifier("undefined"))
]));
}
}
if (replace) return t.inherits(replace, node);
});
}
if (t.isReturnStatement(node)) {
has.hasReturn = true;
replace = t.returnStatement(t.objectExpression([
t.property("init", t.identifier("v"), node.argument || t.identifier("undefined"))
]));
}
if (replace) return t.inherits(replace, node);
});
return has;
};

View File

@@ -21,5 +21,6 @@
"SequenceExpression": ["expressions"],
"UnaryExpression": ["operator", "argument", "prefix"],
"VariableDeclaration": ["kind", "declarations"],
"VariableDeclarator": ["id", "init"]
"VariableDeclarator": ["id", "init"],
"YieldExpression": ["argument", "delegate"]
}