{"record":{"id":"d2856b36028f7a17","repo":"oxc-project/oxc","slug":"prefer-array-flat-over-legacy-techniques-to-flat","errorCode":null,"errorMessage":"Prefer Array#flat() over legacy techniques to flatten arrays.","messagePattern":"Prefer Array#flat\\(\\) over legacy techniques to flatten arrays\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/unicorn/prefer_array_flat.rs","lineNumber":25,"sourceCode":"};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::Span;\n\nuse crate::{\n    AstNode,\n    ast_util::variable_declaration_kind,\n    ast_util::{get_symbol_id_of_variable, is_method_call},\n    context::LintContext,\n    rule::Rule,\n    utils::{\n        get_first_parameter_name, get_return_identifier_name, is_empty_array_expression,\n        is_prototype_property,\n    },\n};\n\nfn prefer_array_flat_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Prefer Array#flat() over legacy techniques to flatten arrays.\")\n        .with_help(r\"Call `.flat()` on the array instead.\")\n        .with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct PreferArrayFlat;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Prefers `Array#flat()` over legacy techniques to flatten arrays.\n    ///\n    /// ### Why is this bad?\n    ///\n    /// ES2019 introduced a new method [`Array#flat()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat) that flatten arrays.\n    ///\n    /// This rule aims to standardize the use of `Array#flat()` over legacy techniques to flatten arrays.\n    ///","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/unicorn/prefer_array_flat.rs#L7-L43","documentation":"Lint diagnostic from oxlint's `unicorn/prefer-array-flat` rule. Before ES2019, arrays were flattened with `reduce((a, b) => a.concat(b), [])`, `[].concat(...array)`, `Array.prototype.concat.apply([], array)`, or the identity `flatMap(x => x)`. The rule flags all these legacy techniques and recommends `Array#flat()`, which is clearer and handles deep/spread cases safely (no `apply` argument-limit issues).","triggerScenarios":"Exact patterns from the rule docs: `array.flatMap(x => x)`, `array.reduce((a, b) => a.concat(b), [])`, `array.reduce((a, b) => [...a, ...b], [])`, `[].concat(maybeArray)`, `[].concat(...array)`, `[].concat.apply([], array)`, `Array.prototype.concat.apply/call(...)` forms. Detected as CallExpressions during oxlint runs.","commonSituations":"Pre-ES2019 codebases or polyfill-heavy code copied from StackOverflow; `[].concat(...bigArray)` also throws `RangeError: Maximum call stack size exceeded` for large arrays, so the warning often marks a latent bug. Common when a team upgrades tooling and adopts the unicorn category.","solutions":["Replace with `array.flat()` (or `[maybeArray].flat()` for the single-argument concat form).","Run `oxlint --fix`; note the rule is `conditional_dangerous_fix`, so review the autofix diff carefully (spread-of-empty behavior can differ).","If you must support very old runtimes without polyfills, suppress inline with `// oxlint-disable-next-line unicorn/prefer-array-flat`.","Disable the rule in `.oxlintrc.json` for legacy-target builds."],"exampleFix":"// before\nconst flat = [].concat(...arrays);\nconst merged = rows.reduce((a, b) => a.concat(b), []);\n\n// after\nconst flat = arrays.flat();\nconst merged = rows.flat();","handlingStrategy":"validation","validationCode":"// oxlint --filter unicorn/prefer-array-flat src/\n// After review of the conditional fix: oxlint --fix src/legacy/","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use `array.flat()` for flattening; never `[].concat(...arr)` (call-stack limit) or `reduce(concat)`.","On legacy targets, add a `flat` polyfill instead of keeping spread-concat idioms.","Review autofix diffs — this rule is marked conditional_dangerous_fix."],"tags":["oxlint","unicorn","arrays","es2019","refactor","autofix"],"backgroundTag":"lint-rule-violation","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"}