eslint-plugin-unicorn
eslint-plugin-unicorn copied to clipboard
Rule proposal: Prevent returning/assigning mutating array methods
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();
Should only allow ExpressionStatement
.
Let's forbid Array#push()
( covers #1487), Array#unshift()
, Array#forEach()
(unicorn/no-array-for-each
already covered this, maybe people still prefer this method, but should only use as an ExpressionStatement
), and Array#splice()
too?
Let's forbid Array#push()( covers https://github.com/sindresorhus/eslint-plugin-unicorn/issues/1487), Array#unshift(), Array#forEach()(unicorn/no-array-for-each already covered this, maybe people still prefer this method, but should only use as an ExpressionStatement)
👍
, and Array#splice() too?
Hmm, a user may need the deleted elements.
Accepted