close

prefer-array-last-methods

Added in v0.9.4

Configuration

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

export default defineConfig([
  unicornPlugin.configs.recommended,
  {
    rules: {
      'unicorn/prefer-array-last-methods': 'error',
    },
  },
]);

Prefer last-oriented array methods over reversing an array and then calling the corresponding forward method.

The rule recognizes:

  • .reverse().find() and .toReversed().find() → .findLast()
  • .reverse().findIndex() and .toReversed().findIndex() → .findLastIndex()
  • .reverse().indexOf() and .toReversed().indexOf() → .lastIndexOf()
  • .reverse().reduce() and .toReversed().reduce() → .reduceRight()
// Incorrect
const result = array.reverse().find(isUnicorn);

// Correct
const result = array.findLast(isUnicorn);

The rule reports a diagnostic and offers an editor suggestion rather than an automatic fix because replacing reversal can change observable mutation, sparse-array, callback-index, or index-return semantics. Suggestions are withheld when comments occur inside the matched call.

This port follows eslint-plugin-unicorn v75.0.0.