Skip to content
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

Rule proposal: No assignment to global object #144

Open
SamVerschueren opened this issue Jan 16, 2018 · 16 comments
Open

Rule proposal: No assignment to global object #144

SamVerschueren opened this issue Jan 16, 2018 · 16 comments

Comments

@SamVerschueren
Copy link
Contributor

Recently someone did a PR where he put something on the global object. Something like

global.foo = 'bar';

This doesn't feel like a good thing to do. You might accidentally override something. Also it makes it very hard to track where the global variable was set. I don't believe I actually ever used the global keyword in my code.

It's the same thing for window in browser environments.

Just wanted to bring up the discussion as I might be wrong though. If not, should we forbid the use of global entirely? Or only setting values?

@SamVerschueren
Copy link
Contributor Author

Related rule https://eslint.org/docs/rules/no-global-assign. But not sure if it blocks my example. On mobile so can't check.

@sindresorhus sindresorhus changed the title Rule Proposal: No assignment to global object Rule proposal: No assignment to global object Sep 5, 2019
@sindresorhus
Copy link
Owner

@SamVerschueren Can you check?

@brettz9
Copy link
Contributor

brettz9 commented Feb 2, 2020

When the env has node, no-global-assign will only report if one attempts to overwrite the whole variable, e.g., global = 'bar';. See https://runkit.com/brettz9/5e3676fecfe2f1001a4f9159 .

The rule won't report for adding to global properties as in the original post. See https://runkit.com/brettz9/5e36777bfa499e001b98ecb4 .

@brettz9
Copy link
Contributor

brettz9 commented Feb 2, 2020

Particularly in the age of ES modules, this sounds like a great rule.

Even a rule to prevent reading from the global object (besides whitelisted properties) would be great (including for checking imported code, as a kind of privilege checker).

@sindresorhus
Copy link
Owner

I agree, this could be useful.

It should prevent setting properties on window, global, and globalThis.

@sindresorhus
Copy link
Owner

Even a rule to prevent reading from the global object (besides whitelisted properties) would be great (including for checking imported code, as a kind of privilege checker).

I don't see how that's feasible in a browser environment. Pretty much everything are globals.

@sindresorhus
Copy link
Owner

// @fisker Thoughts on this rule?

@fisker
Copy link
Collaborator

fisker commented Feb 3, 2020

I agree, this could be useful.

It should prevent setting properties on window, global, and globalThis.

How do we do with setting window properties, location, name, cookie? those setters are sometimes useful, make a list?

Should self forbid too?

@sindresorhus
Copy link
Owner

We should only disallow setting nonexistent properties.

@sindresorhus
Copy link
Owner

self too, yes.

@fisker
Copy link
Collaborator

fisker commented Feb 3, 2020

By nonexistent properties, what about window.JSON = require('json2') ?

I guess, JSON should considered exist, maybe people could allow it by // eslint-disable

@brettz9
Copy link
Contributor

brettz9 commented Feb 6, 2020

@sindresorhus : If you mean that users need to access their own variables, besides the case of ESM where users don't need to access globals for such functionality, users can add their globals to /* globals */ (or in their .eslintrc.*).

If you mean that the HTML APIs are globals, yes, but I had been thinking the rule could allow permitting certain globals (e.g., a dom option could enable access to document, HTMLElement, etc., some safe ones like Math could always be permitted, etc.).

While one can disable env browser and node to get no-undef reporting (of document, etc.), one can't easily whitelist groups of certain globals only. (Admittedly a dynamic .eslintrc.js file could be written to do this, but a rule appeals to me personally.)

@sindresorhus
Copy link
Owner

sindresorhus commented Feb 12, 2020

I guess, JSON should considered exist, maybe people could allow it by // eslint-disable

Yes, we can document that in the rule docs.

@sindresorhus
Copy link
Owner

If you mean that the HTML APIs are globals

Yes, this is what I meant.

@sindresorhus
Copy link
Owner

I feel like this would be better as just an option to https://eslint.org/docs/rules/no-implicit-globals to prevent assigning to known globals.

@sindresorhus
Copy link
Owner

Someone please open an issue on ESLint about this and link it here. If they decline it, we can consider adding it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants