{"record":{"id":"95ac28c298596924","repo":"oxc-project/oxc","slug":"prefer-the-spread-operator-over-bad-metho","errorCode":null,"errorMessage":"Prefer the spread operator (`...`) over {bad_method}","messagePattern":"Prefer the spread operator \\(`\\.\\.\\.`\\) over (.+?)","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/unicorn/prefer_spread.rs","lineNumber":13,"sourceCode":"use cow_utils::CowUtils;\nuse oxc_ast::{\n    AstKind,\n    ast::{CallExpression, Expression, match_member_expression},\n};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::{GetSpan, Span};\n\nuse crate::{AstNode, ast_util, context::LintContext, rule::Rule};\n\nfn unicorn_prefer_spread_diagnostic(span: Span, bad_method: &str) -> OxcDiagnostic {\n    OxcDiagnostic::warn(format!(\"Prefer the spread operator (`...`) over {bad_method}\"))\n        .with_help(\"The spread operator (`...`) is more concise and readable.\")\n        .with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct PreferSpread;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Enforces the use of [the spread operator (`...`)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax) over outdated patterns.\n    ///\n    /// ### Why is this bad?\n    ///\n    /// Using the spread operator is more concise and readable.\n    ///\n    /// ### Examples\n    ///","sourceCodeStart":1,"sourceCodeEnd":31,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/unicorn/prefer_spread.rs#L1-L31","documentation":"This is the oxlint rule `unicorn/prefer-spread` (category `style`, `conditional_fix`). It reports uses of older array-copy/creation APIs where the spread operator `...` is equivalent and clearer: `Array.from(x)` with a single non-object argument, `array.concat(...)`, and zero/one-argument `array.slice()` used as a copy, plus `Array.of`-style patterns handled by the same matcher.","triggerScenarios":"`Array.from(set)`, `Array.from(iterable)` with exactly one argument that is not an object (the map-function/array-like forms are skipped); `someArray.concat(other)`; `someArray.slice()` or `.slice(n)` on an expression known to be an array (array literals and `this` receivers are exempted). Computed or optional member accesses are ignored.","commonSituations":"Codebases predating ES2015 spread, tutorials that copy arrays with `arr.slice(0)` or `arr.concat()`, converting Sets/Maps to arrays with `Array.from`; the fix is conditional because `Array.from` also accepts array-likes and iterables where spread is not always interchangeable in older targets.","solutions":["Replace `Array.from(x)` with `[...x]` (only when `x` is an iterable, not an array-like object).","Replace copy idioms: `arr.concat()` / `arr.slice()` → `[...arr]`; `arr.concat(other)` → `[...arr, ...other]`.","Run `oxlint --fix` and review — the fix is applied only where the rule proved it safe (conditional_fix).","If you target environments without spread support or rely on `Array.from`'s array-like handling, disable the rule or add a line-level disable comment."],"exampleFix":"// before\nconst copy = Array.from(new Set([1, 2]));\nconst merged = a.concat(b);\n\n// after\nconst copy = [...new Set([1, 2])];\nconst merged = [...a, ...b];","handlingStrategy":"validation","validationCode":"// Prefer spread idioms up front\nconst copy = [...arr];\nconst merged = [...a, ...b];\nconst fromSet = [...someSet];\n// CI: npx oxlint --deny-warn unicorn/prefer-spread src/","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Reach for `[...x]` first; keep `Array.from` only for array-likes or when you need its map function.","Remember the fix is conditional — review `oxlint --fix` output where iterables vs array-likes differ.","Spread requires ES2015 targets; if you transpile down, ensure your build handles it."],"tags":["oxlint","unicorn","style","spread","es2015"],"backgroundTag":"spread-over-array-from","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"}