oxc-project/oxc · warning · OxcDiagnostic
Prefer Array#flat() over legacy techniques to flatten arrays
Error message
Prefer Array#flat() over legacy techniques to flatten arrays.
What it means
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).
Source
Thrown at crates/oxc_linter/src/rules/unicorn/prefer_array_flat.rs:25
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
use crate::{
AstNode,
ast_util::variable_declaration_kind,
ast_util::{get_symbol_id_of_variable, is_method_call},
context::LintContext,
rule::Rule,
utils::{
get_first_parameter_name, get_return_identifier_name, is_empty_array_expression,
is_prototype_property,
},
};
fn prefer_array_flat_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Prefer Array#flat() over legacy techniques to flatten arrays.")
.with_help(r"Call `.flat()` on the array instead.")
.with_label(span)
}
#[derive(Debug, Default, Clone)]
pub struct PreferArrayFlat;
declare_oxc_lint!(
/// ### What it does
///
/// Prefers `Array#flat()` over legacy techniques to flatten arrays.
///
/// ### Why is this bad?
///
/// ES2019 introduced a new method [`Array#flat()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat) that flatten arrays.
///
/// This rule aims to standardize the use of `Array#flat()` over legacy techniques to flatten arrays.
///View on GitHub (pinned to e1e7af627c)
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.
Example fix
// before const flat = [].concat(...arrays); const merged = rows.reduce((a, b) => a.concat(b), []); // after const flat = arrays.flat(); const merged = rows.flat();
Defensive patterns
Strategy: validation
Validate before calling
// oxlint --filter unicorn/prefer-array-flat src/ // After review of the conditional fix: oxlint --fix src/legacy/
Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Prefer `find` over filtering and accessing the first result.
- `Array.flatMap` performs `Array.map` and `Array.flat` in one
- Prefer `indexOf` over `findIndex` for simple equality checks
- Prefer `.some(…)` over `.find(…)` or `.findLast(…)`.
- Prefer `.some(…)` over non-zero length check from `.filter(…
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/d2856b36028f7a17.
Report an issue: GitHub.