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.

no-unused-vars behavior with global variables

See original GitHub issue

When the scope is all, and you declare a global variable in one file:

// file1.js
myGlobal = 123;

but you only consume the variable in another file:

// file2.js
console.log(myGlobal);

ESLint doesn’t pass (because it’s only looking at each file individually). Since almost invariably global variables are used across files, this really reduces the utility of the all option.

Any chance ESLint can do a quick pass across all files first so it’s aware of global var definitions and usages? Thanks for the awesome project @nzakas !

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
nzakascommented, May 29, 2015

I’m sorry, this is very far outside the scope of what ESLint is meant to do and I don’t see addressing this. I think the best advice we can offer is that you build a tool that grabs global definitions from your files and outputs an eslintrc file with the globals predefined. Then, you can use extends to import it.

0reactions
qodesmithcommented, May 25, 2016

I was experiencing the same thing. I ended up just putting all the globals I was using inside the globals property within my .eslintrc.js file. The only drag was that each one had to be named individually like such:

'globals': {
  'var1': true,
  'var2': true,
  ...
}

Since each global is going to get a value of true anyway, would have been nice to be able to do it in an array like so:

'globals': {
  ['var1', 'var2', 'var3']: true
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

ESLint - Configuring "no-unused-vars" for TypeScript
My issue was with using decorators and wanting to have a variable with an appropriate name for clarity, for example:.
Read more >
no-unused-vars - ESLint - Pluggable JavaScript Linter
This rule is aimed at eliminating unused variables, functions, and function ... /*eslint no-unused-vars: "error"*/ /*global some_unused_var*/ // It checks ...
Read more >
Global Variables - Opsive
Global Variables. Global variables are similar to local variables except any behavior tree can access an instance of the same variable.
Read more >
The 3 Best ESLint No-Unused-Vars Option Settings (Make it ...
The no-unused-vars rule is one of the most important ESLint rules for keeping ... All three of these options change the default behavior....
Read more >
Why global variables are bad - Code with Jason
Why are global variables so often advised against? ... involve the instance variable in order to understand the behavior of the function.
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