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

Exclude semicolon is a bad practice or not #856

Closed
ufocoder opened this issue Apr 12, 2017 · 3 comments

Comments

@ufocoder
Copy link

commented Apr 12, 2017

I use standard and I have the following code:

const a = 'methodA'
const b = 'methodB'

class MyClass {
  [a] = () => (5 * 7)
  [b] = () => (5 * 10)
}

Of course, it will be compiled not correct. Try it online through REPL

There's the correct code:

const a = 'methodA'
const b = 'methodB'

class MyClass {
  [a] = () => (5 * 7);
  [b] = () => (5 * 10)
}

Standard allow us not to write semicolons, but sometimes it required. How I should resolve this situation more elegantly?

@rstacruz

This comment has been minimized.

Copy link
Member

commented Apr 12, 2017

Never start a line with (, [, or `. This is the only gotcha with omitting semicolons, and standard protects you from this potential issue.
eslint: no-unexpected-multiline

See the no-unexpected-multiline rule for a list of violations it'll catch.

@rstacruz rstacruz closed this Apr 12, 2017

@rstacruz

This comment has been minimized.

Copy link
Member

commented Apr 12, 2017

Usually prefixing the line with ; or void would fix it. In your case, it'll have to be ;.

// Bad
window.a = 2
(function () {})()

// OK
window.a = 2
;(function () {})()

// Also OK
window.a = 2
void (function () {})()

@dcousens dcousens added the question label Apr 12, 2017

@feross

This comment has been minimized.

Copy link
Member

commented Apr 13, 2017

When omitting semicolons, the only rule to remember is never start a line with (, [ or `. When you must, you can prefix that line with a semicolon.

const a = 'methodA'
const b = 'methodB'

class MyClass {
  [a] = () => (5 * 7)
  ;[b] = () => (5 * 10)
}

It looks a bit silly at first, but you quickly get used to it.

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