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 es 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 plains 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.
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 es 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 plains 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.
Share Improve this question asked Jun 5, 2015 at 18:24 Bri BriBri Bri 2,2683 gold badges24 silver badges65 bronze badges 10-
Add
/*jshint unused:false*/
in the scope of the (the first line inside the function) function should do it. You can't be specific to a particular variable, not that I know of. This will only be enforced in the function's scope. – Xotic750 Commented Jun 5, 2015 at 18:33 - 1 If you don't need any of the parameters you can remove them... they don't need to be there, – Brian Glaz Commented Jun 5, 2015 at 18:38
-
1
I was going to add that. :) Another alternative (in some worst case where jshint is more important, but not necessarily, than your actual code) is to create
var
s inside the function and assign them witharguments[n]
– Xotic750 Commented Jun 5, 2015 at 18:41 - 1 @Xotic750 I did mention /* jshint unused: false */ in my original post, and why I'm not able to use it: it would suppress all warnings for that entire function, and I need to know if there are other unused variables aside from the unused parameters. – Bri Bri Commented Jun 5, 2015 at 19:03
- @BrianGlaz - 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. – Bri Bri Commented Jun 5, 2015 at 19:06
3 Answers
Reset to default 6You can use /* jshint unused:vars */
at the top of the function to suppress warnings about function parameters but still get warnings about other variables.
This should work for you based on your question and ments.
/*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 pare 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 ments.
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.
ESLint offer better options for this use case which then allows you to not disable this very important rule (it saved me debugging time when refactoring some code)
by default, it won't throw warning for the first arguments if the last named argument is used (option
{ "args": "after-used" }
), which is useful as you won't always use all the received callback parametersyou can specify a parameter name pattern for those that can safely be ignored (option
{ "argsIgnorePattern": "^_" }
http://eslint/docs/rules/no-unused-vars
So if you have some code like
foo.bar(function (a, b, c, d) {
var unusedLocal;
console.log(c);
});
ESLint will only raise warnings for "d"
and "unsusedLocal"
If you really want to leave the "d" parameter beacuse it is part of the standard callback signature and might be used later, the "argsIgnorePattern"
es to your help. And even better, it makes your whole code more explicit about intentionnaly unused variables
foo.bar(function (_a, _b, c, _d) {
var unusedLocal;
console.log(c);
});
This time, ESLint will only raise warnings for "unsusedLocal"
And you will know at first look which parameters are expected to be used or not in this code.