oxc-project/oxc · error · OxcDiagnostic

Prefer `Reflect.apply()` over `Function#apply()`.

Error message

Prefer `Reflect.apply()` over `Function#apply()`.

What it means

Emitted by the unicorn/prefer-reflect-apply rule at a `Function.prototype.apply` call site (e.g. `fn.apply(this, args)`). It flags the member-call form because `Reflect.apply(fn, thisArg, args)` expresses the same operation more directly and is considered less verbose and clearer.

Source

Thrown at crates/oxc_linter/src/rules/unicorn/prefer_reflect_apply.rs:17

use oxc_ast::{
    AstKind,
    ast::{Argument, Expression, MemberExpression},
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, Span};

use crate::{
    AstNode,
    context::LintContext,
    fixer::{RuleFix, RuleFixer},
    rule::Rule,
};

fn prefer_reflect_apply_diagnostic(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Prefer `Reflect.apply()` over `Function#apply()`.")
        .with_help("`Reflect.apply()` is less verbose and easier to understand.")
        .with_label(span)
}

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

declare_oxc_lint!(
    /// ### What it does
    ///
    /// Disallows the use of `Function.prototype.apply()` and suggests using `Reflect.apply()` instead.
    ///
    /// ### Why is this bad?
    ///
    /// `Reflect.apply()` is arguably less verbose and easier to understand.
    /// In addition, when you accept arbitrary methods,
    /// it's not safe to assume `.apply()` exists or is not overridden.
    ///

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Rewrite `fn.apply(thisArg, args)` as `Reflect.apply(fn, thisArg, args)`
  2. The rule supplies an automatic fix to `Reflect.apply()`
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/oxc_linter/src/rules/unicorn/prefer_reflect_apply.rs:17 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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