Skip to content

Latest commit

Β 

History

History
51 lines (36 loc) Β· 1.3 KB

no-global-object-property-assignment.md

File metadata and controls

51 lines (36 loc) Β· 1.3 KB

no-global-object-property-assignment

πŸ“ Disallow assigning properties on the global object.

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

Disallow assigning properties on the global object.

Global object mutation makes it hard to see where state is created and can accidentally overwrite existing globals. Use module scope, explicit imports and exports, dependency injection, or a local singleton instead.

If assigning a global property is intentional, disable this rule locally.

Test-environment setup (for example, assigning globalThis.window from jsdom) and polyfills or shims are the common intentional cases. Rather than disabling the rule on each line, disable it for the whole file or directory through a config override:

{
	files: ['test/**', '**/*.test.js'],
	rules: {
		'unicorn/no-global-object-property-assignment': 'off',
	},
}

Examples

// ❌
globalThis.foo = value;

// ❌
window.foo += 1;

// ❌
self.foo ||= value;
// βœ…
export const foo = value;

// βœ…
const singleton = {
	foo: value,
};

// βœ…
globalThis.foo;