oxc-project/oxc · error · OxcDiagnostic

Avoid passing a function reference directly to iterator meth

Error message

Avoid passing a function reference directly to iterator methods

What it means

Raised by unicorn/no-array-callback-reference when a bare function reference (e.g. passing `JSON.stringify` or a named function) is passed directly to an array iterator method like `.map(fn)`. The rule flags calls during run(); the faulting input is the argument function reference at the labeled span. It is not an exception — it is a lint report about implicit extra-argument hazards.

Source

Thrown at crates/oxc_linter/src/rules/unicorn/no_array_callback_reference.rs:15

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

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

fn no_array_callback_reference_diagnostic(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Avoid passing a function reference directly to iterator methods")
        .with_help(
            "Wrap the function in an arrow function to explicitly pass only the element argument",
        )
        .with_label(span)
}

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

declare_oxc_lint!(
    /// ### What it does
    ///
    /// Prevents passing a function reference directly to iterator methods.
    ///
    /// ### Why is this bad?
    ///
    /// Passing functions to iterator methods can cause issues when the function is changed
    /// without realizing that the iterator passes 2 more parameters to it (index and array).

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Wrap the reference in an arrow function: `.map(x => fn(x))` so only the element is passed
  2. Use a dedicated arrow when the callback receives (element, index, array) and only the element should reach the referenced function
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/oxc_linter/src/rules/unicorn/no_array_callback_reference.rs:15 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/7789e0d4fdd472e0. Report an issue: GitHub.