close

no-restricted-globals

Added in v0.7.0

Configuration

rslint.config.ts
import { defineConfig, js } from '@rslint/core';

export default defineConfig([
  js.configs.recommended,
  {
    rules: {
      'no-restricted-globals': 'error',
    },
  },
]);

Rule Details

Disallows specified global variable names. This is useful when a project wants to allow globals in general but forbid specific ones — for example, banning the deprecated global event in favor of an explicit handler parameter, or banning ambiguous DOM globals like name or length.

Examples of incorrect code for this rule with ["error", "event", "fdescribe"]:

function onClick() {
  console.log(event);
}

fdescribe("foo", function () {});

Examples of correct code for this rule with ["error", "event"]:

import event from "event-module";

const event2 = 1;

A direct restricted name does not need to be declared through languageOptions.globals: an unshadowed event reference is still reported, while a local declaration or import is not. With checkGlobalObject, the receiver must be an active global for the file. For example, select globals.browser from @rslint/core to check window.foo, or globals.worker to check self.foo; no host environment is enabled by default.

The rule also accepts an object form so a custom message can be attached to each restricted name:

{ "no-restricted-globals": ["error", { "name": "event", "message": "Use the local event parameter instead." }] }
function onClick() {
  console.log(event);
}

Options

The rule accepts either an array of names/objects, or a single object with the following properties:

  • globals — array of restricted names; each entry is either a string or { name, message? }.
  • checkGlobalObject — when true, also flags access through a global object (window.foo, self.foo, globalThis.foo, and any names configured via globalObjects). Defaults to false.
  • globalObjects — additional global object names to check when checkGlobalObject is enabled. globalThis, self, and window are always included.

Examples of incorrect code for this rule with { "globals": ["Promise"], "checkGlobalObject": true }:

{ "no-restricted-globals": ["error", { "globals": ["Promise"], "checkGlobalObject": true }] }
globalThis.Promise;
self.Promise;
window.Promise;

Examples of incorrect code for this rule with a custom globalObjects entry:

{
  "no-restricted-globals": [
    "error",
    { "globals": ["Promise"], "checkGlobalObject": true, "globalObjects": ["myGlobal"] }
  ]
}
myGlobal.Promise;

Original Documentation