π 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',
},
}// β
globalThis.foo = value;
// β
window.foo += 1;
// β
self.foo ||= value;// β
export const foo = value;
// β
const singleton = {
foo: value,
};
// β
globalThis.foo;