oxc-project/oxc · error · OxcDiagnostic
Prefer `globalThis` over environment-specific global aliases
Error message
Prefer `globalThis` over environment-specific global aliases like `window`, `self`, and `global`.
What it means
Diagnostic from the unicorn/prefer-global-this lint rule. It fires when code references an environment-specific global alias (`window`, `self`, `global`, or `globalThis` variants under unary checks) instead of the standard `globalThis`, making the code non-portable across browsers, workers, and Node. The flagged input is the identifier reference to the alias. It is a lint suggestion, not a runtime error.
Source
Thrown at crates/oxc_linter/src/rules/unicorn/prefer_global_this.rs:10
use oxc_ast::{AstKind, ast::Expression};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, Span};
use oxc_syntax::operator::UnaryOperator;
use crate::{AstNode, context::LintContext, rule::Rule};
fn prefer_global_this_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Prefer `globalThis` over environment-specific global aliases like `window`, `self`, and `global`.")
.with_help("Replace the alias with `globalThis`.")
.with_note("https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis")
.with_label(span)
}
#[derive(Debug, Default, Clone)]
pub struct PreferGlobalThis;
declare_oxc_lint!(
/// ### What it does
///
/// Enforces the use of [`globalThis`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis) instead of
/// environment‑specific global object aliases (`window`, `self`, or `global`).
///
/// Using the standard `globalThis` makes your code portable across browsers, Web Workers, Node.js,
/// and future JavaScript runtimes.
///
/// ### Why is this bad?View on GitHub (pinned to e1e7af627c)
Solutions
- Replace `window`/`self`/`global` with `globalThis`
- Use `typeof globalThis` checks instead of alias-specific environment detection
- Apply the rule's auto-fix
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at crates/oxc_linter/src/rules/unicorn/prefer_global_this.rs:10 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/29739dd7c106b19b.
Report an issue: GitHub.