Skip to content

Fix #4150: Correctly outdent ternary followed by method call #4535

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

Merged
merged 1 commit into from
May 3, 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
2 changes: 1 addition & 1 deletion lib/coffee-script/rewriter.js

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

3 changes: 2 additions & 1 deletion src/rewriter.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,8 @@ class exports.Rewriter
not (token[0] is 'TERMINATOR' and @tag(i + 1) in EXPRESSION_CLOSE) and
not (token[0] is 'ELSE' and starter isnt 'THEN') and
not (token[0] in ['CATCH', 'FINALLY'] and starter in ['->', '=>']) or
token[0] in CALL_CLOSERS and @tokens[i - 1].newLine
token[0] in CALL_CLOSERS and
(@tokens[i - 1].newLine or @tokens[i - 1][0] is 'OUTDENT')

action = (token, i) ->
@tokens.splice (if @tag(i - 1) is ',' then i - 1 else i), 0, outdent
Expand Down
23 changes: 23 additions & 0 deletions test/formatting.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,29 @@ test "#1495, method call chaining", ->
).join ', '
eq 'a, b, c', result

test "chaining should not wrap spilling ternary", ->
throws -> CoffeeScript.compile """
if 0 then 1 else g
a: 42
.h()
"""

test "chaining should wrap calls containing spilling ternary", ->
f = (x) -> h: x
id = (x) -> x
result = f if true then 42 else id
a: 2
.h
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GeoffreyBooth I think you missed this test, it's exactly what you asked for.

I will split them each into a separate test call.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It’s it different between the gulp.pipe and result = f lines? I agree they’re very similar, and should both work, but I was wondering if the assignment made a difference.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, there is no difference. I can add a separate test if you want, but I think as long as this works it should be ok (it's hard to make this pass but your case fail).

eq 42, result

test "chaining should work within spilling ternary", ->
f = (x) -> h: x
id = (x) -> x
result = f if false then 1 else id
a: 3
.a
eq 3, result.h

# Nested blocks caused by paren unwrapping
test "#1492: Nested blocks don't cause double semicolons", ->
js = CoffeeScript.compile '(0;0)'
Expand Down