{"record":{"id":"10829a7a7f576f98","repo":"oxc-project/oxc","slug":"blocks-are-nested-too-deeply-num-maximum-allo","errorCode":null,"errorMessage":"Blocks are nested too deeply ({num}). Maximum allowed is {max}.","messagePattern":"Blocks are nested too deeply \\((.+?)\\)\\. Maximum allowed is (.+?)\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/eslint/max_depth.rs","lineNumber":15,"sourceCode":"use oxc_ast::AstKind;\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_semantic::AstNodes;\nuse oxc_span::GetSpan;\nuse oxc_span::Span;\nuse schemars::JsonSchema;\nuse serde::Deserialize;\nuse serde_json::Value;\n\nuse crate::rule::DefaultRuleConfig;\nuse crate::{AstNode, ast_util::is_function_node, context::LintContext, rule::Rule};\n\nfn max_depth_diagnostic(num: u32, max: u32, span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(format!(\"Blocks are nested too deeply ({num}). Maximum allowed is {max}.\"))\n        .with_help(\"Consider refactoring your code.\")\n        .with_label(span)\n}\n\nconst DEFAULT_MAX_DEPTH: u32 = 4;\n\n#[derive(Debug, Clone, JsonSchema, Deserialize)]\n#[serde(rename_all = \"camelCase\", default, deny_unknown_fields)]\npub struct MaxDepth {\n    /// The `max` enforces a maximum depth that blocks can be nested\n    max: u32,\n}\n\nimpl Default for MaxDepth {\n    fn default() -> Self {\n        Self { max: DEFAULT_MAX_DEPTH }\n    }\n}","sourceCodeStart":1,"sourceCodeEnd":33,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/eslint/max_depth.rs#L1-L33","documentation":"Emitted by the `max-depth` rule when nested blocks (if/else, loops, try, plain blocks — but not function boundaries) exceed the configured nesting depth; oxc's default `DEFAULT_MAX_DEPTH = 4` (crates/oxc_linter/src/rules/eslint/max_depth.rs:24). Each level of `{}` nesting inside a function body increments the depth; the diagnostic reports the actual depth and the allowed maximum on the offending block's span.","triggerScenarios":"Write five or more levels of nested control flow in one function, e.g. `if (a) { for (...) { if (b) { while (...) { if (c) { ... } } } } }` — the fifth block triggers with default max 4. Function nodes do not add depth (is_function_node resets the count), so nesting inside callbacks starts fresh.","commonSituations":"Callback-heavy legacy code with layered conditionals; deeply nested validation ladders; porting an ESLint config with a stricter `max` (2 or 3) onto existing code; new contributors adding one more guard clause to already-deep code and tripping the limit.","solutions":["Extract inner blocks into well-named helper functions or predicates (`isValidOrder(user, order)`) to flatten the nesting.","Invert conditionals and return early (guard clauses) to remove whole nesting levels.","Raise `max` in config (e.g. `{ \"max-depth\": [\"error\", 6] }`) if the domain genuinely needs deeper nesting and extraction hurts readability."],"exampleFix":"// before\nif (user) {\n  if (user.active) {\n    if (user.orders) {\n      if (user.orders.length) {\n        process(user.orders);\n      }\n    }\n  }\n}\n\n// after\nif (!user?.active?.orders?.length) return;\nprocess(user.orders);","handlingStrategy":"validation","validationCode":"// Fail fast in CI: oxlint --rule max-depth=4 src/\n// Tighten over time: 4 -> 3 once the worst offenders are refactored.","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Prefer guard clauses (early return) over wrapping another if-layer; it removes a nesting level each time.","Extract predicate helpers (`isEligible(user)`) so compound conditions collapse into one level.","Configure your editor to indent-guide/rainbow brackets so deep nesting is visible while typing, before lint catches it."],"tags":["eslint","oxlint","complexity","nesting","refactor"],"backgroundTag":"nesting-depth-lint","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}