question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 7 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
not-an-aardvarkcommented, Dec 28, 2016

If the globals are available for all files rather than a particular file, it might be better to specify them in your config:

globals:
  window: true
  document: true
  isUser: true
  # ...
0reactions
broofacommented, Dec 29, 2016

@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 file globals should be interchangeable:

You can define global variables either using comments inside of a file or in the configuration file.

(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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

tslint:disable not working properly - Stack Overflow
I'm using VSCode, and have TSLint plugin installed. Apart from this I also have eslint configured ...
Read more >
eslint-plugin-import | Yarn - Package Manager
A list of regex strings that, if matched by a path, will not report the matching ... no-unused-modules : Fix re-export not counting...
Read more >
[eslint] failed to load plugin 'prettier' declared in '.eslintrc.js'
I solved this issue by removing existing eslint-config-prettier and eslint-plugin-prettier from package.json. Then after re-installed with yarn (or npm) as ...
Read more >
Create-react-app not showing any eslint warnings/error (code ...
Create-react-app not showing any eslint warnings/error (code included) ... if I start the app and have unused import/variable, I can see the ...
Read more >
Eslint ignore - Caritas Castellaneta
ESLint supports globbing files. eslintignore File You can tell ESLint to ... but never used no-unused-vars 2:1 error Opening curly brace does not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found