oxc-project/oxc · warning · OxcDiagnostic

Unexpected use of '{global_name}'.

Error message

Unexpected use of '{global_name}'.

What it means

oxlint's port of ESLint `no-restricted-globals`. It fires on any free-variable reference to a global listed in the rule config, e.g. the legacy `event` object. Config entries can be plain strings or `{ name, message }` objects — a message is appended to the diagnostic text as a suffix.

Source

Thrown at crates/oxc_linter/src/rules/eslint/no_restricted_globals.rs:28

use oxc_str::CompactStr;
use rustc_hash::FxHashMap;
use schemars::{
    JsonSchema, SchemaGenerator,
    schema::{ArrayValidation, Schema, SchemaObject},
};
use serde::de::Error;
use serde_json::Value;

use crate::{AstNode, ast_util::iter_outer_expressions, context::LintContext, rule::Rule};

fn no_restricted_globals(global_name: &str, suffix: &str, span: Span) -> OxcDiagnostic {
    let warn_text = if suffix.is_empty() {
        format!("Unexpected use of '{global_name}'.")
    } else {
        format!("Unexpected use of '{global_name}'. {suffix}")
    };

    OxcDiagnostic::warn(warn_text)
        .with_help("Use a local variable or function parameter instead of the restricted global.")
        .with_label(span)
}

#[derive(Debug, Clone, Default)]
pub struct NoRestrictedGlobals(Box<NoRestrictedGlobalsConfig>);

impl Deref for NoRestrictedGlobals {
    type Target = NoRestrictedGlobalsConfig;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

#[derive(Debug, Clone)]
pub struct NoRestrictedGlobalsConfig {
    /// Objects in the format

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Get the value explicitly, e.g. accept an `event` parameter from `addEventListener` instead of the implicit global.
  2. Replace the disallowed global with the modern alternative (`Number.isFinite`, `globalThis`).
  3. Add a `{ name, message }` entry so the diagnostic explains the required replacement.
  4. Remove the name from the rule config if the restriction no longer applies.

Example fix

// before
button.addEventListener('click', () => {
  console.log(event.target);
});

// after
button.addEventListener('click', (event) => {
  console.log(event.target);
});
Defensive patterns

Strategy: validation

Validate before calling

// Detect bare references to restricted globals before lint runs
const RESTRICTED = new Set(['event', 'isFinite', 'parseFloat', 'setTimeout']);
function usesRestrictedGlobals(src) {
  return [...src.matchAll(/(?<![.\w$])([A-Za-z_$][\w$]*)/g)]
    .map(m => m[1])
    .filter(name => RESTRICTED.has(name));
}

Prevention

When it happens

Trigger: `"no-restricted-globals": ["error", "event", "isFinite"]` in .oxlintrc, then code referencing `event.target` or `isFinite(x)` as a bare identifier (the rule checks references found via `iter_outer_expressions`, not string literals or member roots).

Common situations: Banning browser-only globals (`event`, `window`) in SSR/isomorphic code; banning `isFinite`/`parseInt` in favor of `Number.isFinite`/`parseInt` with radix; teams migrating from ESLint keeping the same config.

Related errors


AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20). Data as JSON: /api/errors/a914bec2310f2ab2. Report an issue: GitHub.