oxc-project/oxc · warning · OxcDiagnostic
Unexpected use of `with` statement.
Error message
Unexpected use of `with` statement.
What it means
Diagnostic from the `no-with` rule. The `with (obj) { ... }` statement pushes the object onto the scope chain, making property lookups ambiguous and unoptimizable; it is forbidden in strict mode and ES5+ modules. Oxc warns on any WithStatement, help 'Do not use the `with` statement.'
Source
Thrown at crates/oxc_linter/src/rules/eslint/no_with.rs:9
use oxc_ast::AstKind;
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
use crate::{AstNode, context::LintContext, rule::Rule};
fn no_with_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Unexpected use of `with` statement.")
.with_help("Do not use the `with` statement.")
.with_label(span)
}
#[derive(Debug, Default, Clone)]
pub struct NoWith;
declare_oxc_lint!(
/// ### What it does
///
/// Disallow [`with`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/with) statements.
///
/// ### Why is this bad?
///
/// The with statement is potentially problematic because it adds members
/// of an object to the current scope, making it impossible to tell what a
/// variable inside the block actually refers to.
///View on GitHub (pinned to e1e7af627c)
Solutions
- Replace `with (obj) { prop }` with direct `obj.prop` access.
- Or destructure the properties you need: `const { cos, sin } = Math;`.
- Assign the object to a short local variable and qualify accesses explicitly.
- Delete the statement if it is dead legacy code.
Example fix
// before
with (Math) {
area = PI * r * r;
}
// after
const area = Math.PI * r * r; Defensive patterns
Strategy: validation
Validate before calling
const usesWithStatement = /\bwith\s*\(/.test(source);
Prevention
- Never introduce `with` - it is a SyntaxError in strict mode/modules.
- Qualify property access or destructure the needed values instead.
- Treat any `with` found in dependencies as a signal of legacy code needing review.
When it happens
Trigger: Legacy code containing `with (Math) { x = cos(theta); }` or `with (style) { color = 'red'; }` in any parsed file. Runs on AstKind::WithStatement; modern module code would instead throw a SyntaxError in strict mode, so this mainly catches sloppy/legacy scripts.
Common situations: Old browser scripts copied from the 2000s; generated code from ancient minifiers; teaching materials predating strict mode.
Related errors
- Implied eval. Do not use execScript().
- Avoid unnecessary use of .{name}()
- Unexpected var, use let or const instead.
- Unexpected `void` operator
- Empty array binding pattern
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/da371935eabbf2cb.
Report an issue: GitHub.