An if statement with an else branch tests a negated condition

tx0/ts:negated-if-with-elsestrictwarningstyle

What it finds

if (!ready) { a(); } else { b(); } makes the reader undo the negation to see when b runs. Swapping the branches and dropping the ! says the same plainly.

The condition must be a negation with !, or a comparison with !== or !=, and the statement must have an else branch that is not another if, so that swapping does not break up a chain.

From the rule's source, negated_if_with_else.rs.

Example from the rule's tests
function pick(ready: boolean, a: number, b: number) {  if (!ready) {tx0 reports: swap the branches    return a;  } else {    return b;  }}function chain(a: number, b: number) {  if (a !== b) {tx0 reports: swap the branches    return 1;  } else {    return 2;  }}function fine(ready: boolean, a: number) {  if (!ready) {    return a;  }  if (!ready) {    return 1;  } else if (a) {    return 2;  }  return 0;}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/ts:negated-if-with-else" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/ts:negated-if-with-else] reason="..."

tx0 explain tx0/ts:negated-if-with-else shows the same in the terminal. Found a false positive? Report it.