如何忽略(仅)jslint unparam 块中的特定参数

How to ignore (only) specific parameters in jslint unparam block

我正在开发一个 angular 应用程序,但遇到 jslint 抱怨未使用参数的问题。如您所知,angular 中的控制器要求将“$scope”作为控制器定义中的第一个参数传递。在我的具体情况下,我不喜欢使用 $scope 对象,而是使用 "this" 关键字。我可以使用 $scope 或 this 使代码工作,但由于其他我认为不相关的原因,我需要使用 "this" 而不是“$scope”。如果您一定要知道为什么,我很乐意提供一个 link 来帮助解释我为什么要做出这样的判断。

我发现这篇文章解释了如何为 所有 个未使用的参数关闭此警告。

https://jslinterrors.com/unused-a

这行得通,但它并不理想,因为那样我就看不到除了我已经知道的参数之外是否还有其他未使用的参数(并且没问题)。

我知道您可以打开 unparam 警告,然后再将其关闭,但这仍然不能解决我的问题,因为这是我 想要的代码块的其余部分 允许 jslint 检查,只是不检查这个特定变量($scope)。我有大量此类文件,不幸的是,它们都有类似的问题。这是我的代码:

/*globals angular, console, document */

(function() {
    'use strict';

    /**
     * @ngdoc function
     * @name app.controller:UserLogoutController
     * @description
     * Loggs out the current active user
     *
     */


    /*jslint unparam: true */
    angular.module('myAppName')
        .controller('UserLogoutController', ['$scope', 'userService', 'appConfig',
            function ($scope, userService, config) {

                this.logout = function() {

                    //logout logic goes here

                };
            }
            ]);

    /*jslint unparam: false */
    angular.module('myAppName')
        .directive('logoutbtn', function() {
            return {
                restrict: 'E',
                replace: true,
                templateUrl: './user/userLogout.html',
                controller: 'UserLogoutController',
                controllerAs: 'logout'
            };
        });

}());

这是我能够找到的最接近的内容。对于它的价值,我还尝试将 $scope 添加到顶部的全局部分,这也不会抑制警告。我想做的是这样的

/*jslint unparam: "$scope" */

/*jslint unparam: [$scope] */

所以 jslint 只忽略这个未使用的参数,但仍然警告任何其他未使用的参数。你能给的任何建议都会很棒。如果您需要更多详细信息,请告诉我,我很乐意提供。提前致谢!!!

两个答案。首先,如果您坚持使用今年早些时候发布且最近离开测试版的“Old JSLint", and the second, much easier, case if you're on "New JSLint”。


旧 JSLInt

Bottom line for "Old JSLint": You can't do this without hacking.

幸运的是,根据您使用 JSLint 的方式,黑客攻击可能非常容易。

我要使用 the last version of JSLint before the "New JSLint" beta, from July 8, 2014

前往 line 3177。更改现有代码...

if (!master.used && master.kind !== 'exception' &&
        (master.kind !== 'parameter' || !option.unparam)) {
    master.warn('unused_a');
} else if (!master.init) {
    master.warn('uninitialized_a');
}

...到这个乱七八糟的版本...

if (!master.used && master.kind !== 'exception' &&
        (master.kind !== 'parameter' || !option.unparam)
        && "$scope" !== master.string // <<< Add your exception.
) {
    master.warn('unused_a');
} else if (!master.init) {
    master.warn('uninitialized_a');
}

...瞧。大功告成。

如果你想做一个更复杂的案例,你有很多选项可以添加选项。您可以破解 JSLint 的选项以添加新的非参数语法。您可以将一组可忽略的参数名称设置为稍微混乱的全局对象,并使用 indexOf 检查更改的行而不是单个字符串比较。随便。

但如果这是一次性交易,上面的 hack 是快速而肮脏的方法。你明白了。 JSLint 的巧妙之处在于,您可以使用与 linting 相同的语言进行尽可能多的自定义,JavaScript.

同样,如何开始使用新代码取决于您调用 JSLint 的方式。


新的 JSLint

如果您每次都使用 the latest jslint, you're in some luck; you can use the ignore convention 跳过 $scope。这是一个示例,使用您的代码作为起点:

/*jslint white:true, this:true */
/*globals angular, console, document */

(function() {
    'use strict';

    /**
     * @ngdoc function
     * @name app.controller:UserLogoutController
     * @description
     * Loggs out the current active user
     *
     */


    angular.module('myAppName')
        .controller('UserLogoutController', ['$scope', 'userService', 'appConfig',
            function (ignore, userService, config) {

                this.logout = function() {
                    console.log("logout logic goes here");
                };
            }
            ]);

    angular.module('myAppName')
        .directive('logoutbtn', function() {
            return {
                restrict: 'E',
                replace: true,
                templateUrl: './user/userLogout.html',
                controller: 'UserLogoutController',
                controllerAs: 'logout'
            };
        });

}());

ignore 对于这个用例来说几乎是完美的,因为未使用的参数是第一个参数,并且每个函数 signature/declaration.[=23= 只能使用一次 ignore ]