Skip to content

Latest commit

Β 

History

History
112 lines (80 loc) Β· 1.84 KB

File metadata and controls

112 lines (80 loc) Β· 1.84 KB

prefer-global-this

πŸ“ Prefer globalThis over window, self, and global.

πŸ’Ό This rule is enabled in the following configs: βœ… recommended, β˜‘οΈ unopinionated.

πŸ”§ This rule is automatically fixable by the --fix CLI option.

This rule will enforce the use of globalThis over window, self, and global.

However, there are several exceptions that remain permitted:

  1. Certain window-specific APIs, such as window.innerHeight
  2. Window-specific events, such as window.addEventListener('resize')
  3. Computed property access on window, such as window[foo]
  4. typeof existence checks, such as typeof window === 'undefined'

The complete list of permitted APIs can be found in the rule's source code.

Examples

// ❌
window;

// βœ…
globalThis;
// ❌
window.foo;

// βœ…
globalThis.foo;
// ❌
global;

// βœ…
globalThis;
// ❌
global.foo;

// βœ…
globalThis.foo;
// ❌
const {foo} = window;

// βœ…
const {foo} = globalThis;
// ❌
window.navigator;

// βœ…
globalThis.navigator;
// ❌
window.location;

// βœ…
globalThis.location;
// βœ…
window.innerWidth;

// βœ…
window.innerHeight;
// βœ…
typeof window === 'undefined';

// βœ…
typeof window === 'object';
// ❌
window.addEventListener('click', () => {});

// βœ…
globalThis.addEventListener('click', () => {});

// βœ…
window.addEventListener('resize', () => {});

// βœ…
window.addEventListener('load', () => {});

// βœ…
window.addEventListener('unload', () => {});