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

Rule proposal: `no-negated-some-every`

Open fisker opened this issue 3 years ago • 7 comments

Description

I believe all negative Array.every() can write in positive Array.some()

[
    ![].every(() => true) === [].some(() => !true),
    ![0].every(() => true) === [0].some(() => !true),
    ![1].every(() => true) === [1].some(() => !true),
    ![].every(() => false) === [].some(() => !false),
    ![0].every(() => false) === [0].some(() => !false),
    ![1].every(() => false) === [1].some(() => !false),
]

Fail

if (!array.some(element => test(element)));
if (!array.every(element => test(element)));

Pass

if (array.every(element => !test(element)));
if (array.some(element => !test(element)));
if (!array.every(Boolean));

Extra

I'm not sure about this, maybe only simple cases?

if (
	  !array.some(element => {
	  	if (foo) {
			return true;
		}

	  	if (bar) {
			return false;
		}

		return baz;
	  })
);

fisker avatar Jan 25 '22 06:01 fisker

I remember we have a rule proposal to prevent unnecessary negated condition, but I forget what's in that proposal and I can't find it.

fisker avatar Jan 25 '22 06:01 fisker

I remember we have a rule proposal to prevent unnecessary negated condition, but I forget what's in that proposal and I can't find it.

https://github.com/sindresorhus/eslint-plugin-unicorn/issues/1073 ?

sindresorhus avatar Jan 25 '22 07:01 sindresorhus

Not this one, I'll search issues again.

fisker avatar Jan 25 '22 07:01 fisker

This #856. Kind of related, but this is more complicated.

fisker avatar Jan 25 '22 07:01 fisker

I believe all negative Array.every() can write in positive Array.some()

They can, but I think it might worsen readability sometimes.

papb avatar Feb 27 '22 18:02 papb

They can, but I think it might worsen readability sometimes.

Example?

sindresorhus avatar Mar 01 '22 13:03 sindresorhus

Accepted

sindresorhus avatar Jun 05 '22 09:06 sindresorhus