add generator support let scoping - fixes #178
This commit is contained in:
@@ -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;
|
||||
};
|
||||
|
||||
@@ -21,5 +21,6 @@
|
||||
"SequenceExpression": ["expressions"],
|
||||
"UnaryExpression": ["operator", "argument", "prefix"],
|
||||
"VariableDeclaration": ["kind", "declarations"],
|
||||
"VariableDeclarator": ["id", "init"]
|
||||
"VariableDeclarator": ["id", "init"],
|
||||
"YieldExpression": ["argument", "delegate"]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user