Skip to content

Commit

Permalink
Fix #4533: chained calls incorrectly wrapping enclosing implicit obje…
Browse files Browse the repository at this point in the history
…cts (#4534)
  • Loading branch information
xixixao authored and GeoffreyBooth committed May 12, 2017
1 parent 51c0657 commit e00fa5d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
30 changes: 21 additions & 9 deletions lib/coffee-script/rewriter.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 10 additions & 4 deletions src/rewriter.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions test/formatting.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)'
Expand Down

0 comments on commit e00fa5d

Please sign in to comment.