sindresorhus/eslint-plugin-unicorn

Rule proposal: Prevent returning/assigning mutating array methods

Closed

#1,743 opened on Mar 1, 2022

 (4 comments) (5 reactions) (0 assignees)JavaScript (468 forks)user submission
help wantednew rule

Repository metrics

Stars
 (5,022 stars)
PR merge metrics
 (Avg merge 1d 16h) (399 merged PRs in 30d)

Description

Description

Prevent returning or assigning mutating array methods, to improve readability and also protect against accidentally mutating an array in-place when you shouldn't. Array#sort(), Array#fill(), Array#reverse().

Fail

console.log(foo.sort());
const sorted = foo.sort();

Pass

foo.sort();
console.log(foo);
foo.sort();

I guess we should also allow the case where there's an intermediate array?

const sorted = foo.map(…).sort();

Contributor guide