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

IIFE not checkt for semicolon at the start? #669

Closed
borisdiakur opened this issue Oct 29, 2016 · 6 comments

Comments

@borisdiakur
Copy link

commented Oct 29, 2016

https://github.com/feross/standard/blob/master/RULES.md#semicolons suggests the following:

// ✓ ok
;(function () {
  window.alert('ok')
}())
// ✗ avoid
(function () {
  window.alert('ok')
}())

However, when I navigate to http://standardjs.com/demo.html and paste the following code, the linter tells me it’s fine:

(function () {
  window.alert('ok')
})()

I’m new to the no semicolons style, so now I’m confused. Either the documentation or the linter is wrong.

@Flet

This comment has been minimized.

Copy link
Member

commented Oct 29, 2016

If there is no code above the IIFE, it will work OK.

It becomes more clear when adding a line at the top:
http://standardjs.com/demo.html?gist=f925d40642900c782d2b9d9e18731ece

@borisdiakur

This comment has been minimized.

Copy link
Author

commented Oct 29, 2016

Here is an example with code above. It’s still ok:

if (typeof foo !== 'undefined') {
  throw new Error('foo is already defined!')
}

(function () {
  window.alert('ok')
}())

http://standardjs.com/demo.html?gist=7382351fed23caf08381c01669b0900d

@Flet

This comment has been minimized.

Copy link
Member

commented Oct 29, 2016

Indeed, this one is a funny edge case :)

Somewhat related: #70

@Flet

This comment has been minimized.

Copy link
Member

commented Oct 29, 2016

There is not a specific rule that enforces semicolons before IIFEs, but other rules will catch problems if they arise (like func-call-spacing).

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

The guideline above will ensure code always works, even if there is a special edge case where its not required (like a preceding if block).

If this is uncomfortable, there is always semistandard :)

@borisdiakur

This comment has been minimized.

Copy link
Author

commented Oct 29, 2016

As long as standard catches all problems related to missing semicolons I feel comfortable.

There is not a specific rule that enforces semicolons before IIFEs, but other rules will catch problems if they arise (like func-call-spacing).

Maybe that should be noted in the RULES.md?

@feross

This comment has been minimized.

Copy link
Member

commented Dec 27, 2016

standard catches all problems related to missing semicolons. 👍

@feross feross closed this Dec 27, 2016

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