防止 jshint 报告某个变量未用于特定局部变量?

Prevent jshint from reporting that a variable is unused for specific local variables?

当 运行 对我的几个 javascript 文件进行 jshint 时,我收到这样的警告:

file.js: line X, col 93, 'fromParams' is defined but never used.
file.js: line X, col 72, 'toParams' is defined but never used.
file.js: line X, col 63, 'toState' is defined but never used.
file.js: line X, col 56, 'event' is defined but never used.

对于这样的事情:

$rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) {
    // ... some code that doesn't use event, toState, toParams, or fromParams...
});

对于某种类型的回调,这种情况经常出现——回调函数需要一定数量的参数,但我在函数中的代码并没有使用所有参数,所以 jshint 抱怨它们。但是参数必须要有!

应该有一些方法可以在某些代码部分中禁用此警告,例如:

/*jshint -W098 */
$rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) {
/*jshint +W098 */

但由于 jshint 中的错误,它不起作用,请参阅 this open issue

也可以像这样为整个功能禁用此警告:

/* jshint unused:false */

...但这是不可接受的,因为它会抑制函数中 all 未使用变量的警告,我希望收到任何未使用的通知 except 对于我特别知道我不会使用的函数参数。

我有办法解决这个问题吗?我非常希望我的代码不会触发任何 linter 警告,但就目前而言,jshint 会报告几个我不知道如何修复的 "defined but never used" 警告。

根据您的问题和评论,这应该对您有用。

/*global console */

(function () {
    'use strict';

    var jshintUnused;

    (function () {
        return;
    }(jshintUnused));

    function blah(arg1, arg2, arg3) {
        jshintUnused = arg1;
        jshintUnused = arg2;
        console.log(arg3);
    }

    blah(null, null, 'Hello world');
}());

现在将上述方法与 /*jshint unused: false*/

进行比较

jsHint unused

In addition to that, this option will warn you about unused global variables declared via the global directive.

This can be set to vars to only check for variables, not function parameters, or strict to check all variables and parameters. The default (true) behavior is to allow unused parameters that are followed by a used parameter.

/*global console */

(function () {
    'use strict';

    var jshintUnused;

    (function () {
        return;
    }(jshintUnused));

    function blah(arg1, arg2, arg3, oops) {
        jshintUnused = arg1;
        jshintUnused = arg2;

        var hmm;

        console.log(arg3);
    }

    blah(null, null, 'Hello world');
}());

上面就知道oopshmm不应该声明了你会得到。 Warning: unused var: oops, hmm

/*global console */

(function () {
    'use strict';

    function blah(arg1, arg2, arg3, oops) {
        /*jshint unused: false */
        var hmm;

        console.log(arg3);
    }

    blah(null, null, 'Hello world');
}());

在上面的 jsHint 中忽略了整个函数的未使用变量检查,您将不会收到任何警告。

我演示的方法允许您:

Prevent jshint from reporting that a variable is unused for specific local variables?

我提出的另一个建议是使用 arguments.

将要使用的参数分配给函数的局部变量
/*global console */

(function () {
    'use strict';

    function blah() {
        var arg3 = arguments[2];

        console.log(arg3);
    }

    blah(null, null, 'Hello world');
}());

但是根据您的评论,这似乎不符合您的要求。

But the parameters need to be there!

I'm not keen to remove parameters like that. For one, I think it's pretty ugly and a flaw of javascript that you're allowed to do that, but that's just my opinion. But more practically, if I'm using the last parameter I'll need the other ones there.

最后,建议/*jshint unused: vars */

/*global console */

(function () {
    'use strict';

    function blah(arg1, arg2, arg3, oops) {
        /*jshint unused: vars */
        var hmm;

        console.log(arg3);
    }

    blah(null, null, 'Hello world');
}());

当我尝试使用最新的 jsHint 形式 git repo 然后我得到

Four unused variables
8   hmm
6   oops
6   arg2
6   arg1

这不是我所期望的,我会期望的。

Four unused variables
8   hmm

您可以尝试所有这些 online,方法是将它们直接粘贴到界面中。

您可以在函数顶部使用 /* jshint unused:vars */ 来抑制有关函数参数的警告,但仍会收到有关其他变量的警告。

ESLint 为这个用例提供了更好的选择,然后允许你不禁用这个非常重要的规则(它在重构一些代码时节省了我的调试时间)

  1. 默认情况下,如果使用最后一个命名参数(选项 { "args": "after-used" }),它不会对第一个参数发出警告,这很有用,因为您不会总是使用所有收到的回调参数

  2. 您可以为那些可以安全忽略的参数指定一个参数名称模式(选项{ "argsIgnorePattern": "^_" }

http://eslint.org/docs/rules/no-unused-vars

所以如果你有一些像

这样的代码
foo.bar(function (a, b, c, d) {
  var unusedLocal;

  console.log(c);
});

ESLint 只会对 "d""unsusedLocal"

发出警告

如果您真的想保留 "d" 参数,因为它是标准回调签名的一部分,以后可能会用到,"argsIgnorePattern" 会帮到您。更好的是,它使您的整个代码更加明确地说明了有意未使用的变量

foo.bar(function (_a, _b, c, _d) {
  var unusedLocal;

  console.log(c);
});

这一次,ESLint 只会对 "unsusedLocal" 发出警告 并且你会第一眼就知道这段代码中应该使用哪些参数,哪些参数不用。