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

Add `prefer-nullish-coalescing` rule

Open fisker opened this issue 4 years ago • 6 comments

Fixes #471

fisker avatar Jun 30 '21 02:06 fisker

After excluding many possible boolean values, there still false positive, because we can't know the || left side value type

https://github.com/fisker/eslint-plugin-unicorn/pull/296/files#diff-922f3a7977dfb855fdc9b3a7ab921bf1de948d8b27ac6e2c8286b089db47b899L255

https://github.com/fisker/eslint-plugin-unicorn/pull/296/files#diff-929e56747d8ebca03aa53d228a84d03e8d3ba3cf2c63adafaa9dc0eac82b471eR97

https://github.com/fisker/eslint-plugin-unicorn/pull/296/files#diff-1cfe9745542244677eac6e608d3b38f06600a40c3f092c1535e220114dabcfe3R137

https://github.com/fisker/eslint-plugin-unicorn/pull/296/files#diff-4f3314711d30c02b6d078f0a04c3fe5fd455eb17d68185c4a9f4b7a792d4a25eR40

https://github.com/fisker/eslint-plugin-unicorn/pull/296/files#diff-ffbf303eac24990fbfa25f4240a4e5f8c88f86e4cc15aa4d45207b323db554f3R36

https://github.com/fisker/eslint-plugin-unicorn/pull/296/files#diff-d40e13bf614e2b95c6ee4e14a2ea3d2aa286efec3da4f87dd39947a1b294ab63R29

https://github.com/fisker/eslint-plugin-unicorn/pull/296/files#diff-4057a0454b81be8653d171ac0c8bcd6b183f35d8c233dd81208827b387dd5a54R11

fisker avatar Jun 30 '21 04:06 fisker

Is this even safely possible without types?

fregante avatar Jul 13 '21 17:07 fregante

Is this even safely possible without types?

It's not safe, as you can see the changes, I was thinking maybe we can set this rule to warn by default, there is no harm without fix.

There are same concerns in the ESLint issue https://github.com/eslint/eslint/issues/14037, and I don't think they will accept it.

I've already used this rule at my work with warn, this reminds me to use ?? every time when I use ||, and it did help.

I'm also trying to reduce false positives, I've excluded many cases with boolean value on the left, see this commit.

fisker avatar Jul 14 '21 04:07 fisker

Unless the user disables warnings, warnings are still noisy enough that makes people wary of using || at all. Correct, common code should not cause warnings IMHO

fregante avatar Jul 14 '21 05:07 fregante

That's all I got in mind.

fisker avatar Jul 14 '21 05:07 fisker

I think this rule would be useful as something I would run once-in-a-while to catch cases where I forgot to use ??, but I agree, even warnings can be annoying when there are a lot of false-positives. So I think this rule should be off by default.

Alternatively, it would use type information when available: https://github.com/sindresorhus/eslint-plugin-unicorn/issues/347

sindresorhus avatar Dec 27 '21 20:12 sindresorhus

Closing for lack of activity.

sindresorhus avatar Oct 29 '23 18:10 sindresorhus