Skip to content

Commit bfce054

Browse files
Fix parenthesized conditions in if-else assignment (#4519)
* Fix parenthesized conditions in if-else assignment * Add compiled output * Use ‘is’ instead of ‘==‘; ‘right’ is a poor name when you mean ‘correct,’ not the right-hand side of the assignments in this test
1 parent ff60e6a commit bfce054

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

lib/coffee-script/nodes.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/nodes.coffee

+2-1
Original file line numberDiff line numberDiff line change
@@ -2254,7 +2254,8 @@ exports.Parens = class Parens extends Base
22542254
return expr.compileToFragments o
22552255
fragments = expr.compileToFragments o, LEVEL_PAREN
22562256
bare = o.level < LEVEL_OP and (expr instanceof Op or expr instanceof Call or
2257-
(expr instanceof For and expr.returns))
2257+
(expr instanceof For and expr.returns)) and (o.level < LEVEL_COND or
2258+
fragments.length <= 3)
22582259
if bare then fragments else @wrapInBraces fragments
22592260

22602261
#### StringWithInterpolations

test/control_flow.coffee

+11
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,17 @@ test "#748: trailing reserved identifiers", ->
198198
nonce
199199
eq nonce, result
200200

201+
test 'if-else within an assignment, condition parenthesized', ->
202+
result = if (1 is 1) then 'correct'
203+
eq result, 'correct'
204+
205+
result = if ('whatever' ? no) then 'correct'
206+
eq result, 'correct'
207+
208+
f = -> 'wrong'
209+
result = if (f?()) then 'correct' else 'wrong'
210+
eq result, 'correct'
211+
201212
# Postfix
202213

203214
test "#3056: multiple postfix conditionals", ->

0 commit comments

Comments
 (0)