jshint: Redefinition bugs

I am not sure if this is my mistake but it seems like a bug to me, jshint v2.9.1.

I am getting redefinition warnings on:

var setup = require('../setup/home');

And:

var suite = new benchmark.Suite();

Feel free to correct me but this appears to be a regexp/comparison that is a little too simple.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 21 (4 by maintainers)

Most upvoted comments

If I am correct that predef is set when mocha is enabled then I think the behaviour should be changed so that they are not set as predef but only detected when mocha is in use.

That kind of behavior would probably surprise many people.

The other thing of course is that when I use var in a module I expect the variable to be local to that module, however, if I had omitted the var then I would expect the redefinition warning.

I agree with you on this. I have submitted a patch so that in Node.js environments, JSHint recognizes that var statements in seemingly-global code do not resolve to the global bindings–see gh-2877.

So while JSHint is definitely misbehaving here, I recommend that you extend your configuration to only enable the mocha option for files that are actually executed in an environment where mocha has been loaded. This is not simply a workaround to the bug; it’s actually safer overall. When your entire project is configured with mocha: true, JSHint cannot consistently detect references to undefined variables.

For instance, a source file that is not executed with Mocha might contain the expression test(123) without a corresponding declaration for test, and JSHint would not issue a warning. That will be a risk even when the underlying bug is fixed.

This can be done by creating distinct configuration files for different directories within your project. In the case of https://github.com/tmpfs/rlx, that would involve a new file at test/.jshintrc. You can limit duplication by using the extends option.