eslint-plugin-unicorn icon indicating copy to clipboard operation
eslint-plugin-unicorn copied to clipboard

Rule proposal: Prefer array `last` methods over `reverse`

Open fregante opened this issue 1 year ago • 5 comments

Description

It was mentioned in https://github.com/sindresorhus/eslint-plugin-unicorn/issues/1323 but it was closed in favor of prefer-array-find, which only covered filter instead.

Fail

array.reverse().find(logic);
array.reverse().findIndex(logic);
array.reverse().indexOf(logic);
array.reverse().reduce(logic);
array.toReversed().find(logic);
array.toReversed().findIndex(logic);
array.toReversed().indexOf(logic);
array.toReversed().reduce(logic);

Pass

array.findLast(logic);
array.findLastIndex(logic);
array.lastIndexOf(logic);
array.reduceRight(logic);

Proposed rule name

avoid-array-reverse

Additional Info

No response

fregante avatar Jul 31 '24 12:07 fregante

Name: prefer-array-last-methods?

sindresorhus avatar Jul 31 '24 12:07 sindresorhus

Accepted.

sindresorhus avatar Aug 21 '24 08:08 sindresorhus

array.toReversed().find(logic);
array.toReversed().findIndex(logic);
array.toReversed().indexOf(logic);
array.toReversed().reduce(logic);

fisker avatar Aug 21 '24 20:08 fisker

array.toReversed().find(logic);
array.toReversed().findIndex(logic);
array.toReversed().indexOf(logic);
array.toReversed().reduce(logic);

Before node 18.x is deprecated, the current solution is more suitable https://github.com/sindresorhus/eslint-plugin-unicorn/pull/2428#issuecomment-2298284336

axetroy avatar Aug 22 '24 02:08 axetroy

@fisker I now added them to the "fail" examples above

fregante avatar Aug 22 '24 03:08 fregante