{"record":{"id":"396cd6fd05663046","repo":"oxc-project/oxc","slug":"array-flatmap-performs-array-map-and-array-fl","errorCode":null,"errorMessage":"`Array.flatMap` performs `Array.map` and `Array.flat` in one step.","messagePattern":"`Array\\.flatMap` performs `Array\\.map` and `Array\\.flat` in one step\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/unicorn/prefer_array_flat_map.rs","lineNumber":12,"sourceCode":"use oxc_ast::{\n    AstKind,\n    ast::{Argument, CallExpression, Expression},\n};\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, fixer::Fix, rule::Rule};\n\nfn prefer_array_flat_map_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"`Array.flatMap` performs `Array.map` and `Array.flat` in one step.\")\n        .with_help(\"Prefer `.flatMap(…)` over `.map(…).flat()`.\")\n        .with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct PreferArrayFlatMap;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Prefers the use of `.flatMap()` when `map().flat()` are used together.\n    ///\n    /// ### Why is this bad?\n    ///\n    /// It is slightly more efficient to use `.flatMap(…)` instead of `.map(…).flat()`.\n    ///\n    /// ### Examples\n    ///","sourceCodeStart":1,"sourceCodeEnd":30,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/unicorn/prefer_array_flat_map.rs#L1-L30","documentation":"Lint diagnostic from oxlint's `unicorn/prefer-array-flat-map` rule. Chaining `.map(fn).flat()` builds a whole mapped array and then a second flattened array; `.flatMap(fn)` does both in one pass and one allocation. The rule fires on `map(...).flat()` chains (and `flat(1)` depth) and offers a fixer to merge them.","triggerScenarios":"Member-call chain where the result of `.map(predicate)` is immediately `.flat()`-ed: `words.map(w => w.split('')).flat()`, `lists.map(expand).flat(1)`. Detected while oxlint walks CallExpressions with `is_method_call` on `map` followed by `flat`.","commonSituations":"Tokenizers (map-to-split then flatten), normalize-and-expand pipelines. Very common in data-munging code. Hits in bulk when adopting the unicorn ruleset; autofix is generally safe but review when the map callback returns arrays-with-arrays (flat() default depth 1).","solutions":["Merge the chain: `arr.map(fn).flat()` -> `arr.flatMap(fn)`.","Run `oxlint --fix` for an automatic rewrite of every match.","Keep `.map().flat(2)` as-is (flatMap is depth-1 only) and suppress with an inline disable comment if flagged erroneously.","Disable the rule via `.oxlintrc.json` if depth-2+ flattening after map is a common pattern in the codebase."],"exampleFix":"// before\nconst letters = words.map(w => w.split('')).flat();\n\n// after\nconst letters = words.flatMap(w => w.split(''));","handlingStrategy":"validation","validationCode":"// oxlint --fix --filter unicorn/prefer-array-flat-map src/\n// CI gate: oxlint --deny-warnings src/","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Prefer `flatMap` whenever a map callback returns arrays.","Remember flatMap flattens one level only; for deeper flattening keep `.map().flat(n)` with a suppression note."],"tags":["oxlint","unicorn","arrays","performance","es2019","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"}