From e00fa5d5f81b609541b495397561ecd2deeb49cf Mon Sep 17 00:00:00 2001 From: Michal Srb Date: Fri, 12 May 2017 14:12:06 +0100 Subject: [PATCH] Fix #4533: chained calls incorrectly wrapping enclosing implicit objects (#4534) --- lib/coffee-script/rewriter.js | 30 +++++++++++++++++++++--------- src/rewriter.coffee | 14 ++++++++++---- test/formatting.coffee | 11 +++++++++++ 3 files changed, 42 insertions(+), 13 deletions(-) diff --git a/lib/coffee-script/rewriter.js b/lib/coffee-script/rewriter.js index 717e7fab..e3d8dd4f 100644 --- a/lib/coffee-script/rewriter.js +++ b/lib/coffee-script/rewriter.js @@ -171,7 +171,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, ref3, ref4, ref5, 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, ref3, ref4, ref5, s, sameLine, stackIdx, stackItem, stackTag, stackTop, startIdx, startImplicitCall, startImplicitObject, startsLine, tag; tag = token[0]; prevTag = (prevToken = i > 0 ? tokens[i - 1] : [])[0]; nextTag = (i < tokens.length - 1 ? tokens[i + 1] : [])[0]; @@ -182,17 +182,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; @@ -316,8 +323,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 5217e727..0d173f04 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 d175fbe3..2712a029 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)'