Ternaries are a useful syntactic construct for const
assignments. They reduce a let/if/else mess to (hopefully) a single line. They can also be misused in various ways. The current implementation of the rule always enforces the use of a ternary if both branches of an if/else expression return the same type. This is unfortunately coincidentally true for Promises and Errors. Take these two examples:
if (condition) {
await doSomething();
} else {
await doSomeOtherThing();
}
The rule corrects this to:
await (condition ? doSomething() : doSomeOtherThing());
if (condition) {
throw new ThisKindOfError();
} else {
throw new ThatKindOfError();
}
The rule corrects this to:
throw (condition ? new ThisKindOfError() : new ThatKindOfError());
Both cases I would categorize as misuse of ternaries, since they do nothing more than deduplicate a await
/throw
and make the code less readable.
However, I would still like the rule to catch valid cases, which mostly come in this form:
let someVariable;
if (condition) {
someVariable = thisValue;
} else {
someVariable = otherValue;
}
This is (correctly) corrected to:
const someVariable = condition ? thisValue : otherValue;
I propose that the rule should be configurable to only apply to assignments. This would exclude situations using await/throw/yield.
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too