Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Idea: warn about repeated expressions #3152

Closed
w0rp opened this issue Aug 23, 2017 · 3 comments
Closed

Idea: warn about repeated expressions #3152

w0rp opened this issue Aug 23, 2017 · 3 comments

Comments

@w0rp
Copy link

w0rp commented Aug 23, 2017

Here is an idea for a new rule, which requires type information and can be quite complex. TSLint could warn about redundant conditions, which could reveal typos in code. Consider the following code.

if (x > 0 && x > 0) {
}

The check for x > 0 is redundant in the above code, and you might have wanted to write y > 0 instead, but wrote x by mistake.

I'm mainly writing this down as I might try implementing this myself in the future.

@ajafff
Copy link
Contributor

ajafff commented Aug 23, 2017

Some random thoughts:

Does this rule only look for exact duplicates?

if (x >= 0 && x > 0) {} // error here too? note the >=

It could/should check following else if conditions:

if (x > 0) {
} else if (x > 0) { // error on this condition
}

... and maybe even nested if-statements:

if (x > 0) {
    if (x > 0) { // error on this condition
    }
}

... and how about control flow analysis?

if (x > 0) {
return;
} 
if (x > 0) { // error on this condition, because it is always false
}

Also note that this would be way easier to implement if typescript had type ranges microsoft/TypeScript#15480 and subtraction types microsoft/TypeScript#4183

@w0rp
Copy link
Author

w0rp commented Aug 23, 2017

The way I see it, there is just about no end to how far you could take this idea, so I'd start with looking for the same expressions for variables and attributes inside one local Boolean expression.

@JoshuaKGoldberg
Copy link
Contributor

Note: per #4534, this issue will be closed in less than a month if no PR is sent to add the rule. If you really need the rule, custom rules are always an option and can be maintained outside this repo!

@w0rp w0rp closed this as completed Jul 5, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants