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:
[lint.rules]
"no-restricted-globals" = "error" # or "warn"
Examples
[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:
| Option | Default | What it does |
|---|---|---|
globals | required | The list of names, written as above. |
checkGlobalObject | false | Also 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");