【问题标题】:How to ignore (only) specific parameters in jslint unparam block如何忽略(仅)jslint unparam 块中的特定参数
【发布时间】:2016-02-05 06:33:56
【问题描述】:

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

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

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 添加到顶部的 globals 部分,这也不会抑制警告。我想做的是这样的

/*jslint unparam: "$scope" */

/*jslint unparam: [$scope] */

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

【问题讨论】:

  • 一笔删除所有unused警告?
  • 任何我可以添加到我的答案中以达到您正在寻找的内容?
  • @ruffin 我实际上是从命令行(在 Mac 上)运行 jslint,所以我相信我使用的是您在评论中描述它的方式的“旧”jslint。几件事:1)运行jslint --version时,我得到以下“node-jslint版本:0.9.3 JSLint edition 2013-08-26”以防万一。 2) 我已经通过运行npm outdated -g --depth=0 检查以确保我的本地 jslint 没有过时,并且我没有在输出中提到 jslint,所以我认为我在那里很好。我不想破解实际的 jslint 核心,所以我认为我现在无法使用 unparam。

标签: javascript angularjs jslint


【解决方案1】:

两个答案。第一种情况是您无法使用“Old JSLint”,第二种情况要容易得多,如果您使用的是今年早些时候推出且最近退出测试版的“New JSLint”。


旧的 JSLInt

“旧 JSLint”的底线:如果不进行黑客攻击,您将无法做到这一点。

幸运的是,根据您使用 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 的选项来添加新的 unparam 语法。您可以将一组可忽略的参数名称设置为一个稍微混乱的全局对象,并使用indexOf 来检查更改的行而不是单个字符串比较。随便。

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

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


新的 JSLint

如果您使用的是the latest jslint,那么您很幸运;您可以使用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 非常适合这个用例,因为未使用的参数是第一个参数,并且每个函数签名/声明只能使用一次 ignore

【讨论】:

    猜你喜欢
    • 2013-06-16
    • 2012-11-29
    • 2011-12-13
    • 2023-03-13
    • 1970-01-01
    • 2012-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多