-
-
Notifications
You must be signed in to change notification settings - Fork 369
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: Avoid or prefer using globals as globalThis.something
#2419
Comments
Drawbacks/exceptions: // Assuming Node.js
const log = globalThis.alert; // undefined
const log = alert; // Uncaught ReferenceError: alert is not defined If the function is called, this makes no difference: // Assuming Node.js
globalThis.alert(); // Uncaught ReferenceError: alert is not defined
alert(); // Uncaught ReferenceError: alert is not defined
new globalThis.Image(); // Uncaught ReferenceError: Image is not defined
new Image(); // Uncaught ReferenceError: Image is not defined |
Name: |
Not sure about this. I do agree that it's nice to avoid it in browser-only contexts, but for cross-platform code, it's necessary. We could ignore cases like Maybe we could exclude it from the recommended preset and in XO, only enable it when the environment is browser-only? |
I suppose that focusing on window.self.alert(); // ❌ `prefer-global-this`
globalThis.self.alert(); // ❌ `no-unnecessary-globalthis`
self.alert(); // ❌ `prefer-global-this`
globalThis.alert(); // ❌ `no-unnecessary-globalthis`
alert(); ✅
See my second comment, in some cases it's indeed required, but in others it doesn't make a difference.
That's what I thought too, but So, safe to replace:
Unsafe to replace:
|
Maybe similar proposal #1959 |
👍 Yeah the core is the same, but this request is more generic, including node. Some of the comments are also covered by another rule: So I'll close that in favor of this and |
Description
Originally posted in #2410 (comment)
I'd lean towards never using
globalThis
(andwindow
,self
,global
, etc) for known globals.globalThis.jQuery = jQuery
should still be allowed though.Fail
Pass
The exception is the same as the one described in #2410 (comment)
Proposed rule name
global-properties
or justavoid-global-propertoes
Additional Info
No response
The text was updated successfully, but these errors were encountered: