no-restricted-globals

Disallow the global variables you list

Browsers define globals with everyday names like event, name, status and length. If you forget to declare a variable or a parameter with one of those names, the code still runs and reads the global. Listing those names here turns the mistake into a report. You can also use the rule to keep a global that your project has replaced out of new code.

The rule does nothing until you give it names. Your own variable, parameter or import with the same name isn't reported.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"no-restricted-globals" = "error" # or "warn"

Examples#

bunfig.toml
[lint.rules]
no-restricted-globals = ["error", "event", {
  name = "fetch",
  message = "Use the client in api.js.",
}]
example.jswith "event", { "name": "fetch", "message": "Use the client in api.js." }2 problems
button.addEventListener("click", () => {
  fetch("/track?id=" + event.target.id);
});
example.jswith "event", { "name": "fetch", "message": "Use the client in api.js." }No problems
import { track } from "./api.js";

button.addEventListener("click", event => {
  track(event.target.id);
});

Options#

Each option is a name, or an object with a name and a message that's added to the report.

To set more than the names, pass one object instead:

OptionDefaultWhat it does
globalsrequiredThe list of names, written as above.
checkGlobalObjectfalseAlso report a listed name used as a property of globalThis, self or window.
globalObjects[]More global object names for checkGlobalObject, such as global.

With "event" alone, going through window isn't reported:

example.jswith "event"No problems
console.log(window.event);

With { globals: ["event"], checkGlobalObject: true }, it is:

example.jswith { "globals": ["event"], "checkGlobalObject": true }1 problem
console.log(window.event);

With { globals: ["fetch"], checkGlobalObject: true, globalObjects: ["global"] }:

example.jswith { "globals": ["fetch"], "checkGlobalObject": true, "globalObjects": ["global"] }1 problem
global.fetch("/status");