jashkenas/coffee-script — Issue #2181: Bug in `unless` with conditional assignment
Author: stephank
Created: 2012-03-07 12:18:47+00:00
State: open

Something like:

unless foo ||= bar
baz()

...is compiled as:

if (!foo || (foo = bar)) {
baz();
}

...which is not applying the 'not' operator to the entire result.

I was expecting this:

if (!(foo || (foo = bar))) {
baz();
}

The below examples all have a falsy condition result, but swap the assignment result and position of the conditional assignment. I believe the last example is incorrect. This is on master (ef0cb46b at the time of writing) and 1.2.0.

# coffee -s
foo = null
foo ||= no
unless foo
console.log('yay')
^D
yay

# coffee -s
foo = null
unless foo ||= no
console.log('yay')
^D
yay

# coffee -s
foo = null
foo ||= yes
unless foo
console.log('boo')
^D

# coffee -s
foo = null
unless foo ||= yes
console.log('boo')
^D
boo