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

The rule “Never start a line with (, [, or `” is incorrect, too restrictive, and not easy enough to follow #938

Closed
claudepache opened this issue Jun 27, 2017 · 3 comments

Comments

@claudepache
Copy link

commented Jun 27, 2017

The rule in question:

  • Never start a line with (, [, or `
    • This is the only gotcha with omitting semicolons – automatically checked for you!

(1) The rule is incorrect, because:

  • +, - and / are also hazards for ASI, and should be added to the list of offending tokens.

(2) The rule is too restrictive, because it is fine to start a line with any of the offending character when it is not at the beginning of a standalone statement. That includes:

  • comments // and /* (those are not statements);
  • dependent statements of an if, else, etc. clause (those are not standalone);
  • second, third, etc. lines of a statement split across several lines (those are not at the beginning).

(3) Finally, it lacks a simple receipt of what to do when you are in a situation where you want to start a line with ( or [. You don’t have to refactor your code. You just need to precede it with a semicolon, but only when you are at the beginning of a standalone statement (hence the usefulness of mentioning “beginning of a standalone statement”).

@claudepache claudepache changed the title The rule “Never start a line with (, [, or `” is incorrect, too restrictive, and not simple enough to follow The rule “Never start a line with (, [, or `” is incorrect, too restrictive, and not easy enough to follow Jun 27, 2017

@yoshuawuyts

This comment has been minimized.

Copy link
Contributor

commented Jun 27, 2017

Err, alright - I'm not sure what your question is here. If your question is "could you please change this?" then I can save us all some time by telling you it's not going to happen. Keen to hear what you mean tho, we're always open to improvements in documentation or otherwise! (:

@bcomnes

This comment has been minimized.

Copy link
Member

commented Jun 27, 2017

  1. beginning new lines with + - and - are already protected by other eslint rules included in standard that yell at you when you do this (e.g '+' should be placed at the end of the line).
  2. Starting a line with those characters are fairly common ([a,b].forEach()), but writing semicolon free JS requires you protect those cases with preceding semicolons. Also standard doesn't mind if you start lines with [] inside of /**/ blocks.
  3. The simple solution is prefixing the line with a semicolon, and this is allowed.

The rule in question is a high level conceptual rule, and isn't the rule specially used to lint, enforce or solve the scenario its trying to protect against. For a more in depth discussion of the rational for this read https://feross.org/never-use-semicolons/

I recommend trying out the linter and the style as is, and if you like it great! If not, then you are free to choose your own slew of eslint rules, or some of the variants but beware of wasted time bike shedding.

@feross

This comment has been minimized.

Copy link
Member

commented Jun 27, 2017

@claudepache Thanks for sharing your thoughts. The statement in question "Never start a line with..." is easy-to-understand and succinct. You're technically correct that it should say standalone statements, not lines, but the way it is written now gets the point across more clearly to users of all skill levels.

Of course, the actual ESLint rules operate on the AST and enforce that no unexpected ASI occurs, based on how the code would actually execute. But, I think sharing a simple "rule of thumb" makes more sense in the first few lines of the readme.

Cheers :)

@feross feross closed this Jun 27, 2017

@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.
4 participants
You can’t perform that action at this time.