{"record":{"id":"948e08139ffe3912","repo":"oxc-project/oxc","slug":"use-spread-operators-instead-of-apply","errorCode":null,"errorMessage":"Use spread operators instead of `.apply()`.","messagePattern":"Use spread operators instead of `\\.apply\\(\\)`\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/eslint/prefer_spread.rs","lineNumber":12,"sourceCode":"use oxc_ast::{\n    AstKind,\n    ast::{CallExpression, ChainElement, Expression, MemberExpression, match_member_expression},\n};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::{ContentEq, Span};\n\nuse crate::{AstNode, ast_util::is_method_call, context::LintContext, rule::Rule};\n\nfn eslint_prefer_spread_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Use spread operators instead of `.apply()`.\")\n        .with_help(\"Replace `.apply()` with spread syntax (`...args`).\")\n        .with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct PreferSpread;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Require spread operators instead of `.apply()`\n    ///\n    /// ### Why is this bad?\n    ///\n    /// Before ES2015, one must use `Function.prototype.apply()` to call variadic functions.\n    /// ```javascript\n    /// var args = [1, 2, 3, 4];\n    /// Math.max.apply(Math, args);","sourceCodeStart":1,"sourceCodeEnd":30,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/eslint/prefer_spread.rs#L1-L30","documentation":"oxlint `eslint/prefer-spread`: an `fn.apply(thisArg, args)` style call was found where ES2015 spread syntax (`fn(...args)`) expresses the same intent more clearly. The diagnostic at prefer_spread.rs:12 is produced by `is_method_call` matching a member call named `apply`. Spread is preferred because it works with iterable arguments and is not limited to array-likes.","triggerScenarios":"`foo.apply(null, args)`, `foo.apply(undefined, args)`, `obj.method.apply(obj, args)`, or `Math.max.apply(null, arr)` — i.e. `.apply()` called with an array or array-like second argument.","commonSituations":"Old ES5 maximization idioms like `Math.max.apply(null, largeArray)`; copied Stack Overflow answers; code targeting environments older than ES2015 that are no longer supported.","solutions":["Replace with spread: `foo.apply(null, args)` becomes `foo(...args)`; `Math.max.apply(null, arr)` becomes `Math.max(...arr)`.","For `f.apply(this, args)` inside methods, keep the receiver: `this.method(...args)`.","Run `oxlint --fix` where a safe fix is available.","Disable the rule only if you must support pre-ES2015 runtimes."],"exampleFix":"// before\nconst max = Math.max.apply(null, numbers);\nfoo.apply(null, args);\n\n// after\nconst max = Math.max(...numbers);\nfoo(...args);","handlingStrategy":"validation","validationCode":"# find .apply() calls ripe for spread\nrg -n '\\.apply\\(\\s*(null|undefined|this)\\s*,' src/","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use `fn(...args)` / `Math.max(...arr)` as the default idiom; reserve `.apply` for when `this` binding plus an array-like must both be preserved.","During ES5→ES2015 migrations, grep for `.apply(` and convert mechanically.","Enable prefer-spread in shared config so the idiom is enforced consistently."],"tags":["eslint","oxlint","es6","spread","lint"],"backgroundTag":"prefer-spread","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"}