close

no-array-callback-reference

Unreleased

Configuration

PresetConfigured Value
✅ unicornPlugin.configs.recommended"error"
rslint.config.ts
import { defineConfig, unicornPlugin } from '@rslint/core';

export default defineConfig([
  unicornPlugin.configs.recommended,
  {
    rules: {
      'unicorn/no-array-callback-reference': 'error',
    },
  },
]);

Rule Details

Prevent passing function references directly to array iterator methods. An iterator passes extra arguments, such as the index and array, which can unexpectedly change a callback's behavior.

This rule checks every, filter, find, findLast, findIndex, findLastIndex, flatMap, forEach, map, reduce, reduceRight, and some. It offers editor suggestions to wrap the callback and choose the arguments to forward. There is no automatic fix.

Examples of incorrect code:

array.map(callback);
array.forEach(callback);
array.reduce(reducer, initialValue);

Examples of correct code:

array.map(element => callback(element));
array.forEach(element => {
  callback(element);
});
array.reduce((accumulator, element) => reducer(accumulator, element), initialValue);

Primitive conversions such as array.map(Number) and boolean predicates such as array.filter(Boolean) are allowed. TypeScript type predicates declared directly on a local callback and imported callbacks are allowed for every, filter, find, and findLast, so those methods can continue to narrow the result type.

Arrays, typed arrays, and unknown receivers are checked. Known non-array collections are ignored. Non-reducing iterator calls directly under await are also ignored.

Options

ignore

Type: string[]

Default: []

Additional receiver names or dotted paths to ignore. The same names are recognized when the receiver is a call, such as library().map(callback).

{
  'unicorn/no-array-callback-reference': ['error', {
    ignore: ['Angular', 'library', 'myLib.utils'],
  }],
}

Promise, React.Children, Children, lodash, underscore, _, Async, async, this, $, and jQuery are always ignored. Vue.filter() and types.map() are also excluded.

Differences from upstream

Suggested parameter names can differ for some irregular or capitalized array names. For example, for People.map(callback), rslint suggests (element) => callback(element), while upstream suggests (Person) => callback(Person). The diagnostic and the arguments forwarded to the callback are the same.

Original Documentation