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
Enforce use of non-strict equality (==
/!=
) when comparing to null
#6543
Comments
Thank you for this issue. As far as I know, there is no rule to enforce @eslint/eslint-team what do you think? |
Something like an "except-null" option for eqeqeq? I could get on board with that. |
This would match JSHint |
We have "allow-null" that permits but does not require |
If @mysticatea is on board, I count a champion and two (non-champion) 👍's. (Not counting myself.) |
I'll 👍 with the caveat that we need a better option name. We've tried to stay away from using the word "except" because it's unclear what it means. Especially since we have "allow-null", i don't think "except-null" is clear at all. @sstur we will happily accept a PR once we get the option name figured out. |
Some thoughts on option name:
Let's iterate... |
If you go for more than two words, then that should become a property option:
I'd strongly suggest you avoid "loose" and "strict" as descriptions since those are also modes (loose or nonstrict mode and strict mode) for executing JavaScript. |
@nzakas Good points about loose/strict. Regarding an option object key, one issue is that this option cannot be specified in conjunction with "allow-null". I could see something like this though: {
"eqeqeq": ["error", { "eqeqForNull": "allow|always|never" }]
} Where the default is "never" and the current string option "allow-null" is soft-deprecated and equivalent to "allow" in the new option. Then "always" represents the functionality requested in this issue. We can continue to iterate on the option name, but is this better? |
Having a named property with "allow" (or "either"?) /always/never" seems like the most explicit. Other options "eqeqNull" or "doubleEqualsNull" (or even "==null", though that would require quotes in yml and js configs). |
Thank you, everyone! There was By the spec,
What about the following object-form options: {
"eqeqeq": ["error", {
"eqeqNull": "never" | "always" | "allow",
"eqeqTypeof": "never" | "allow",
"eqeqBetweenLiterals": "never" | "allow"
}],
// Or shorthands:
"eqeqeq": ["error", "always" | "smart" | "allow-null"],
}
Shorthands:
|
I don't think we should use "eqeq" in option names as it's the opposite of the rule. "always" , "never", etc. Should mean "always use ===" and "never use ===". It might be easiest to deprecate allow-null in favor of an options object:
Where "ignore" is equivalent to |
👍 what @nzakas said |
👍 to @nzakas's idea. Would that just be for |
Sounds good. Indeed, opposite options were confused. |
Thanks @nzakas. That makes perfect sense. I'll work on a PR for that. @btmills: My issue is purely with null since that has a distinct behavioral difference between Comparing |
@sstur Just checking in, how are things coming along? Is there anything I can do to help? |
@btmills I'd say let's stick with "null" for now. If we get further requests for configuration, we can always add more options. I'm the meantime, we can have this new option only work with "always". |
👍 works for me |
👍 |
@sstur are you still working on this? |
Hi @nzakas and @btmills. Thanks for all the input and sorry for the delay. I am working on this now. I expect to get a PR out early this week. The plan as I understand it is:
So to summarize:
I will proceed with that plan. Thanks! |
@sstur correct. Thanks! |
==
/!=
) when comparing to null
?==
/!=
) when comparing to null
?
==
/!=
) when comparing to null
?==
/!=
) when comparing to null
Right now I use
eqeqeq
to enforce strict (===) equality everywhere, except when comparing tonull
. That is great, but I actually want to go one step further and enforce non-strict (==) equality when comparing any value tonull
literal.To put it another way, this will NOT currently throw any lint warning:
Those two statements are not easy to tell apart during code review. The issue can arise that a coder will use strict equality out of habit yet (in our codebase) they should always use loose equality when comparing to
null
in order to also catchundefined
. Similar code will pass lint and pass tests and then later it might fail in production when undefined sneaks in somehow. I'd like to statically enforce loose equality fornull
.Would you be open to a PR?
The text was updated successfully, but these errors were encountered: