Consider the following standard Express function:
if (app.get('env') === 'development') {
app.use(function (err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
The following warning is displayed:
'next' is defined but never used. - W098
I know I can disable all warnings, but that's not what I want. What I need is to silence JSHint only for a specific parameter ('next' in the snippet above.)
I haven't seen any clear answers to this seemingly common issue. Any ideas?
Thanks!
Yes, this is possible!
If you are using the default setup of JSHint, the accepted answer on this thread is your answer too: Is there a way to suppress JSHint warning for one given line? It shows how to use comments to conditionally ignore specific errors/warnings from being thrown in JSHint.
If you are using a build tool like Gulp or Grunt, you can add this to your options in your plugin's task configuration. For example, with Grunt: See section "Ignoring specific warnings" on https://github.com/gruntjs/grunt-contrib-jshint/blob/master/README.md.
Hope this helps!
In this case you can do something like below
if (app.get('env') === 'development') {
app.use(function (err, req, res, next) {
/*jshint unused: vars*/
var foo;
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
Now jshint
will not cry about next
, though it will still warn about foo
being defined but not used.
Importantly, This suppression will be bound to scope of the function.