unicorn/no-array-reverse Suspicious ​
What it does ​
Prefer using Array#toReversed()
over Array#reverse()
.
Why is this bad? ​
Array#reverse()
modifies the original array in place, which can lead to unintended side effects—especially when the original array is used elsewhere in the code.
Examples ​
Examples of incorrect code for this rule:
js
const reversed = [...array].reverse();
Examples of correct code for this rule:
js
const reversed = [...array].toReversed();
Options ​
allowExpressionStatement ​
{ type: boolean, default: true }
This rule allow array.reverse()
as an expression statement by default, Pass allowExpressionStatement: false to forbid Array#reverse()
even it's an expression statement.
Examples of incorrect code for this rule with the { "allowExpressionStatement": false }
option:
js
array.reverse();
How to use ​
To enable this rule in the CLI or using the config file, you can use:
bash
oxlint --deny unicorn/no-array-reverse
json
{
"rules": {
"unicorn/no-array-reverse": "error"
}
}