diff --git a/lib/coffeescript/rewriter.js b/lib/coffeescript/rewriter.js index 591e784a..78e2587b 100644 --- a/lib/coffeescript/rewriter.js +++ b/lib/coffeescript/rewriter.js @@ -168,7 +168,7 @@ stack = []; start = null; return this.scanTokens(function(token, i, tokens) { - var endImplicitCall, endImplicitObject, forward, inImplicit, inImplicitCall, inImplicitControl, inImplicitObject, newLine, nextTag, offset, prevTag, prevToken, ref, ref1, ref2, s, sameLine, stackIdx, stackTag, stackTop, startIdx, startImplicitCall, startImplicitObject, startsLine, tag; + var endImplicitCall, endImplicitObject, forward, inImplicit, inImplicitCall, inImplicitControl, inImplicitObject, isImplicit, isImplicitCall, isImplicitObject, k, newLine, nextTag, offset, prevTag, prevToken, ref, ref1, ref2, s, sameLine, stackIdx, stackItem, stackTag, stackTop, startIdx, startImplicitCall, startImplicitObject, startsLine, tag; [tag] = token; [prevTag] = prevToken = i > 0 ? tokens[i - 1] : []; [nextTag] = i < tokens.length - 1 ? tokens[i + 1] : []; @@ -179,17 +179,24 @@ forward = function(n) { return i - startIdx + n; }; + isImplicit = function(stackItem) { + var ref; + return stackItem != null ? (ref = stackItem[2]) != null ? ref.ours : void 0 : void 0; + }; + isImplicitObject = function(stackItem) { + return isImplicit(stackItem) && (stackItem != null ? stackItem[0] : void 0) === '{'; + }; + isImplicitCall = function(stackItem) { + return isImplicit(stackItem) && (stackItem != null ? stackItem[0] : void 0) === '('; + }; inImplicit = function() { - var ref, ref1; - return (ref = stackTop()) != null ? (ref1 = ref[2]) != null ? ref1.ours : void 0 : void 0; + return isImplicit(stackTop()); }; inImplicitCall = function() { - var ref; - return inImplicit() && ((ref = stackTop()) != null ? ref[0] : void 0) === '('; + return isImplicitCall(stackTop()); }; inImplicitObject = function() { - var ref; - return inImplicit() && ((ref = stackTop()) != null ? ref[0] : void 0) === '{'; + return isImplicitObject(stackTop()); }; inImplicitControl = function() { var ref; @@ -310,8 +317,13 @@ startImplicitObject(s, !!startsLine); return forward(2); } - if (inImplicitObject() && indexOf.call(LINEBREAKS, tag) >= 0) { - stackTop()[2].sameLine = false; + if (indexOf.call(LINEBREAKS, tag) >= 0) { + for (k = stack.length - 1; k >= 0; k += -1) { + stackItem = stack[k]; + if (isImplicitObject(stackItem)) { + stackItem[2].sameLine = false; + } + } } newLine = prevTag === 'OUTDENT' || prevToken.newLine; if (indexOf.call(IMPLICIT_END, tag) >= 0 || indexOf.call(CALL_CLOSERS, tag) >= 0 && newLine) { diff --git a/src/rewriter.coffee b/src/rewriter.coffee index 57013822..b122a041 100644 --- a/src/rewriter.coffee +++ b/src/rewriter.coffee @@ -149,9 +149,12 @@ exports.Rewriter = class Rewriter forward = (n) -> i - startIdx + n # Helper functions - inImplicit = -> stackTop()?[2]?.ours - inImplicitCall = -> inImplicit() and stackTop()?[0] is '(' - inImplicitObject = -> inImplicit() and stackTop()?[0] is '{' + isImplicit = (stackItem) -> stackItem?[2]?.ours + isImplicitObject = (stackItem) -> isImplicit(stackItem) and stackItem?[0] is '{' + isImplicitCall = (stackItem) -> isImplicit(stackItem) and stackItem?[0] is '(' + inImplicit = -> isImplicit stackTop() + inImplicitCall = -> isImplicitCall stackTop() + inImplicitObject = -> isImplicitObject stackTop() # Unclosed control statement inside implicit parens (like # class declaration or if-conditionals) inImplicitControl = -> inImplicit() and stackTop()?[0] is 'CONTROL' @@ -298,7 +301,10 @@ exports.Rewriter = class Rewriter # .g b # .h a - stackTop()[2].sameLine = no if inImplicitObject() and tag in LINEBREAKS + # Mark all enclosing objects as not sameLine + if tag in LINEBREAKS + for stackItem in stack by -1 when isImplicitObject stackItem + stackItem[2].sameLine = no newLine = prevTag is 'OUTDENT' or prevToken.newLine if tag in IMPLICIT_END or tag in CALL_CLOSERS and newLine diff --git a/test/formatting.coffee b/test/formatting.coffee index 138cdf2d..66042c47 100644 --- a/test/formatting.coffee +++ b/test/formatting.coffee @@ -128,6 +128,9 @@ test "indented heredoc", -> # * single line arguments # * inline function literal # * inline object literal +# +# * chaining inside +# * implicit object literal test "chaining after outdent", -> id = (x) -> x @@ -221,6 +224,14 @@ test "chaining should work within spilling ternary", -> .a eq 3, result.h +test "method call chaining inside objects", -> + f = (x) -> c: 42 + result = + a: f 1 + b: f a: 1 + .c + eq 42, result.b + # Nested blocks caused by paren unwrapping test "#1492: Nested blocks don't cause double semicolons", -> js = CoffeeScript.compile '(0;0)'