As of about 14 January 2016, JSLint has started complaining about var
or let
declarations that have more than one variable per declaration, and has created a new directive, multivar
that ignores this new "problem".
This is a pretty significant change, as earlier versions would complain if you did have two var
s in the same code block.
That is, as of today (18 Jan 2016), this code now breaks in JSLint:
/*jslint white:true, browser:true, devel:true */
function a(b) {
"use strict";
var c, d; // <<< Now bad!!
d = "test";
c = d + b;
console.log(c);
}
The error reported is, Expected ';' and instead saw ','.
for the line var c,d;
The "correct" fix is apparently this:
/*jslint white:true, browser:true, devel:true */
function a(b) {
"use strict";
var c;
var d; // <<< this *used* to be forbidden.
d = "test";
c = d + b;
console.log(c);
}
Note that this newly correct code would have produced the error, Error: combine_var
, in earlier versions of JSLint.
The only descriptions for the change from Crockford that I can find seem to be this post on Google Plus:
JSLint now has a
multivar
option that tolerates multiple names being declared in a single var, let, or const statement.
... and a quick mention on the website instructions...
Tolerate multiple variables declarations per statement
multivar
true
if avar
,let
, orconst
statement can declare two or more variables in a single statement.
The multivar
change on JSLint.com doesn't seem to be in the GitHub repo yet. See the two checkins (1, 2) entitled "var" for 14 Jan. Those two make the code in JSLint follow the new requirement, but don't (afaict) add the multivar
directive described and in use on JSLint.com.
Can anybody tell me why multiple var
lines are encouraged/required now, other than the usual, "You should ignore JSLint," answers? That is, why was a single var required before (I'd guess to encourage an understanding of hoisting), and why is that reasoning suddenly moot?
From the AirBNB style guide.
It's easier to add new variable declarations this way, and you never have to worry about swapping out a ; for a , or introducing punctuation-only diffs. You can also step through each declaration with the debugger, instead of jumping through all of them at once.
More on this at: https://github.com/airbnb/javascript#variables
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With