oxc-project/oxc · warning · OxcDiagnostic

Use spread operators instead of `.apply()`.

Error message

Use spread operators instead of `.apply()`.

What it means

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.

Source

Thrown at crates/oxc_linter/src/rules/eslint/prefer_spread.rs:12

use oxc_ast::{
    AstKind,
    ast::{CallExpression, ChainElement, Expression, MemberExpression, match_member_expression},
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::{ContentEq, Span};

use crate::{AstNode, ast_util::is_method_call, context::LintContext, rule::Rule};

fn eslint_prefer_spread_diagnostic(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Use spread operators instead of `.apply()`.")
        .with_help("Replace `.apply()` with spread syntax (`...args`).")
        .with_label(span)
}

#[derive(Debug, Default, Clone)]
pub struct PreferSpread;

declare_oxc_lint!(
    /// ### What it does
    ///
    /// Require spread operators instead of `.apply()`
    ///
    /// ### Why is this bad?
    ///
    /// Before ES2015, one must use `Function.prototype.apply()` to call variadic functions.
    /// ```javascript
    /// var args = [1, 2, 3, 4];
    /// Math.max.apply(Math, args);

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Replace with spread: `foo.apply(null, args)` becomes `foo(...args)`; `Math.max.apply(null, arr)` becomes `Math.max(...arr)`.
  2. For `f.apply(this, args)` inside methods, keep the receiver: `this.method(...args)`.
  3. Run `oxlint --fix` where a safe fix is available.
  4. Disable the rule only if you must support pre-ES2015 runtimes.

Example fix

// before
const max = Math.max.apply(null, numbers);
foo.apply(null, args);

// after
const max = Math.max(...numbers);
foo(...args);
Defensive patterns

Strategy: validation

Validate before calling

# find .apply() calls ripe for spread
rg -n '\.apply\(\s*(null|undefined|this)\s*,' src/

Prevention

When it happens

Trigger: `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.

Common situations: 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.

Related errors


AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20). Data as JSON: /api/errors/948e08139ffe3912. Report an issue: GitHub.