New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New: no-lexical-globals rule #11981
New: no-lexical-globals rule #11981
Conversation
Thank you for your contribute! I'd like to update
I'm very surprised by the fact. Because |
It might be also good to have an additional step between:
User would probably want to set But, when the same user intends to declare one or two It might be also useful for users who prefer |
Also, I believe that the current message is not in line with the purpose of this rule, and that is to prevent global scope pollution (#4542). E.g. if you have 10 variables, you'll see 10 messages like this:
That's more like encouraging the user to use a different way to pollute the global scope than preventing? Perhaps the rule should suggest two options (like "wrap in IIFE if you want local, assign as global property if you want global"), though I'm not sure how short such message can be. Is there a convention for max message length? |
Sounds good to me.
Sounds good to me. There is no message length limitation currently. |
I'm closing this because it's replaced with #11996 |
What is the purpose of this pull request? (put an "X" next to item)
[X] New rule
Please describe what the rule should do:
Report global variables that are probably intended to be local to the script.
What category of rule is this? (place an "X" next to just one item)
[ ] Enforces code style
[ ] Warns about a potential error
[X] Suggests an alternate way of doing something
[ ] Other (please specify:)
Provide 2-3 code examples that this rule will warn about:
Applies to scripts only, not modules.
Why should this rule be included in ESLint (instead of a plugin)?
It's the best practice to avoid polluting the global scope.
I tried the following code with
"eslint:all"
and got no warnings:What changes did you make? (Give an overview)
New rule.
Is there anything you'd like reviewers to focus on?
The warning message might be too long.
This rule might be merged in
no-implicit-globals
, but I guess that would be a breaking change.These declarations are intentionally excluded from
no-implicit-globals
from the start (see #4565). I'm not sure what was the reason, they will not set properties on the global object but they still create global variables that could clash with variables from other scripts or shadow those directly set on the global object.On the other hand, this might be a separate rule. I'm not sure how useful are
const
andlet
in the global scope, it might be still the best practice to usevar foo = bar
orwindow.foo = bar
for globals. For example, there can be problems with TDZ:I also think that both rules should be in
"eslint:recommended"
, perhaps with a modification to support/* exported */
likeno-unused-vars
.