Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

yet another autocb corner case!

  • Loading branch information...
commit da98af50a24634a3a7e7dfac6baba85f1f6913ce 1 parent 1520ef1
@maxtaco authored
View
18 lib/coffee-script/nodes.js
@@ -394,18 +394,25 @@
};
Block.prototype.makeReturn = function(res) {
- var expr, len;
+ var expr, foundReturn, len;
len = this.expressions.length;
+ foundReturn = false;
while (len--) {
expr = this.expressions[len];
if (!(expr instanceof Comment)) {
this.expressions[len] = expr.makeReturn(res);
if (expr instanceof Return && !expr.expression && !expr.tameHasAutocbFlag) {
this.expressions.splice(len, 1);
+ foundReturn = true;
+ } else if (!(expr instanceof If) || expr.elseBody) {
+ foundReturn = true;
}
break;
}
}
+ if (this.tameHasAutocbFlag && !this.tameNodeFlag && !foundReturn) {
+ this.expressions.push(new Return(null, true));
+ }
return this;
};
@@ -687,10 +694,10 @@
Return.prototype.isStatement = YES;
- Return.prototype.jumps = THIS;
-
Return.prototype.makeReturn = THIS;
+ Return.prototype.jumps = THIS;
+
Return.prototype.compile = function(o, level) {
var expr, _ref2;
expr = (_ref2 = this.expression) != null ? _ref2.makeReturn() : void 0;
@@ -1788,10 +1795,7 @@
o.scope.parameter(vars[i] = v.compile(o));
}
}
- if (wasEmpty && this.tameHasAutocbFlag) {
- this.body.expressions.unshift(new Return(null, true));
- wasEmpty = false;
- }
+ if (this.tameHasAutocbFlag) wasEmpty = false;
if (!(wasEmpty || this.noReturn)) this.body.makeReturn();
if (this.bound) {
if ((_ref6 = o.scope.parent.method) != null ? _ref6.bound : void 0) {
View
5 lib/coffee-script/parser.js
@@ -524,7 +524,7 @@ parse: function parse(input) {
token = self.symbols_[token] || token;
}
return token;
- };
+ }
var symbol, preErrorSymbol, state, action, a, r, yyval={},p,len,newState, expected;
while (true) {
@@ -542,6 +542,7 @@ parse: function parse(input) {
}
// handle parse error
+ _handle_error:
if (typeof action === 'undefined' || !action.length || !action[0]) {
if (!recovering) {
@@ -552,7 +553,7 @@ parse: function parse(input) {
}
var errStr = '';
if (this.lexer.showPosition) {
- errStr = 'Parse error on line '+(yylineno+1)+":\n"+this.lexer.showPosition()+'\nExpecting '+expected.join(', ');
+ errStr = 'Parse error on line '+(yylineno+1)+":\n"+this.lexer.showPosition()+"\nExpecting "+expected.join(', ') + ", got '" + this.terminals_[symbol]+ "'";
} else {
errStr = 'Parse error on line '+(yylineno+1)+": Unexpected " +
(symbol == 1 /*EOF*/ ? "end of input" :
View
15 src/nodes.coffee
@@ -322,13 +322,20 @@ exports.Block = class Block extends Base
# ensures that the final expression is returned.
makeReturn: (res) ->
len = @expressions.length
+ foundReturn = false
while len--
expr = @expressions[len]
if expr not instanceof Comment
@expressions[len] = expr.makeReturn res
- @expressions.splice(len, 1) if expr instanceof Return and
+ if expr instanceof Return and
not expr.expression and not expr.tameHasAutocbFlag
+ @expressions.splice(len, 1)
+ foundReturn = true
+ else if not (expr instanceof If) or expr.elseBody
+ foundReturn = true
break
+ if @tameHasAutocbFlag and not @tameNodeFlag and not foundReturn
+ @expressions.push(new Return null, true)
this
# Optimization!
@@ -1457,11 +1464,7 @@ exports.Code = class Code extends Base
throw SyntaxError "multiple parameters named '#{name}'" if name in uniqs
uniqs.push name
- # Empty guys should still autocb
- if wasEmpty and @tameHasAutocbFlag
- @body.expressions.unshift(new Return null, true)
- wasEmpty = false
-
+ wasEmpty = false if @tameHasAutocbFlag
@body.makeReturn() unless wasEmpty or @noReturn
if @bound
if o.scope.parent.method?.bound
View
39 test/tame.coffee
@@ -295,3 +295,42 @@ atest "empty autocb", (cb) ->
bar = (autocb) ->
await bar defer()
cb(true, {})
+
+atest "more autocb (false)", (cb) ->
+ bar = (autocb) ->
+ if false
+ console.log "not reached"
+ await bar defer()
+ cb(true, {})
+
+atest "more autocb (true)", (cb) ->
+ bar = (autocb) ->
+ if true
+ 10
+ await bar defer()
+ cb(true, {})
+
+atest "more autocb (true & false)", (cb) ->
+ bar = (autocb) ->
+ if false
+ 10
+ else
+ if false
+ 11
+ await bar defer()
+ cb(true, {})
+
+atest "more autocb (while)", (cb) ->
+ bar = (autocb) ->
+ while false
+ 10
+ await bar defer()
+ cb(true, {})
+
+atest "more autocb (comments)", (cb) ->
+ bar = (autocb) ->
+ ###
+ blah blah blah
+ ###
+ await bar defer()
+ cb(true, {})
Please sign in to comment.
Something went wrong with that request. Please try again.