Skip to content

Latest commit

Β 

History

History
103 lines (73 loc) Β· 1.69 KB

File metadata and controls

103 lines (73 loc) Β· 1.69 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]

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;
// ❌
window.addEventListener('click', () => {});

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

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

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

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