Skip to content

Javascript warning in Mozilla's engine #3579

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

Closed
djdeath opened this issue Aug 9, 2014 · 9 comments
Closed

Javascript warning in Mozilla's engine #3579

djdeath opened this issue Aug 9, 2014 · 9 comments

Comments

@djdeath
Copy link

djdeath commented Aug 9, 2014

When the mozilla JS engine has its strict option turned on, you get a lot of warning for CoffeeScript code transformed into JS code.
Most of them are related to return values :

anonymous function does not always return a value

It seems if you have the following CoffeeScript code :

(obj) ->
  return if obj.value < 0
  obj.value += 1

you get something like this :

(function() {
  (function(obj) {
    if (obj.value < 0) {
      return;
    }
    return obj.value += 1;
  });

}).call(this);

Now you can see the inconsistency between the 2 different return statement.

I'm wondering if this is something CoffeeScript people have given a thought to before, or if I should just give up on strict mode with JS code generated from CoffeeScript.

Thanks!

@vendethiel
Copy link
Collaborator

Well, you're the one responsible for your code — you were the one who made it return undefined.

@djdeath
Copy link
Author

djdeath commented Aug 9, 2014

But what the correct thing to do?

(obj) ->
  return if obj.value < 0
  obj.value += 1
  return

generates the exact same JS code.

@rlidwka
Copy link

rlidwka commented Aug 9, 2014

or if I should just give up on strict mode

I'd suggest doing it regardless of whether you're using coffeescript or not.

generates the exact same JS code.

No, it does not. In first case it would return obj.value, in second case it would return undefined. Look closely at the return statements.

@connec
Copy link
Collaborator

connec commented Aug 11, 2014

Could this warning be from the outer IIFE, which indeed does not return anything?

@vendethiel vendethiel reopened this Aug 11, 2014
@vendethiel
Copy link
Collaborator

No, probably not: it says it might not return something. The IIFE will never return anything, and it's easy to see. Also, I don't see them disallowing IIFEs.

Also, damn these little phones that make it so easy to reopen!

@epidemian
Copy link
Contributor

@Nami-Doc, i don't think this issue is about the enclosing IIFE. If i understand correctly, it's about a function that's supposed to act as a procedure (i.e. only have side effects and not return anything) being compiled into something that indeed returns something. I'd say it's related to #899 and others.

@djdeath
Copy link
Author

djdeath commented Aug 11, 2014

Do you think some kind of middle ground could be found here? :

  • if the function/method has already returned void previously, then no return statement at the end means it should return void to be consistent
  • other return the last expression/statement

@michaelficarra
Copy link
Collaborator

No. This would break existing code and be inconsistent. Just put a return at the end of your function. It will omit the actual return, and if your interpreter still complains about it, tell them to fix it on their end.

@epidemian
Copy link
Contributor

In this case, omitting the early return and using only one at the end would probably make it clearer that the function returns nothing:

(obj) ->
  obj.value += 1 if obj.value >= 0
  return

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

No branches or pull requests

6 participants