How do I enable unnecessary semicolon warnings in JSHint?

1.9k Views Asked by At

Right now if I have:

console.log;

I don't get a warning from JSHint. I'd like to start using the standard of not using semicolons unnecessarily, and I'd appreciate a little help from JSHint. Is there a supported way to enable warnings for unnecessary semicolon use?

See: https://www.codecademy.com/blog/78

It recommends that I use JSLint instead, but JSLint enforces spaces over tabs which I'd prefer not to use.

2

There are 2 best solutions below

0
On BEST ANSWER

After reading all of the options, I can find no method for enabling this in JSHint.

Then, after reading more into standardJS which is really what I'm after, they have their own linter for atom which is what I was looking for.

https://atom.io/packages/linter-js-standard

0
On

If you're referring to the so-called "JavaScript Standard Style", that is not a "new standard", unless you mean a new standard your company adopted, rather than an industry standard. That is just one guy's misguided personal standard.

If anything, ES6 introduces more reasons not to omit semicolons:

a = b
[x, y] = [1, 2]

where the second line is array destructuring.

This will be parsed as

a = b[x, y] = [1, 2]

which is certainly not what you want. The "standard" suggests using

a = b
;[x, y] = [1, 2]

which is one of the ugliest things I have ever seen. If you have to put in a semicolon, why not put it on the line above where it belongs?

The above is in addition to all the well-known cases where ASI fails, including the famous

a = b
(function foo() { }())

which will be parsed as

a = b(function(foo() { }())

trying to pass a function as parameter to a function b.

There is no option for what you want in jshint, because there is no need for it. "Unneeded semicolon" is not an error that needs to be picked up by a linter.

Using a custom linter to support a broken "standard" seems like a horrible idea. You'll miss out on all the innovation going on in the wisely-used linters like jshint and eslint. All the more so if your custom linter works only in one particular editor. What if someone on your team wants to use Sublime Text?

I'd strongly suggest revisiting your ill-advised decision to go without semicolons. In surveys of github JS repos, the percentage using this approach was in the single-digits. While you're at it, you could also move from tabs to spaces; the same survey also shows that spaces are overwhelmingly common practice.

Of course, none of the above refers to truly unnecessary semicolons, such as

a = b;;
function foo() { };

which are unnecessary and a linter will generally pick up.