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

Rule proposal: No multiple array check in logical expression

Open fisker opened this issue 1 year ago • 4 comments

Description

Use .some() or .every() check on one array repeatly can simplify to use one call.

Fail

if (
	array.some(element => element.foo === 1)
	|| array.some(element => element.bar === 2)
) {}
if (
	array.every(element => element.foo === 1)
	&& array.every(element => element.bar === 2)
) {}

Pass

if (
	array.some(element => element.foo === 1 || element.bar === 2)
) {}
if (
	array.every(element => element.foo === 1 && element.bar === 2)
) {}

Proposed rule name

Not sure about the name yet

Additional Info

No response

fisker avatar May 23 '24 07:05 fisker

Inside boolean context, I think it could als handle .find and its variants exactly the same as it would .some.

silverwind avatar May 28 '24 21:05 silverwind

Rule name: no-multiple-array-checks-in-logical-expressions?

sindresorhus avatar Jun 22 '24 10:06 sindresorhus

Inside boolean context, I think it could als handle .find and its variants exactly the same as it would .some.

Did you mean

if (
	array.find(element => element.foo === 1)
	&& array.find(element => element.bar === 2)
) {}

?

prefer-array-some should already handled?

fisker avatar Jun 22 '24 11:06 fisker

Accepted

sindresorhus avatar Jul 24 '24 22:07 sindresorhus