Skip to content
NewBun 1.4.5 is out. Release notes
AphrodyBun

Linter · Rules

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.",
}]

Reported:

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

Not reported:

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:

console.log(window.event);

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

console.log(window.event);

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

global.fetch("/status");