# 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.

{/* Generated by src/codegen/lint-rule-docs.ts. Do not edit this section. */}

## Configure

Off by default. To turn it on:

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

{/* End of the generated section. */}

## Examples

```toml bunfig.toml
[lint.rules]
no-restricted-globals = ["error", "event", {
  name = "fetch",
  message = "Use the client in api.js.",
}]
```

Reported:

{/* bad {"options": ["event", {"name": "fetch", "message": "Use the client in api.js."}], "count": 2} */}
```js
button.addEventListener("click", () => {
  fetch("/track?id=" + event.target.id);
});
```

Not reported:

{/* good {"options": ["event", {"name": "fetch", "message": "Use the client in api.js."}]} */}
```js
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:

{/* good {"options": ["event"]} */}
```js
console.log(window.event);
```

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

{/* bad {"options": [{"globals": ["event"], "checkGlobalObject": true}]} */}
```js
console.log(window.event);
```

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

{/* bad {"options": [{"globals": ["fetch"], "checkGlobalObject": true, "globalObjects": ["global"]}]} */}
```js
global.fetch("/status");
```
