Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expected ';' and instead saw ','. - JSLint multivar setting

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 vars 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 a var, let, or const 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?

like image 695
ruffin Avatar asked Jan 18 '16 19:01

ruffin


1 Answers

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

like image 142
David Bradshaw Avatar answered Sep 24 '22 14:09

David Bradshaw