prefer-global-this
Added in v0.10.1Configuration
Rule Details
Prefer globalThis over window, self, and global to keep code portable
between browsers, Node.js, and other JavaScript runtimes.
This rule reports references to those global objects, including references not
declared in languageOptions.globals. Local bindings are respected.
Examples of incorrect code:
Examples of correct code:
Window-specific APIs and events remain allowed, as do computed accesses on
window, runtime typeof checks, and TypeScript typeof type queries:
Fixes
The rule automatically replaces read references. Shorthand object properties
retain their keys: { window } becomes { window: globalThis }. Writes such as
window = value are reported without a fix because replacing the assignment
target could overwrite globalThis.
Options
This rule has no options.
Differences from upstream
Unlike upstream, the following references are reported without an automatic fix:
- A local value named
globalThiswould change what the replacement refers to. For example,function f(globalThis) { return window; }must keep returning the global object, rather than the parameter. Rename the local binding to enable the fix. - A paired JSX tag in a JavaScript file, such as
<window.Widget></window.Widget>, must keep its opening and closing names in sync. Change both names manually. Self-closing tags and TypeScript TSX tags retain automatic fixes.