oxc-project/oxc · error · OxcDiagnostic

Use `Array#toReversed()` instead of `Array#reverse()`.

Error message

Use `Array#toReversed()` instead of `Array#reverse()`.

What it means

Raised by unicorn/no-array-reverse when `Array#reverse()` is called on an array (with ES2023 available). Because `reverse()` mutates in place, the rule suggests the copying variant; the faulting input is the `.reverse()` call at the labeled span.

Source

Thrown at crates/oxc_linter/src/rules/unicorn/no_array_reverse.rs:19

use oxc_ast::{
    AstKind,
    ast::{ArrayExpressionElement, Expression},
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
use schemars::JsonSchema;
use serde::Deserialize;
use serde_json::Value;

use crate::{
    AstNode,
    context::LintContext,
    rule::{DefaultRuleConfig, Rule},
};

fn no_array_reverse_diagnostic(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Use `Array#toReversed()` instead of `Array#reverse()`.")
        .with_help("`Array#reverse()` mutates the original array. Use `Array#toReversed()` to return a new reversed array without modifying the original.")
        .with_label(span)
}

#[derive(Debug, Clone, JsonSchema, Deserialize)]
#[serde(rename_all = "camelCase", default, deny_unknown_fields)]
pub struct NoArrayReverse {
    /// This rule allows `array.reverse()` as an expression statement by default.
    /// Set to `false` to forbid `Array#reverse()` even if it's an expression statement.
    ///
    /// Examples of **incorrect** code for this rule with this option set to `false`:
    /// ```js
    /// array.reverse();
    /// ```
    allow_expression_statement: bool,
}

impl Default for NoArrayReverse {

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Use `array.toReversed()` which returns a new reversed array without mutating the original
  2. Spread-then-reverse (`[...array].reverse()`) when targeting older runtimes
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/oxc_linter/src/rules/unicorn/no_array_reverse.rs:19 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/61e9f5aba8237000. Report an issue: GitHub.