Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #4533: chained calls incorrectly wrapping enclosing implicit objects #4534

Merged
merged 1 commit into from
May 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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