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

10,338

Solution 1

You can use /* jshint unused:vars */ at the top of the function to suppress warnings about function parameters but still get warnings about other variables.

Solution 2

This should work for you based on your question and comments.

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

Now compare the above method against /*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');
}());

The above will know that oops and hmm shouldn't have been declared and you will get. 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');
}());

In the above jsHint ignored the unused variable check for the entire function and you will get no warnings at all.

The method that I have demonstrated allows you to:

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

The other suggestion that I made was to assign the parameters to be used to a variable local to the function using arguments.

/*global console */

(function () {
    'use strict';

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

        console.log(arg3);
    }

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

But this didn't seem to fit your requirements based on your comments.

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.

Finally, /*jshint unused: vars */ is suggested.

/*global console */

(function () {
    'use strict';

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

        console.log(arg3);
    }

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

When I try this with the latest jsHint form the git repo then I get

Four unused variables
8   hmm
6   oops
6   arg2
6   arg1

Which was not what I expected, I would have expected.

Four unused variables
8   hmm

You can try all of these online by pasting them directly into the the interface.

Share:
10,338
Bri Bri
Author by

Bri Bri

I like to computer. When I computer I often: use a Mac and make it do strange, twisted, power-usery things much to the chagrin of my Mac using friends tear out my hair trying to do any sort of development in Autodesk Maya create animation software and maybe even figure out a new algorithm for something interesting write software for graphical and multi-touch analysis make mouse gestures or multi-touch gestures do cool things work on a fun game have something make a fart noise

Updated on July 25, 2022

Comments

  • Bri Bri
    Bri Bri almost 2 years

    When running jshint on several of my javascript files, I get warnings like this:

    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.
    

    For something like this:

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

    This comes up very often for callbacks of one sort or another -- the callback function requires a certain number of parameters, but my code in the function doesn't use all of the parameters, so jshint complains about them. But the parameters need to be there!

    There's supposed to be ways of disabling this warning in certain sections of code like this:

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

    But it doesn't work due to a bug in jshint, see this open issue.

    It's also possible to disable this warning for entire functions like so:

    /* jshint unused:false */
    

    ...but this is unacceptable, because it would suppress the warning for all unused variables in the function, and I want to be notified about anything that's unused except for the function parameters I specifically know I'm not going to use.

    Is there anyway for me to work around this? I'd very much like my code to not trigger any linter warnings, but as it stands, jshint will report several "defined but never used" warnings that I don't know how to fix.

  • cxw
    cxw over 6 years
    Thanks - your first method looks quite nice! Would you be willing to dual-license it MIT or CC-BY? (All of my contributions are dual-licensed CC-BY, so I'm not asking you to do anything I haven't myself :) .)