mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-05-03 03:00:14 -04:00
using 'yield' automatically turns functions into generators
This commit is contained in:
@@ -1798,6 +1798,14 @@
|
||||
this.params = params || [];
|
||||
this.body = body || new Block;
|
||||
this.bound = tag === 'boundfunc';
|
||||
this.isGenerator = false;
|
||||
this.body.traverseChildren(false, (function(_this) {
|
||||
return function(child) {
|
||||
if (child.operator === 'yield') {
|
||||
return _this.isGenerator = true;
|
||||
}
|
||||
};
|
||||
})(this));
|
||||
}
|
||||
|
||||
Code.prototype.children = ['params', 'body'];
|
||||
@@ -1910,6 +1918,9 @@
|
||||
this.body.makeReturn();
|
||||
}
|
||||
code = 'function';
|
||||
if (this.isGenerator) {
|
||||
code += '*';
|
||||
}
|
||||
if (this.ctor) {
|
||||
code += ' ' + this.name;
|
||||
}
|
||||
|
||||
@@ -456,7 +456,6 @@ exports.Return = class Return extends Base
|
||||
answer.push @makeCode ";"
|
||||
return answer
|
||||
|
||||
|
||||
#### Value
|
||||
|
||||
# A value, variable or literal or parenthesized, indexed or dotted into,
|
||||
@@ -1302,6 +1301,9 @@ exports.Code = class Code extends Base
|
||||
@params = params or []
|
||||
@body = body or new Block
|
||||
@bound = tag is 'boundfunc'
|
||||
@isGenerator = false
|
||||
@body.traverseChildren false, (child) =>
|
||||
@isGenerator = true if child.operator is 'yield'
|
||||
|
||||
children: ['params', 'body']
|
||||
|
||||
@@ -1369,6 +1371,7 @@ exports.Code = class Code extends Base
|
||||
uniqs.push name
|
||||
@body.makeReturn() unless wasEmpty or @noReturn
|
||||
code = 'function'
|
||||
code += '*' if @isGenerator
|
||||
code += ' ' + @name if @ctor
|
||||
code += '('
|
||||
answer = [@makeCode(code)]
|
||||
|
||||
Reference in New Issue
Block a user