{"record":{"id":"be21fc566cec5481","repo":"oxc-project/oxc","slug":"magic-number-for-array-prototype-flat-depth-is-n","errorCode":null,"errorMessage":"Magic number for `Array.prototype.flat` depth is not allowed.","messagePattern":"Magic number for `Array\\.prototype\\.flat` depth is not allowed\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/unicorn/no_magic_array_flat_depth.rs","lineNumber":9,"sourceCode":"use oxc_ast::{AstKind, ast::Expression};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::{GetSpan, Span};\n\nuse crate::{AstNode, ast_util::is_method_call, context::LintContext, rule::Rule};\n\nfn no_magic_array_flat_map_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Magic number for `Array.prototype.flat` depth is not allowed.\")\n        .with_help(\"Add a comment explaining the depth.\")\n        .with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct NoMagicArrayFlatDepth;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Disallow magic numbers for [`Array.prototype.flat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat)\n    /// depth.\n    ///\n    /// ### Why is this bad?\n    ///\n    /// Magic numbers are hard to understand and maintain.\n    /// When calling `Array.prototype.flat`, it is usually called with\n    /// `1` or `Infinity`. If you are using a different number, it is","sourceCodeStart":1,"sourceCodeEnd":27,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/unicorn/no_magic_array_flat_depth.rs#L1-L27","documentation":"Diagnostic from the oxlint rule `unicorn/no-magic-array-flat-depth` (category: restriction). `flat()` is normally called with depth 1 (the default) or Infinity (fully flatten); any other bare numeric literal hides intent — is `2` the data's actual nesting depth or a guess? The rule flags a numeric-literal depth other than 1 when there is no explaining comment inside the call parentheses, and the help suggests adding that comment.","triggerScenarios":"`tree.flat(2)`, `nested.flat(20)`, `array.flat(0b10)` — exactly one numeric literal argument to `.flat()`, value not equal to 1, and no comment between the `(` and the argument. `flat()`, `flat(1)`, `flat(Infinity)`, `flat(Number.POSITIVE_INFINITY)`, `flat(depthVariable)`, and `flat(2 /* explanation */)` all pass.","commonSituations":"Flattening API responses or trees with known nesting levels; porting recursive flattening to `flat()`; teams requiring depth choices to be documented.","solutions":["Add an inline comment inside the call: `tree.flat(2 /* category -> item */)` — the rule's own escape hatch","Name the depth: `const SCHEMA_DEPTH = 2; tree.flat(SCHEMA_DEPTH)`","Use Infinity when full flattening is what you mean"],"exampleFix":"// before\nconst leaves = tree.flat(2);\n\n// after\nconst leaves = tree.flat(2 /* category -> item */);\n// or: const leaves = tree.flat(SCHEMA_DEPTH);","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["Default to `flat()` or `flat(Infinity)`; use other depths only with a comment","Extract reused depths into named constants","Treat any unexplained literal passed to flat() as a review question"],"tags":["arrays","magic-numbers","readability","oxlint"],"backgroundTag":"magic-number-lint","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"}