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

using `return` in the top level of a node.js script #510

Closed
75lb opened this issue May 4, 2016 · 5 comments

Comments

@75lb
Copy link

commented May 4, 2016

using standard v7, trying to validate this node.js script:

console.log('one')
return 
console.log('two')

throws this issue:

Parsing error: 'return' outside of function

In node.js, the top level script is wrapped by a function. Even though it might look like use of return outside a function, it is not..

what to do here? This is a valid use of return..

@LinusU

This comment has been minimized.

Copy link
Member

commented May 4, 2016

I thought that this had been the case for a long time. I would recommend using process.exit(0) instead.

@75lb

This comment has been minimized.

Copy link
Author

commented May 4, 2016

@LinusU if return and process.exit() were equivalent, i would.. they are not related and have different consequences.

@feross

This comment has been minimized.

Copy link
Member

commented May 4, 2016

Yes, this has been an issue for a long time.

This is an unfortunate side effect of two bad decisions made by TC39:

  1. Top-level return is an error.
  2. ES modules have a completely different parsing mode ("module") than the normal mode ("script").

The decision to have two modes means that every piece of tooling that parses JS has to expose this decision to the user.

To keep standard simple, we just assume "module" which makes the ESLint parser support all the module syntax (export, etc.) but has the downside of preventing top-level return. :(

If we assumed "script" then many more things would not work, so this is the right compromise I think.

@feross feross closed this May 4, 2016

@feross feross added the question label May 4, 2016

@75lb

This comment has been minimized.

Copy link
Author

commented May 15, 2016

could you leave the "module" or "script" mode decision to the user with a --mode option or similar?

The current behaviour prevents me using Standard in test suites or CI as it will always fail when linting my top-level scripts (which typically contain return in a catch handler to gracefully exit the process).

@feross

This comment has been minimized.

Copy link
Member

commented May 15, 2016

It's an intentional decision to not have modes or options in standard. I understand that this is painful right now, but adding a mode would add complexity for every user of standard, in perpetuity.

We'll have to find a better long-term workaround for this.

For now, consider not linting the files which contain top-level return.

@lock lock bot locked as resolved and limited conversation to collaborators May 10, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
3 participants
You can’t perform that action at this time.