eslint-configured globals should not be reported as unused
See original GitHub issue- ESLint Version: v3.11.1
- Node Version: v6.9.1
- npm Version: 4.0.5
What parser (default, Babel-ESLint, etc.) are you using? default
Please show your full configuration:
See cat .eslintrc.json
, below
What did you do? Please include the actual source code causing the issue.
kieffer@Roberts-MacBook-Pro$ cat .eslintrc.json
{
"rules": {
"no-unused-vars": "warn",
"no-undef": "error"
}
}
kieffer@Roberts-MacBook-Pro$ cat foo.js
/* global foo */
kieffer@Roberts-MacBook-Pro$ eslint foo.js
/Users/kieffer/work/foo.js
1:11 warning 'foo' is defined but never used no-unused-vars
✖ 1 problem (0 errors, 1 warning)
What did you expect to happen?
Since foo
is never actually declared in real code, it should not be reported as being unused.
What actually happened? Please include the actual, raw output from ESLint.
The comment-configured global foo
is reported as unused, despite not being defined anywhere in real code.
“Just remove the unused var(s) from the configuration comment, you moron!”
The use-case for this is that I have code that dynamically creates helper methods for each of our DB models. Something along the lines of …
// Dynamically create some helper functions based on DB model classes
for (var modelName in models) global['is' + modelName] = function(thing) {...}
// Tell eslint about the functions we just created
/* global isUser, isAccount, isSession, etc... */
This configuration comment avoids the more serious no-undef
errors if/when a helper function is used. But I end up with no-unused-vars
warnings for the ones that aren’t. It’s a lose-lose situation, as far as getting eslint to shut up goes. 😕
Make sense?
Issue Analytics
- State:
- Created 7 years ago
- Comments:12 (6 by maintainers)
Top GitHub Comments
If the globals are available for all files rather than a particular file, it might be better to specify them in your config:
@not-an-aardvark TIL And feel like a bit of an idiot. Thanks for correcting me. 'Not sure why I thought
global
was scoped to a module.I need to ponder the implications of that for my own code. It may be I should be doing this another way.
I do think there’s still an issue here. The eslint docs imply inline
global
and config fileglobals
should be interchangeable:(link). That they’re not is (to me) a little weird. But given my own misunderstanding of what my code is doing I’ll concede this isn’t worth attending to for the time being. Sorry to have troubled you folks.