Reputation: 14039
I have made a sample JavaScript file to illustrate my confusion.
(function() { console.log(true == true); })()
This should trip two errors; first the use of ==
instead of ===
, and second, a missing semicolon. However, when I run
var jshint = require('jshint').JSHINT;
jshint('(function() { console.log(true == true); })()');
on in the Node REPL I get errors when I don't expect to. The reason I expect no errors is because in the JSHint documents, it says:
The second parameter [to jshint] is an optional object of options which control the operation of JSHINT. Most of the options are booleans: They are all optional and have a default value of false.
I would expect this to produce no errors as I've defined no options so all should be false. The final part of unexpected behavior is that setting options seems to do nothing. The following two calls to jshint also produce the same errors:
var myFile = '(function() { console.log(true == true); })()';
jshint(myFile, {eqeqeq: false, asi: false});
jshint('/*jshint eqeqeq:false, asi:false */\n' + myFile);
I'm not sure how the JSHint options work and I very well may be misinterpreting the documentation. I'd appreciate knowing what in either my invocation of jshint
above or in my assumptions is incorrect or if there actually is a problem with JSHint.
Upvotes: 1
Views: 2216
Reputation: 2808
Not all options are off by default. In our docs (http://www.jshint.com/docs/) you can see that some options are 'enforcing' and some 'relaxing'[1]. This means that some warnings will be displayed by default and you need to enable 'relaxing' option to turn them off.
Now, with this information, let's look at your second question:
/*jshint eqeqeq:false, asi:false */
(function () { console.log(true == true); })()
This code will trigger an unsafe comparison warning for == true
and a missing semicolon. You tried to fix that by turning off two options eqeqeq and asi. The former, when set to true, requires strict comparison everywhere and is turned off by default so you can omit it. You get a warning because JSHint considers == true/false/0/null
comparisons unsafe and currently there is no way to turn that off (there won't be warning for a == b
for example)[2]. And the latter option (asi) is a relaxing option so you actually need to turn it on to tell JSHint that it's okay to tolerate missing semicolons. So to make your example pass you will need to change it this way:
/*jshint asi:true */
(function () { console.log(true === true); }()
[1] — I understand that this is confusing. Because of backwards compatibility I can't just change how options work but I'm working towards making it less confusing. I will add default values for each option to the docs soon-ish.
[2] — The upcoming 1.0.0 release will allow you to ignore any warnings by their code, even if there's no corresponding named option.
Upvotes: 6