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.",
}]"event", { "name": "fetch", "message": "Use the client in api.js." }2 problemsbutton.addEventListener("click", () => {
fetchUnexpected use of fetch. Use the client in api.js.("/track?id=" + eventUnexpected use of event..target.id);
});"event", { "name": "fetch", "message": "Use the client in api.js." }No problemsimport { 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:
"event"No problemsconsole.log(window.event);With { globals: ["event"], checkGlobalObject: true }, it is:
{ "globals": ["event"], "checkGlobalObject": true }1 problemconsole.log(window.eventUnexpected use of event.);With { globals: ["fetch"], checkGlobalObject: true, globalObjects: ["global"] }:
{ "globals": ["fetch"], "checkGlobalObject": true, "globalObjects": ["global"] }1 problemglobal.fetchUnexpected use of fetch.("/status");