oxc-project/oxc · error

'{name}' is used outside of binding context.

Error message

'{name}' is used outside of binding context.

What it means

Lint diagnostic from eslint/block-scoped-var: a variable declared with var is referenced in a scope where its binding is no longer in context (typically redeclaration analysis across scopes). var's function-level hoisting makes such uses confusing and error-prone compared to block-scoped bindings.

Source

Thrown at crates/oxc_linter/src/rules/eslint/block_scoped_var.rs:15

use crate::{AstNode, context::LintContext, rule::Rule};
use oxc_ast::{
    AstKind,
    ast::{BindingPattern, VariableDeclarationKind},
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_ecmascript::BoundNames;
use oxc_macros::declare_oxc_lint;
use oxc_semantic::Scoping;
use oxc_span::{GetSpan, Span};
use oxc_str::Ident;
use oxc_syntax::{scope::ScopeId, symbol::SymbolId};

fn redeclaration_diagnostic(decl_span: Span, redeclare_span: Span, name: &str) -> OxcDiagnostic {
    OxcDiagnostic::warn(format!("'{name}' is used outside of binding context."))
        .with_help(format!("Variable '{name}' is used outside its declaration block. Declare it outside the block or use 'let'/'const'."))
        .with_labels([
            redeclare_span.label("it is redeclared here"),
            decl_span.label(format!("'{name}' is first declared here")),
        ])
}
fn use_outside_scope_diagnostic(decl_span: Span, used_span: Span, name: &str) -> OxcDiagnostic {
    OxcDiagnostic::warn(format!("'{name}' is used outside of binding context."))
        .with_help(format!("Variable '{name}' is used outside its declaration block. Declare it outside the block or use 'let'/'const'."))
        .with_labels([
            used_span.label(format!("'{name}' is used here")),
            decl_span.label("It is declared in a different scope here"),
        ])
}

#[derive(Debug, Default, Clone)]
pub struct BlockScopedVar;

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Replace var with let or const so the binding matches the block where it is used
  2. Move the usage inside the scope in which the variable is declared
  3. Rename colliding declarations in nested scopes
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/oxc_linter/src/rules/eslint/block_scoped_var.rs:15 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/39210f4279c2941a. Report an issue: GitHub.