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

Wrong semicolon on semi = false mode #1388

Closed
vinhlh opened this issue Apr 22, 2017 · 6 comments
Closed

Wrong semicolon on semi = false mode #1388

vinhlh opened this issue Apr 22, 2017 · 6 comments
Labels
locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting.

Comments

@lydell
Copy link
Member

lydell commented Apr 22, 2017

This is on purpose. Here’s a comment that sparked the idea: #736 (comment)

@viankakrisna
Copy link

is it necessary to have semicolon right after opening brace?

@lydell
Copy link
Member

lydell commented Apr 22, 2017

The semicolon in your example is technically not needed – right now. But when you add a new line it is:

 it('should blah blah', () => {
+  console.log('is this test even running??')
   ['vinh'].forEach(t => {
     expect(convertRawToIds(t)).toEqual(['vinh'])
   })
 })

When you now run the tests you will be greeted by TypeError: Cannot read property 'vinh' of undefined. Or, if you’re lucky, you notice what prettier did to your code before running the tests:

it('should blah blah', () => {
  console.log('is this test even running??')['vinh'].forEach(t => {
    expect(convertRawToIds(t)).toEqual(['vinh'])
  })
})

Luckily, Prettier prints that “unnecessary” semicolon so you don’t have to think about them and won’t fall into frustrating traps. :)

@vinhlh
Copy link
Contributor Author

vinhlh commented Apr 22, 2017

Could you explain more about how the compiler works in this case? @lydell
http://astexplorer.net/#/gist/d6561f6a60b5ae5a0d53a4f9731a9c5b/76d8a6bd0bb48a46e9982b36a0532e5f479d3820
Without semicolon, console.log('is this test even running??') can not be understood as a CallExpression.

@lydell
Copy link
Member

lydell commented Apr 22, 2017

Sorry, I don’t understand what you mean.

@vinhlh
Copy link
Contributor Author

vinhlh commented Apr 22, 2017

@lydell the problem was that I misunderstood how semicolon is inserted automatically:
http://www.ecma-international.org/ecma-262/7.0/index.html#sec-rules-of-automatic-semicolon-insertion

@vinhlh vinhlh closed this as completed Apr 22, 2017
@lock lock bot added the locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. label Jul 7, 2018
@lock lock bot locked as resolved and limited conversation to collaborators Jul 7, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting.
Projects
None yet
Development

No branches or pull requests

3 participants