The rule has exceptions:
eslint-plugin-unicorn/rules/prefer-global-this.js
Lines 8 to 121 in 2b469be
const globalIdentifier = new Set(['window', 'self', 'global']); | |
const windowSpecificEvents = new Set([ | |
'resize', | |
'blur', | |
'focus', | |
'load', | |
'scroll', | |
'scrollend', | |
'wheel', | |
'beforeunload', // Browsers might have specific behaviors on exactly `window.onbeforeunload =` | |
'message', | |
'messageerror', | |
'pagehide', | |
'pagereveal', | |
'pageshow', | |
'pageswap', | |
'unload', | |
]); | |
/** | |
Note: What kind of API should be a windows-specific interface? | |
1. It's directly related to window (β window.close()) | |
2. It does NOT work well as globalThis.x or x (β window.frames, window.top) | |
Some constructors are occasionally related to window (like Element !== iframe.contentWindow.Element), but they don't need to mention window anyway. | |
Please use these criteria to decide whether an API should be added here. Context: https://github.com/sindresorhus/eslint-plugin-unicorn/pull/2410#discussion_r1695312427 | |
*/ | |
const windowSpecificAPIs = new Set([ | |
// Properties and methods | |
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#the-window-object | |
'name', | |
'locationbar', | |
'menubar', | |
'personalbar', | |
'scrollbars', | |
'statusbar', | |
'toolbar', | |
'status', | |
'close', | |
'closed', | |
'stop', | |
'focus', | |
'blur', | |
'frames', | |
'length', | |
'top', | |
'opener', | |
'parent', | |
'frameElement', | |
'open', | |
'originAgentCluster', | |
'postMessage', | |
// Events commonly associated with "window" | |
...[...windowSpecificEvents].map(event => `on${event}`), | |
// To add/remove/dispatch events that are commonly associated with "window" | |
// https://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-flow | |
'addEventListener', | |
'removeEventListener', | |
'dispatchEvent', | |
// https://dom.spec.whatwg.org/#idl-index | |
'event', // Deprecated and quirky, best left untouched | |
// https://drafts.csswg.org/cssom-view/#idl-index | |
'screen', | |
'visualViewport', | |
'moveTo', | |
'moveBy', | |
'resizeTo', | |
'resizeBy', | |
'innerWidth', | |
'innerHeight', | |
'scrollX', | |
'pageXOffset', | |
'scrollY', | |
'pageYOffset', | |
'scroll', | |
'scrollTo', | |
'scrollBy', | |
'screenX', | |
'screenLeft', | |
'screenY', | |
'screenTop', | |
'screenWidth', | |
'screenHeight', | |
'devicePixelRatio', | |
]); | |
const webWorkerSpecificAPIs = new Set([ | |
// https://html.spec.whatwg.org/multipage/workers.html#the-workerglobalscope-common-interface | |
'addEventListener', | |
'removeEventListener', | |
'dispatchEvent', | |
'self', | |
'location', | |
'navigator', | |
'onerror', | |
'onlanguagechange', | |
'onoffline', | |
'ononline', | |
'onrejectionhandled', | |
'onunhandledrejection', | |
// https://html.spec.whatwg.org/multipage/workers.html#dedicated-workers-and-the-dedicatedworkerglobalscope-interface | |
'name', | |
'postMessage', | |
'onconnect', | |
]); |
But these are not checked in existence checks, so the rule just suggested I replace 'open' in window
while leaving window.open()
behind
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too