Skip to content

Commit

Permalink
Fix #4533: chained calls incorrectly wrapping enclosing implicit objects
Browse files Browse the repository at this point in the history
  • Loading branch information
xixixao committed May 7, 2017
1 parent ac1b2b5 commit 253e9d4
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 @@ -151,9 +151,12 @@ class exports.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 @@ -300,7 +303,10 @@ class exports.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 @@ -198,6 +201,14 @@ test "#1495, method call chaining", ->
).join ', '
eq 'a, b, c', result

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 253e9d4

Please sign in to comment.