GuyGizmo GuyGizmo - 14 days ago 5
Javascript Question

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

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.

Answer

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.