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
This commit is contained in:
Geoffrey Booth 2017-04-19 00:10:20 -07:00 committed by GitHub
parent ff60e6a6ce
commit bfce05438b
3 changed files with 14 additions and 2 deletions

View File

@ -3349,7 +3349,7 @@
return expr.compileToFragments(o);
}
fragments = expr.compileToFragments(o, LEVEL_PAREN);
bare = o.level < LEVEL_OP && (expr instanceof Op || expr instanceof Call || (expr instanceof For && expr.returns));
bare = o.level < LEVEL_OP && (expr instanceof Op || expr instanceof Call || (expr instanceof For && expr.returns)) && (o.level < LEVEL_COND || fragments.length <= 3);
if (bare) {
return fragments;
} else {

View File

@ -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

View File

@ -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", ->