From 5868260bfb61edb64d5e4e332955861f15b576ec Mon Sep 17 00:00:00 2001 From: Julian Rosse Date: Sun, 2 Jul 2017 21:56:23 -0500 Subject: [PATCH] refine sameLine implicit object tagging [Fixes #4568] --- lib/coffee-script/rewriter.js | 3 +++ src/rewriter.coffee | 5 +++-- test/formatting.coffee | 10 ++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/coffee-script/rewriter.js b/lib/coffee-script/rewriter.js index 8e7a6183a9..c134e34348 100644 --- a/lib/coffee-script/rewriter.js +++ b/lib/coffee-script/rewriter.js @@ -326,6 +326,9 @@ if (indexOf.call(LINEBREAKS, tag) >= 0) { for (k = stack.length - 1; k >= 0; k += -1) { stackItem = stack[k]; + if (!isImplicit(stackItem)) { + break; + } if (isImplicitObject(stackItem)) { stackItem[2].sameLine = false; } diff --git a/src/rewriter.coffee b/src/rewriter.coffee index ef409cb417..cd8d607429 100644 --- a/src/rewriter.coffee +++ b/src/rewriter.coffee @@ -303,8 +303,9 @@ exports.Rewriter = class Rewriter # Mark all enclosing objects as not sameLine if tag in LINEBREAKS - for stackItem in stack by -1 when isImplicitObject stackItem - stackItem[2].sameLine = no + for stackItem in stack by -1 + break unless isImplicit stackItem + stackItem[2].sameLine = no if isImplicitObject stackItem 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 2712a029d7..cf3fc2a3f0 100644 --- a/test/formatting.coffee +++ b/test/formatting.coffee @@ -232,6 +232,16 @@ test "method call chaining inside objects", -> .c eq 42, result.b +test "#4568: refine sameLine implicit object tagging", -> + condition = yes + fn = -> yes + + x = + fn bar: { + foo: 123 + } if not condition + eq x, undefined + # Nested blocks caused by paren unwrapping test "#1492: Nested blocks don't cause double semicolons", -> js = CoffeeScript.compile '(0;0)'