Skip to content

Fix parenthesized conditions in if-else assignment #4519

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 4 commits into from
Apr 19, 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/nodes.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/nodes.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2254,7 +2254,8 @@ exports.Parens = class Parens extends Base
return expr.compileToFragments o
fragments = expr.compileToFragments o, LEVEL_PAREN
bare = o.level < LEVEL_OP and (expr instanceof Op or expr instanceof Call or
(expr instanceof For and expr.returns))
(expr instanceof For and expr.returns)) and (o.level < LEVEL_COND or
fragments.length <= 3)
if bare then fragments else @wrapInBraces fragments

#### StringWithInterpolations
Expand Down
11 changes: 11 additions & 0 deletions test/control_flow.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,17 @@ test "#748: trailing reserved identifiers", ->
nonce
eq nonce, result

test 'if-else within an assignment, condition parenthesized', ->
result = if (1 is 1) then 'correct'
eq result, 'correct'

result = if ('whatever' ? no) then 'correct'
eq result, 'correct'

f = -> 'wrong'
result = if (f?()) then 'correct' else 'wrong'
eq result, 'correct'

# Postfix

test "#3056: multiple postfix conditionals", ->
Expand Down