Fix multi-line if-else in single-line expression.

* When searching for the closing token of a single-line expression, ignore TERMINATORS that will subsequently be removed.
* Add a test.
This commit is contained in:
Marc Häfner 2013-07-31 12:18:50 +02:00
parent fdd5796f5e
commit 4342bedd2f
3 changed files with 12 additions and 2 deletions

View File

@ -358,8 +358,8 @@
var action, condition, indent, outdent, starter;
starter = indent = outdent = null;
condition = function(token, i) {
var _ref, _ref1;
return token[1] !== ';' && (_ref = token[0], __indexOf.call(SINGLE_CLOSERS, _ref) >= 0) && !(token[0] === 'ELSE' && starter !== 'THEN') && !(((_ref1 = token[0]) === 'CATCH' || _ref1 === 'FINALLY') && (starter === '->' || starter === '=>'));
var _ref, _ref1, _ref2;
return token[1] !== ';' && (_ref = token[0], __indexOf.call(SINGLE_CLOSERS, _ref) >= 0) && !(token[0] === 'TERMINATOR' && ((_ref1 = this.tag(i + 1)) === 'THEN' || _ref1 === 'ELSE')) && !(token[0] === 'ELSE' && starter !== 'THEN') && !(((_ref2 = token[0]) === 'CATCH' || _ref2 === 'FINALLY') && (starter === '->' || starter === '=>'));
};
action = function(token, i) {
return this.tokens.splice((this.tag(i - 1) === ',' ? i - 1 : i), 0, outdent);

View File

@ -361,6 +361,7 @@ class exports.Rewriter
condition = (token, i) ->
token[1] isnt ';' and token[0] in SINGLE_CLOSERS and
not (token[0] is 'TERMINATOR' and @tag(i + 1) in ['THEN', 'ELSE']) and
not (token[0] is 'ELSE' and starter isnt 'THEN') and
not (token[0] in ['CATCH', 'FINALLY'] and starter in ['->', '=>'])

View File

@ -151,3 +151,12 @@ test "#2981: Enforce initial indentation", ->
ok no
catch e
eq 'missing indentation', e.message
test "'single-line' expression containing multiple lines", ->
doesNotThrow -> CoffeeScript.compile """
(a, b) -> if a
-a
else if b
then -b
else null
"""