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

Invalid JS: return if not x? creates empty if block #2298

Closed
colinhowe opened this issue May 2, 2012 · 9 comments
Closed

Invalid JS: return if not x? creates empty if block #2298

colinhowe opened this issue May 2, 2012 · 9 comments
Labels

Comments

@colinhowe
Copy link

We have some code like:

return if not x?

And this generates:

if (!(x != null)) return

As I understand it, this should output:

if (x == null) return

Or am I missing something unusual about JS? :)

@satyr
Copy link
Collaborator

satyr commented May 2, 2012

Interestingly:

$ coffee -bce '(x) -> return unless x?'
// Generated by CoffeeScript 1.3.2-pre

(function(x) {
  if (x == null) {

  }
});

$ coco -bce '(x) -> return if not x?'
(function(x){
  if (x == null) {
    return;
  }
});

@goto-bus-stop
Copy link

CoffeeScript reads not x as !x, no matter what x is... perhaps unary ! should call .invert, like unless, but that's not exactly congruent with JS and stuff

@osuushi
Copy link

osuushi commented May 2, 2012

Why not just make "if not" specifically synonymous with "unless"? Are there cases where they should not be equivalent?

@michaelficarra
Copy link
Collaborator

See #1393.

@GeoffreyBooth GeoffreyBooth changed the title return if not x? creates confusing code return if not x? creates empty if block Apr 29, 2017
@GeoffreyBooth
Copy link
Collaborator

Even more minimal case:

-> return if yes

(function() {
  if (true) {

  }
});

@connec
Copy link
Collaborator

connec commented May 1, 2017

This seems like some kind of optimisation. If there's anything after the return if yes line everything is fine.

Example

->
  return if yes
  'hello'

(function() {
  if (true) {
    return;
  }
  return 'hello';
});

@vendethiel
Copy link
Collaborator

vendethiel commented May 1, 2017

The optimization is in Block, which is an If's @body.

@GeoffreyBooth GeoffreyBooth changed the title return if not x? creates empty if block Invalid JS: return if not x? creates empty if block May 5, 2017
@defiled
Copy link

defiled commented May 24, 2017

The phrasing on this one is a bit strange and can lead to confusion. Heres an example:

return 'Please enter a number.' if not consumer.number? or consumer.number.length is < 9

So this is saying to return an error message of "Please enter a number" when the two specified conditions have been met. The first one being that there is no data in consumer.number?. The second condition checks for valid data, so it doesn't take any strings with a length less than 9.

@GeoffreyBooth
Copy link
Collaborator

I can’t seem to write a test for this. Returning void (i.e. a plain return statement) produces the same return value as a function that doesn’t return. So

(function() {
  if (true) {

  }
})();

is the same, really, as

(function() {
  if (true) {
    return;
  }
})();

And as @connec wrote, as soon as you put a value anywhere (return 1 if yes, or a second return after the line with the if yes) it compiles as we’d expect.

So I guess this is just an optimization that works. It’s a bit confusing to read, but -> return if yes is such a weird edge case that we shouldn’t do anything special here just for readability.

As for the question about the readability of if not versus unless, I don’t think the generated output is confusing enough to justify revision.

So I don’t see anything here that needs fixing. Please comment if I’m wrong, and I’ll reopen.

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

9 participants