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: disallow window.document/window.location/window.localStorage in favour of document/location/localStorage #1959

Closed
dimaMachina opened this issue Nov 9, 2022 · 6 comments

Comments

@dimaMachina
Copy link
Contributor

Description

since document/location/localStorage are global variables calling window.* is redundant

Fail

window.localStorage.getItem('foo')

Pass

localStorage.getItem('foo')

Additional Info

No response

@fisker
Copy link
Collaborator

fisker commented Nov 9, 2022

Personally, I prefer only use ES globals, I even do globalThis.setTimeout(...). Anyway I didn't strictly follow this rule nowadays, I use document directly sometime.

@dimaMachina
Copy link
Contributor Author

@fisker The generic rule for allowing/disallowing globalThis/window for global variables will satisfy both cases

@fregante
Copy link
Collaborator

It feels like this was already suggested maybe on eslint's repo, I can't find the issue here.

I do like the rule, I think window.* is either completely unnecessary or it indicates a bad pattern. The exceptions should be in tests and polyfills.

There are a couple of things to exclude though:

  • properties and methods related to the window itself, like window.addEventListener, window.onerror and window.name

@dimaMachina
Copy link
Contributor Author

It feels like this was already suggested maybe on eslint's repo, I can't find the issue here.

even it was already suggested they will reject this rule since it's stylistic rule

@silverwind
Copy link
Contributor

silverwind commented Nov 16, 2022

Adding window or globalThis to globals can break stuff with dumb bundlers, thought that's 100% the bundler's fault of course.

But I do see value in enforcing globalThis or window on certain names so that one does not accidentially use the global when they meant to use a local variable, and I do emulate this to a degree in my config via:

no-restricted-globals: [2, addEventListener, blur, close, closed, confirm, defaultStatus, defaultstatus, error, event, external, find, focus, frameElement, frames, history, innerHeight, innerWidth, isFinite, isNaN, length, location, locationbar, menubar, moveBy, moveTo, name, onblur, onerror, onfocus, onload, onresize, onunload, open, opener, opera, outerHeight, outerWidth, pageXOffset, pageYOffset, parent, print, removeEventListener, resizeBy, resizeTo, screen, screenLeft, screenTop, screenX, screenY, scroll, scrollbars, self, scrollBy, scrollTo, scrollX, scrollY, status, statusbar, stop, toolbar, top]

@fregante
Copy link
Collaborator

fregante commented Aug 1, 2024

@fregante fregante closed this as not planned Won't fix, can't repro, duplicate, stale Aug 1, 2024
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

4 participants