Adding unmatched 'then' to the list of things that closes a single-line implicit call early. Issue #611.

This commit is contained in:
Jeremy Ashkenas 2010-08-30 22:04:13 -04:00
parent 0caa731291
commit eb9a524ea1
3 changed files with 26 additions and 2 deletions

View File

@ -185,7 +185,7 @@
var classLine;
classLine = false;
return this.scanTokens(function(token, i) {
var _c, action, callObject, condition, idx, next, prev;
var _c, action, callObject, condition, idx, next, prev, seenSingle;
if (token[0] === 'CLASS') {
classLine = true;
}
@ -196,6 +196,7 @@
if (callObject) {
idx = 2;
}
seenSingle = false;
if (include(LINEBREAKS, token[0])) {
classLine = false;
}
@ -205,6 +206,13 @@
if (prev && (prev.spaced && (include(IMPLICIT_FUNC, prev[0]) || prev.call) && include(IMPLICIT_CALL, token[0]) && !(token[0] === 'UNARY' && (('IN' === (_c = this.tag(i + 1)) || 'OF' === _c || 'INSTANCEOF' === _c)))) || callObject) {
this.tokens.splice(i, 0, ['CALL_START', '(', token[2]]);
condition = function(token, i) {
var _c;
if (!seenSingle && token.fromThen) {
return true;
}
if (('IF' === (_c = token[0]) || 'ELSE' === _c || 'UNLESS' === _c || '->' === _c || '=>' === _c)) {
seenSingle = true;
}
return (!token.generated && this.tokens[i - 1][0] !== ',' && include(IMPLICIT_END, token[0]) && !(token[0] === 'INDENT' && (include(IMPLICIT_BLOCK, this.tag(i - 1)) || this.tag(i - 2) === 'CLASS'))) || token[0] === 'PROPERTY_ACCESS' && this.tag(i - 1) === 'OUTDENT';
};
action = function(token, i) {
@ -236,6 +244,9 @@
_c = this.indentation(token);
indent = _c[0];
outdent = _c[1];
if (starter === 'THEN') {
indent.fromThen = true;
}
indent.generated = (outdent.generated = true);
this.tokens.splice(i + 1, 0, indent);
condition = function(token, i) {

View File

@ -164,12 +164,15 @@ exports.Rewriter = class Rewriter
idx = 1
callObject = not classLine and token[0] is 'INDENT' and next and next.generated and next[0] is '{' and prev and include(IMPLICIT_FUNC, prev[0])
idx = 2 if callObject
seenSingle = no
classLine = no if include(LINEBREAKS, token[0])
token.call = yes if prev and not prev.spaced and token[0] is '?'
if prev and (prev.spaced and (include(IMPLICIT_FUNC, prev[0]) or prev.call) and include(IMPLICIT_CALL, token[0]) and
not (token[0] is 'UNARY' and (@tag(i + 1) in ['IN', 'OF', 'INSTANCEOF']))) or callObject
@tokens.splice i, 0, ['CALL_START', '(', token[2]]
condition = (token, i) ->
return yes if not seenSingle and token.fromThen
seenSingle = yes if token[0] in ['IF', 'ELSE', 'UNLESS', '->', '=>']
(not token.generated and @tokens[i - 1][0] isnt ',' and include(IMPLICIT_END, token[0]) and
not (token[0] is 'INDENT' and (include(IMPLICIT_BLOCK, @tag(i - 1)) or @tag(i - 2) is 'CLASS'))) or
token[0] is 'PROPERTY_ACCESS' and @tag(i - 1) is 'OUTDENT'
@ -198,7 +201,8 @@ exports.Rewriter = class Rewriter
not (token[0] is 'ELSE' and @tag(i + 1) is 'IF')
starter = token[0]
[indent, outdent] = @indentation token
indent.generated = outdent.generated = true
indent.fromThen = true if starter is 'THEN'
indent.generated = outdent.generated = true
@tokens.splice i + 1, 0, indent
condition = (token, i) ->
(include(SINGLE_CLOSERS, token[0]) and token[1] isnt ';') and

View File

@ -109,3 +109,12 @@ func = ->
a
ok func() is 5
# Unmatched 'then' should catch implicit calls.
i = 1
isTrue = (x) -> x is true
if isTrue yes then i += 1
ok i is 2