Open fregante opened 2 months ago
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: no-unnecessary-globalthis
?
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 globalThis.alert?.()
, but there are many ways to use APIs that may or may not be available, and I worry that this rule will be annoying.
Maybe we could exclude it from the recommended preset and in XO, only enable it when the environment is browser-only?
Name:
no-unnecessary-globalthis
?
I suppose that focusing on globalThis
simplifies the logic and leaves window
-specific exclusions to prefer-global-this
:
window.self.alert(); // ❌ `prefer-global-this`
globalThis.self.alert(); // ❌ `no-unnecessary-globalthis`
self.alert(); // ❌ `prefer-global-this`
globalThis.alert(); // ❌ `no-unnecessary-globalthis`
alert(); ✅
it's necessary
See my second comment, in some cases it's indeed required, but in others it doesn't make a difference.
globalThis.alert?.()
would indeed have to be an exception since alert?.()
won't catch reference errors. Related:
when the environment is browser-only?
That's what I thought too, but globalThis.fetch()
and globalThis.atob()
is also available on Node.
So, safe to replace:
?.
globalThis.location.href
, new globalThis.Element()
Unsafe to replace:
const fn = globalThis.alert;
-> Function
in browser, undefined
in Node?.
globalThis.alert?.()
Maybe similar proposal https://github.com/sindresorhus/eslint-plugin-unicorn/issues/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 prefer-global-this
Description
Originally posted in https://github.com/sindresorhus/eslint-plugin-unicorn/issues/2410#issuecomment-2255792543
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 https://github.com/sindresorhus/eslint-plugin-unicorn/pull/2410#discussion_r1695312427
Proposed rule name
global-properties
or justavoid-global-propertoes
Additional Info
No response