Skip to content
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

Bug in unless with conditional assignment #2181

Closed
stephank opened this issue Mar 7, 2012 · 1 comment
Closed

Bug in unless with conditional assignment #2181

stephank opened this issue Mar 7, 2012 · 1 comment
Labels

Comments

@stephank
Copy link

stephank commented Mar 7, 2012

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 (ef0cb46 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
@michaelficarra
Copy link
Collaborator

Yep, it looks like ||= isn't being negated properly. We're not adding the necessary parentheses.

edit: A temporary solution is adding parentheses around the expression yourself. Apparently they're being preserved...

$ coffee -bep 'a = null; b unless a ||= c'
var a;

a = null;

if (!a || (a = c)) {
  b;

}
$ coffee -bep 'a = null; b unless (a ||= c)'
var a;

a = null;

if (!(a || (a = c))) {
  b;

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants