Expected an assignment or function call and instead saw an expression

孤人 提交于 2019-12-18 10:48:38

问题


I'm totally cool with this JSLint error. How can I tolerate it? Is there a flag or checkbox for it?

You get it when you do stuff like:

v && arr.push(v);

as opposed to:

if (v) {
    arr.push(v);
}

Both do the same exact thing. If you put:

window.test = function(v) {
    'use strict';
    var arr = [];
    if (v) {
        arr.push(v);
    }
    return arr;
};

into the minifier it minifies down to this anyway:

window.test=function(a){var b=[];a&&b.push(a);return b};

回答1:


I don't think JSLint has an option to turn that off.

JSHint (a fork with more options) has an option for it, though: The expr option, documented as "if ExpressionStatement should be allowed as Programs".




回答2:


You can add the following line to ignore that warning:

/*jshint -W030 */

You can read more about it here.




回答3:


There's no option for this in JSLint. You can circumvent it using:

var dummy = v && arr.push(v);

NB: dummy evaluates to true after that.

Another workaround could be:

function expression(statement) { 
 'use strict';
 return statement; 
}
expression(v && arr.push);



回答4:


People who are looking how to suppress it when using ESLint. You can ingnore it by writing the following comment just above the line.

// eslint-disable-next-line no-unused-expressions

You can also suppress the warning for the entire file by placing the following comment at the very top of the file

/* eslint-disable no-unused-expressions */


来源:https://stackoverflow.com/questions/9534031/expected-an-assignment-or-function-call-and-instead-saw-an-expression

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