Skip to content

Global return statements fail #167

Closed

Description

@watson

If I read #148 correctly, this should already be fixed and on npm, but given the following code in the file foo.js it fails anyway:

var n = Math.random()
if (n > 0.5) return console.log('You won the lottery!')
console.log('No luck - try again')

Standard will fail with the error "Illegal return statement":

$ standard --version
4.3.1
$ standard
standard: Use JavaScript Standard Style (https://github.com/feross/standard)
  /tmp/foo.js:2:20: Illegal return statement

So, help me out here :) What's going on?

Activity

  1. added a commit that references this issue on Jun 20, 2015
  2. feross commented on Jun 24, 2015

    @feross
    Member

    eslint (what standard uses under the hood for error checking) doesn't both support ES6 module syntax and global return statements at the same time. It's super annoying. We added module syntax support somewhere in the v3 series and lost global return support then. Right now, we've opted to support the module syntax instead of global return, as you can see in #148.

    But, since we now have support for custom js parsers now, perhaps we can remove module support and add back global return support. Users can get module support by using the esprima-fb or babel-eslint custom parsers.

    What do folks think?

  3. dcousens commented on Jun 24, 2015

    @dcousens
    Member

    Users can get module support by using the esprima-fb or babel-eslint custom parsers.

    vs

    No configuration. The easiest way to enforce consistent style in your project. Just drop it in.

    and

    No decisions to make. No .eslintrc, .jshintrc, or .jscsrc files to manage. It just works.

    I'm undecided.

  4. watson commented on Jun 24, 2015

    @watson
    MemberAuthor

    Just to clarify my use case: Some of my modules (like git-att referenced in this PR) ships with command line scripts (bin in package.json) and here it's really convenient to use global returns to abort under certain circumstances.

  5. feross commented on Jun 24, 2015

    @feross
    Member

    @watson Understood - I like to do that in my code too. Here's a workaround, if you're interested in one:

    if (argv.version || argv.v) {
      version()
      process.exit(0)
    }
  6. feross commented on Jun 24, 2015

    @feross
    Member

    @dcousens Yeah, I get what you're saying. Maybe we should just make standard use babel-eslint for broader ES6 and ES7 support? I wonder if that parser supports global return...

  7. dcousens commented on Jun 25, 2015

    @dcousens
    Member

    That could be worth looking into.

    Out of interest, would the desired behaviour with ES6 modules be that if an export is detected, global return should give an error?

  8. watson commented on Jun 26, 2015

    @watson
    MemberAuthor
  9. feross commented on Jun 28, 2015

    @feross
    Member

    Using babel-eslint won't solve the problem. babel doesn't support top-level return either.

    babel/babel#1298

  10. feross commented on Jul 10, 2015

    @feross
    Member

    I think this issue is unlikely to be fixed since global returns aren't valid ecmascript (which is silly). The various teams that work on the parsers are unwilling to add a special rule for invalid ecmascript.

    Unless anyone can think of something clever, I'm going to close this issue as WONTFIX.

  11. dcousens commented on Jul 11, 2015

    @dcousens
    Member

    I'm thinking WONTFIX is suitable. To clarify, the final position is that any globalReturn shall be invalid, ES5 or not?

  12. feross commented on Jul 13, 2015

    @feross
    Member

    @dcousens That's correct. standard doesn't distinguish between ES5 or ES6 right now so there's no easy way to allow it for just ES5.

  13. locked as resolved and limited conversation to collaborators on May 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions