Skip to content
Advertisement

ESLint “no-implicit-globals” rule is being ignored

I’m working with some old Sencha Touch code, written in old-style JavaScript, that I’m trying to clean up. It’s too easy with this code to simply misspell a variable and not see the error, because nothing is being flagged as global or undefined.

I’m trying to use ESLint to address this problem. ESLint is effectively finding other problems, like var instead of let or const, == instead of ===, etc. But variables that should be flagged as undeclared or global are being brashly ignored. For example:

JavaScript

I’m using the default .eslintrc file provided by Visual Studio, with only a few small changes so far. It’s a bit long, but I’ll include it for reference.

Any idea what I might be missing? I don’t see anything in the default rules that would defeat the “no-implicit-globals” rule, and I defined one allowed global (Ext) just in case that was necessary to make the rule kick in.

JavaScript

Advertisement

Answer

You are missing the rule no-undef.

Disallows the use of undeclared variables unless mentioned in /*global */ comments.

The most common reason why I’ve seen this rule being disabled despite being marked as recommended is because it doesn’t work well with TypeScript files, where it is not needed anyway (you can read more in this FAQ).

So, for your use case, a possible solution would be enabling the rule only for .js and .jsx files. You already have that rule disables in "overrides" > "rules", so all you need to do is enabling it in the general "rules" section above:

JavaScript

Note that no-implicit-globals only warns about variables declared in the global scope, not about implicit globals that appear in inner scopes (e.g. in the body of a function).

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement