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

Never start a line with regular expression literal #728

Closed
academyofzhuang opened this issue Dec 23, 2016 · 7 comments

Comments

@academyofzhuang
Copy link

commented Dec 23, 2016

If one start a line with a regular expression, it causes parsing error, too.

var s = 'About'
// if string begins with a Capital letter, print it
/^[A-Z].+/.test(s) && console.log(s)

Adding a semicolon before regex solved the problem.

Shall we add this exception to the rule.

@ifraixedes

This comment has been minimized.

Copy link

commented Dec 23, 2016

I don't understand what you meant.

Without semicolon, standard reports an error, because it's an actual syntax error, if you add a semicolon then the syntax is correct and standard doesn't report any error.

@dcousens

This comment has been minimized.

Copy link
Member

commented Dec 24, 2016

@ifraixedes I think @academyofzhuang means these rules:

Never start a line with (, [, or `
@feross

This comment has been minimized.

Copy link
Member

commented Dec 24, 2016

The list is intentionally incomplete. I listed only characters that have I've seen used in actual code, and ignored characters that are usually never used. A more complete list appears later in the rules in the quote from An Open Letter to JavaScript Leaders Regarding Semicolons:

The next line starts with [, (, +, *, /, -, ,, ., or some other binary operator that can only be found between two tokens in a single expression.

@feross feross closed this Dec 24, 2016

@dcousens

This comment has been minimized.

Copy link
Member

commented Dec 24, 2016

@ifraixedes a better solution to your problem:

if (/^[A-Z].+/.test(s)) console.log(s)

I agree with @feross, this is a non-issue as the only use case I can see, is if you are doing what you were, which is never ideal IMHO.

@ifraixedes

This comment has been minimized.

Copy link

commented Dec 25, 2016

@dcousens I think that you ping the wrong person.

I have never written something how it was expressed in the question, I prefer forms as the one that you described above.

Cheers

@dcousens

This comment has been minimized.

Copy link
Member

commented Dec 25, 2016

Apologies @ifraixedes, I meant @academyofzhuang

@academyofzhuang

This comment has been minimized.

Copy link
Author

commented Dec 27, 2016

I agreed with you. Using boolean shortcut to save a few key strokes isn't best practice to start with.

The reason I brought up the issue is the parsing error message by js engine was cryptic:

standard: Use JavaScript Standard Style (http://standardjs.com)
  /Users/aoz/academyofzhuang/test/std.js:2:2: Parsing error: Unexpected token ^

When I look at the code, it doesn't make any sense.

var s = 'About'
/^[A-Z].+/.test(s) && console.log(s) // quick and dirty debug

Since I believed, as stated in the project readme

This is the only gotcha with omitting semicolons – automatically checked for you!

It took me some time to realize a semicolon might be needed.

I agree this is a non-issue. To quote the project readme:

Clever short-hands are discouraged, in favor of clear and readable expressions, whenever possible.

Thank you for the detailed explanation and example code!

@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.