Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lint): add ignoreNull option to noDoubleEquals rule #3702

Conversation

peaBerberian
Copy link
Contributor

@peaBerberian peaBerberian commented Aug 23, 2024

Note: this is my first contribution/proposal to biome, and I was unsure of if I had to open an issue/discussion first for a relatively simple proposal like this one, or if writing it directly to not waste time was OK. Do not hesitate to tell me the preferred etiquette for potential future contributions, thanks.

Summary

The noDoubleEquals rule made an exception when checking against null, as it's a frequently used pattern to check against both null and undefined.

This not something that is always wanted by developers, some preferring no exception to this rule in the name of explicitness.
I also often seen == null used as a shortcut when people were unsure if some nullable value could also be undefined due to other factors not catched by a type checker (out-of-bounds array indexing or Record lookup on inexistent properties for example), in which case I do also prefer making such shortcuts less tempting to allow simpler future code updates.

The original eslint rule, eqeqeq, has an always option to disable specific handling of null. Here, I propose an ignoreNull option instead, by default set to true to not break API (and as it may be your recommended setting), because I found it much more explicit on what it does than the more vague always, though I don't really have any attachment to that naming if you prefer another one.

For implementation and documentation, I tried to look at how rules like useNamingConvention declared options and try to rely on more or less the same syntax and macros for its struct.

Test Plan

I added tests for invalid cases under the ignoreNull: false case, that I named invalidNoNull in the corresponding directory. I checked that their output seemed right.

Biome's `noDoubleEquals` rule initially made an exception when checking
against `null`.

This not something that is always wanted by linter users, some prefering
no exception to this rule in the name of explicitness.

The original `eslint` rule, `eqeqeq` has an `always` option to disable
specific handling of `null`. Here, I propose an `ignoreNull` option
instead, by default set to `true` to not break API (and as it may be
your recommended setting), because I find it much more explicit on what
it does than the more vague `always`.
@github-actions github-actions bot added A-Linter Area: linter L-JavaScript Language: JavaScript and super languages labels Aug 23, 2024
Copy link

codspeed-hq bot commented Aug 23, 2024

CodSpeed Performance Report

Merging #3702 will not alter performance

Comparing peaBerberian:feat/suspicious-no-double-equals-authorize-null-option (a63ee2d) with main (58d1cdc)

Summary

✅ 101 untouched benchmarks

Copy link
Contributor

@arendjr arendjr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great contribution, thank you!

As for the etiquette, filing an issue in advance, isn't required, but it's recommended to avoid filing PRs the maintainers may disagree with.

@arendjr
Copy link
Contributor

arendjr commented Aug 26, 2024

Thanks!

@arendjr arendjr merged commit e2ebf05 into biomejs:main Aug 26, 2024
11 checks passed
@Conaclos Conaclos added the A-Changelog Area: changelog label Aug 26, 2024
@shuhaowu
Copy link

shuhaowu commented Sep 6, 2024

Thanks for this feature. It helps a lot when trying to migrate to biome. One thing is there are also some project that standardizes on using == undefined instead of == null to check for undefined or null. Is it possible to allow that case as well?

@Conaclos
Copy link
Member

Conaclos commented Sep 7, 2024

Thanks for this feature. It helps a lot when trying to migrate to biome. One thing is there are also some project that standardizes on using == undefined instead of == null to check for undefined or null. Is it possible to allow that case as well?

Note that by default Biome already ignores == and != against null. ignoreNull was introduced for opting in ESLint always mode.
ESLint doesn't allow == and != against undefined in any mode.
However, by symmetry with null, I am not opposed to also allow == and != against undefined by default.
If we make the change, we should certainly replace ignoreNull with a more general option (maybe strict?).

We could also add a new rule that ensure using consistently ==/!= with null or undefined.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Changelog Area: changelog A-Linter Area: linter L-JavaScript Language: JavaScript and super languages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants