2

I'm working on an angular app and am having issues with jslint complaining about an unused parameter. As you may know, controllers in angular require "$scope" to be passed as the first parameter in your controller definition. In my specific case, I prefer not to use the $scope object, but instead use the "this" keyword. I can make the code work using either $scope or this, but for other reasons that I don't think are relevant, I need to use "this" and not "$scope". If you must know why, I'll be happy to provide a link that helps explain why I'm making this judgement call.

I found this article that explains how you can turn off this warning for all unused parameters.

https://jslinterrors.com/unused-a

This works, but it's not ideal because then I can't see if there are other unused parameters besides the one I already know about (and am ok with).

I'm aware that you can turn the unparam warning on,then turn it back off again, but this still does not solve my problem because it's the rest of the code block that I want to allow jslint to check for, just not this specific variable ($scope). I have a ton of these types of files that, unfortunately, all share similar issues. Here is my code:

/*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'
            };
        });

}());

This is as close as I've been able to get to what I'm looking for. For what it's worth, I've also tried adding $scope to the globals section at the top and that doesn't suppress the warning either. What I'd like to be able to do is something like this

/*jslint unparam: "$scope" */

or

/*jslint unparam: [$scope] */

So that jslint only ignores only this one unused parameter, but still warns about any other unused parameters. Any advice you can give would be great. Let me know if you need more detail on any of this, I'd be happy to provide. Thanks in advance!!!

seanforyou23
  • 126
  • 1
  • 8
  • Remove all _unused_ warning by one stroke of code? – Appeiron Nov 04 '15 at 16:50
  • Anything I can add to my answer to hit what you're looking for? – ruffin Nov 12 '15 at 15:13
  • @ruffin I'm actually running jslint from the command line (on a mac) so I believe I'm using the "old" jslint from the way you are describing it in your comment. A couple things: 1) When running `jslint --version` I get the following "node-jslint version: 0.9.3 JSLint edition 2013-08-26" in case that helps. 2) I've checked to ensure my local jslint isn't outdated by running `npm outdated -g --depth=0` and I don't get any mention of jslint in the output so I think I'm good there. I'd prefer not to hack on the actual jslint core so I think I'm stuck using unparam for now. – seanforyou23 Nov 17 '15 at 16:11

1 Answers1

2

Two answers. First if you're stuck using "Old JSLint", and the second, much easier, case if you're on "New JSLint" that came out earlier this year and recently left beta.


Old JSLInt

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

Luckily, depending on how you're using JSLint, hacking could be pretty easy.

I'm going to use the last version of JSLint before the "New JSLint" beta, from July 8, 2014.

Go to line 3177. Change the existing code from...

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

... to this kludged version...

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');
}

... and voila. You're done.

If you want to make a more complicated case, you've got lots of options for adding options. You could hack JSLint's options to add your new unparam syntax. You could set an array of ignoreable param names as a slightly kludged global object and use indexOf to check on that changed line instead of a single string comparison. Whatever.

But if this is a one-shot deal, the hack, above, is the quick and dirty method. You get the point. The neat thing about JSLint is that you can customize as much as you like using the same language you're linting, JavaScript.

Again, how to start using the new code depends on how you're calling JSLint.


New JSLint

If you're using the latest jslint, you're in some luck; you can use the ignore convention to skip $scope each time. Here's an example, using your code as a starting place:

/*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 is nearly perfect for this use case, as the unused param is the first param, and you can only use ignore once per function signature/declaration.

Community
  • 1
  • 1
ruffin
  • 16,507
  • 9
  • 88
  • 138