no-global-assign
Disallow assigning to built-in and other read-only global variables
warn by default
Assigning to a global such as Promise or window replaces it for all the code that runs afterwards, including libraries. More often it's an accident. A const or let is missing, and the name you picked is already a browser global.
This rule is on by default.
Configure#
On by default as a warning. To turn it off:
bunfig.toml
[lint.rules]
"no-global-assign" = "off" # or "error"Every option, set to its default:
bunfig.toml
[lint.rules]
"no-global-assign" = ["warn", {
exceptions = [], # list of strings
}]Examples#
example.js3 problems
PromiseRead-only global Promise should not be modified. = loadPromiseLibrary();
windowRead-only global window should not be modified. = createFakeWindow();
statusRead-only global status should not be modified. = "ready";example.jsNo problems
const Promise = loadPromiseLibrary();
let status = "ready";
status = "done";A global that's declared as writable can be assigned:
example.jsNo problems
/* global analytics:writable */
analytics = createAnalytics();example.js1 problem
/* global analytics:readonly */
analyticsRead-only global analytics should not be modified. = createAnalytics();Options#
| Option | Default | What it does |
|---|---|---|
exceptions | [] | Names of read-only globals that can be assigned. |
example.jswith
{ "exceptions": ["Promise"] }No problemsPromise = loadPromiseLibrary();