6

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!

titusmagnus
  • 2,014
  • 3
  • 23
  • 23
  • possible duplicate of [Prevent JSHint warning that 'functionName is defined but never used'](http://stackoverflow.com/questions/12607289/prevent-jshint-warning-that-functionname-is-defined-but-never-used) – Paolo Moretti Aug 31 '14 at 19:22
  • 1
    I'm afraid that what you're asking is not possible. Essentially you can add `unused: vars` in your `.jshintrc` or in your file, but I don't think you can disable it only for a specific parameter. – Paolo Moretti Aug 31 '14 at 19:26
  • "*[...] this seemingly common issue.*" This is actually rather unusual with Express requiring 4 named parameters to recognize error handlers. In most cases, the `length` of a function isn't significant and removing the additional parameter to satisfy the warning wouldn't affect how the application operates. – Jonathan Lonowski Aug 31 '14 at 19:42
  • The [2nd answer](http://stackoverflow.com/a/13028466) to the question Paolo linked to is probably the closest possible -- disable the `unused` warning within that `function`. JSHint's ["inline configuration" comments](http://jshint.com/docs/) "*are function scoped meaning that if you put them inside a function they will affect only this function's code.*" – Jonathan Lonowski Aug 31 '14 at 19:53
  • Jonathan, Express 4 *requires* an arity of 4 for error handling: "Error-handling middleware is defined just like regular middleware, except that must be defined with an arity of 4 (that is the signature (err, req, res, next))". See http://expressjs.com/guide.html#error-handling – titusmagnus Aug 31 '14 at 22:28

2 Answers2

3

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.

kushdilip
  • 7,606
  • 3
  • 24
  • 30
1

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!

Community
  • 1
  • 1
  • While not exactly what I was looking for, adding "// jshint ignore:line" after the offending line silences the warning. It may swallow other warnings, but in my case it doesn't, so it works fine. – titusmagnus Aug 31 '14 at 22:30