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
Promise = loadPromiseLibrary();
window = createFakeWindow();
status = "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 */
analytics = createAnalytics();

Options#

OptionDefaultWhat it does
exceptions[]Names of read-only globals that can be assigned.
example.jswith { "exceptions": ["Promise"] }No problems
Promise = loadPromiseLibrary();