eslint-plugin-array-func
eslint-plugin-array-func copied to clipboard
New rule: unnecessary filter then find
// invalid:
arr
.filter(x => p(x))
.find(x => q(x))
// valid
arr
.find(x => p(x) && q(x))
// Outputs are equivalent (I think). Also potentially short-circuits faster.
The fix is not always safe. For example when p
and q
has side effects that depends on the order of execution, or when p
or q
throws errors.
Maybe also doing
arr
.filter(x=>p(x))[0]
would be something valid to be caught by this
Maybe also doing
arr .filter(x=>p(x))[0]
would be something valid to be caught by this
This case is already available with unicorn/prefer-array-find. (That plugin also offers a prefer-flat-map rule)