close

no-array-reverse

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-reverse': 'error',
    },
  },
]);

Rule Details

Prefer Array#toReversed() over the mutating Array#reverse() method. The rule provides editor suggestions, not an automatic fix, because changing whether the original array is mutated requires an explicit choice.

Examples of incorrect code:

const result = array.reverse();
const copy = [...array].reverse();

Examples of correct code:

const result = array.toReversed();
const copy = [...iterable].toReversed();

For a single spread, suggestions let you either remove the spread when its value is an array or retain it for another iterable. Optional member access is preserved; optional calls and computed method names are not checked.

Options

allowExpressionStatement

Type: boolean

Default: true

By default, a direct array.reverse() expression statement is allowed. A spread-copy mutation is still reported. Set allowExpressionStatement to false to report direct expression statements too.

{
  'unicorn/no-array-reverse': ['error', { allowExpressionStatement: false }]
}

Original Documentation