-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Enhancement: [no-unnecessary-condition] flag when logical equality check has the same variable on either side #5076
Comments
This is actually not always strictly correct! The I would accept a PR adding such a check to the lint rule which works like this:
As for the logical expression side of things - The only place this behaviour won't work is when TS is unable to refine out the truthy/falsey type (i.e. if the value is exactly In the cases of string/number - I think it's getting to be too complicated for us to consider supporting in the lint rule. Logical expressions and understanding their AST is very complicated. For example: b || a || a It's obvious to us as humans that this is a duplicated condition, but based on the AST this is more difficult:
I.e. the outer logical expression's left side is a logical expression whose right side matches the right side of the outer logical expression.
The right of the outer logical expression is the same as the left of the logical expression on the left of logical expression the left logical expression.
the |
Hmm, seams quite complicated for not all that much benefit. And we can't even safely support member expressions, something like let i = 0
const x = { get x() { return i++ } };
console.log (x.x == x.x); // logs false |
yeah it's a really dangerous thing! |
Before You File a Proposal Please Confirm You Have Done The Following...
My proposal is suitable for this project
Link to the rule's documentation
https://typescript-eslint.io/rules/no-unnecessary-condition/
Description
If the left and right side of a condition are the same, something has gone wrong.
Fail
Pass
Additional Info
No response
The text was updated successfully, but these errors were encountered: