mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-05-03 03:00:14 -04:00
fixes #1858: top-level returns should force safety wrapper
This commit is contained in:
@@ -302,13 +302,21 @@
|
||||
};
|
||||
|
||||
Block.prototype.compileRoot = function(o) {
|
||||
var code;
|
||||
var code, hasReturn;
|
||||
o.indent = this.tab = o.bare ? '' : TAB;
|
||||
o.scope = new Scope(null, this, null);
|
||||
o.level = LEVEL_TOP;
|
||||
this.spaced = true;
|
||||
code = this.compileWithDeclarations(o);
|
||||
if (o.bare || o.scope.variables.length <= 1) return code;
|
||||
hasReturn = (function(exps) {
|
||||
var e, _i, _len;
|
||||
for (_i = 0, _len = exps.length; _i < _len; _i++) {
|
||||
e = exps[_i];
|
||||
if (e instanceof Return) return true;
|
||||
}
|
||||
return false;
|
||||
})(this.expressions);
|
||||
if ((o.bare || o.scope.variables.length <= 1) && !hasReturn) return code;
|
||||
return "(function() {\n" + code + "\n}).call(this);\n";
|
||||
};
|
||||
|
||||
|
||||
@@ -243,13 +243,18 @@ exports.Block = class Block extends Base
|
||||
# It would be better not to generate them in the first place, but for now,
|
||||
# clean up obvious double-parentheses.
|
||||
compileRoot: (o) ->
|
||||
o.indent = @tab = if o.bare then '' else TAB
|
||||
o.scope = new Scope null, this, null
|
||||
o.level = LEVEL_TOP
|
||||
@spaced = yes
|
||||
code = @compileWithDeclarations o
|
||||
o.indent = @tab = if o.bare then '' else TAB
|
||||
o.scope = new Scope null, this, null
|
||||
o.level = LEVEL_TOP
|
||||
@spaced = yes
|
||||
code = @compileWithDeclarations o
|
||||
hasReturn = ((exps)->
|
||||
for e in exps
|
||||
return true if e instanceof Return
|
||||
false
|
||||
)(@expressions)
|
||||
# the `1` below accounts for `arguments`, always "in scope"
|
||||
return code if o.bare or o.scope.variables.length <= 1
|
||||
return code if (o.bare or o.scope.variables.length <= 1) and not hasReturn
|
||||
"(function() {\n#{code}\n}).call(this);\n"
|
||||
|
||||
# Compile the expressions body for the contents of a function, with
|
||||
|
||||
Reference in New Issue
Block a user