-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Rule proposal: disallow comparing non-numeric values with >, < operators #9335
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
Comments
By "bug" do you mean to say that the API types were wrong (eg it sent a string but the types said number) or that the API types were right but the code was wrong (eg it sent a string, the types said string, and the code compared anyways). I don't think that such a rule truly makes sense in all cases. In a codebase you might want to mix-and-match depending on what type of string you're dealing with. There's not going to be a hard-and-fast rule for always or never using logical comparisons on strings. Sometimes one may make sense, and sometimes another would. Without a consistent case a rule is difficult - because it will have many known false positives that need to be ignored with a disable comment. Which is the same thing I posted in this related slash almost the same issue - #5647 (comment). This request is a subset of that one. Thats my take - so I'm a -1 on this as a rule within our project. Would like to hear what the rest of the team thinks. |
+1 on this question being important. I'm definitely somewhat sympathetic to the idea of not comparing strings with From the title, which says "non-numeric", I think of
|
As in the API was correctly returning strings (because precision is important to preserve) but the code was incorrectly comparing them alphabetically. I do not believe a rule is only justified if all possible code in all possible projects would benefit from it. A rule like this doesn't need to be in the recommended rule set. In my case I would rather that my linter catches this sort of logical error, and in the case where it is desired, just disable the rule for that line (or use localeCompare, or just make a helper that does alphabetic comparison that turns off the rule for a single line internally). And in turn, eliminate the risk of this type of bug cropping up for my team in the future. |
As for whether or not to disallow date comparisons, that is not as much of an issue for me (though the equality one is relevant) - but if it is for someone I don't see why having granularity by type be a configurable option would be a problem, potentially even differentiating equality vs greater/less than comparisons. That said maybe handling === with objects in a separate rule might make more sense, as that other issue originally requested. Personally, I am totally fine with reserving > and < comparison entirely to numeric types for my projects, since it is exceedingly rare in my company's code to ever use it for anything else. But it would be sufficient for my needs to just disallow it for strings if other types are controversial. |
The criteria for a new rule to be added now a days isn't just "would someone find this useful" and instead "would a large portion of our users find this useful". Why do we have a higher bar now? Well at this point we have well over a hundred rules. We have a massive surface area to maintain and each rule comes with a cost. Not that even if our eventual verdict is no - that doesn't mean this rule shouldn't exist at all! Just that we don't think it should live within our project. But this is all part of the discussion that we need to have to come to a decision. |
What about a |
@Josh-Cena that would be covered by the broader #5647 |
So, we have another four-way scenario here with overlapping concerns.
#5647 wants 1 and 2, but we have a lot of doubts on 1. This one wants 4 but the impact is not general enough so I'm suggesting it be merged with 2. 2 is the only case where we definitely want to report, but all other cases are in the gray area. Alternatively |
TIL about TS allowing +1 to not including |
Agreement from me on banning the cases that are always wrong, like |
@JoshuaKGoldberg if that's the only part of the proposal we're in agreement on then we should close this and instead use and refine #5647 |
SGTM. Closing now, but someone yell at me if there's anything here that we should look at differently. |
Before You File a Proposal Please Confirm You Have Done The Following...
My proposal is suitable for this project
Description
I ran into a bug where because numbers were passed from an API as strings, and we needed to compare two of those numbers, we ended up not actually comparing the numbers numerically but instead comparing them alphabetically. It would have been useful for us to have a rule banning code like:
since in reality whenever we compare strings we usually use
localeCompare()
anyway.Fail Cases
Pass Cases
The text was updated successfully, but these errors were encountered: