【发布时间】: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