{"record":{"id":"da371935eabbf2cb","repo":"oxc-project/oxc","slug":"unexpected-use-of-with-statement","errorCode":null,"errorMessage":"Unexpected use of `with` statement.","messagePattern":"Unexpected use of `with` statement\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/eslint/no_with.rs","lineNumber":9,"sourceCode":"use oxc_ast::AstKind;\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::Span;\n\nuse crate::{AstNode, context::LintContext, rule::Rule};\n\nfn no_with_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Unexpected use of `with` statement.\")\n        .with_help(\"Do not use the `with` statement.\")\n        .with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct NoWith;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Disallow [`with`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/with) statements.\n    ///\n    /// ### Why is this bad?\n    ///\n    /// The with statement is potentially problematic because it adds members\n    /// of an object to the current scope, making it impossible to tell what a\n    /// variable inside the block actually refers to.\n    ///","sourceCodeStart":1,"sourceCodeEnd":27,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/eslint/no_with.rs#L1-L27","documentation":"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.'","triggerScenarios":"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.","commonSituations":"Old browser scripts copied from the 2000s; generated code from ancient minifiers; teaching materials predating strict mode.","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."],"exampleFix":"// before\nwith (Math) {\n  area = PI * r * r;\n}\n\n// after\nconst area = Math.PI * r * r;","handlingStrategy":"validation","validationCode":"const usesWithStatement = /\\bwith\\s*\\(/.test(source);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["lint","eslint","oxlint","with-statement","legacy","javascript"],"backgroundTag":"no-with-statement","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}