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

我与影子孤独终老i 提交于 2019-12-05 15:02:45

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/34862541/expected-and-instead-saw-jslint-multivar-setting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!