-
Notifications
You must be signed in to change notification settings - Fork 889
prefer-conditional-expression complains about using if - elseif - else #2899
Comments
yeah, after turning this on in another code base, I mostly agree; I think the rule should be more lenient and only enforce single if-else conditions by default while having an option to opt-in to more strict checking. you can write your code like this: this.selectedTimeInMillis = (currentSelectedTimeInMillis < this.minTimeInMillis)
? this.minTimeInMillis
: (currentSelectedTimeInMillis > this.maxTimeInMillis)
? this.maxTimeInMillis
: currentSelectedTimeInMillis; but I can see how some developers might reject that style. accepting PRs |
+1 this check is pretty annoying for if/else if/else cases |
Ternary ifs are horrible and this rule should default to false. This should never be preferred. This is anecdotal but misused ternary operators have caused more bugs than the use of any other operator i've seen. A single ternary frequently ends up being a nested ternary and nested ternary operators are the problem. Saved_LOC > code_readability // Expression is always false. |
Well, we fixed the rule to not warn on nested ternary expressions. If you want to ban all ternary expressions, you're better off creating a custom rule and disabling this one in your tslint config. |
I don't really think that this: const returnPath = error.httpMethod === "GET"
? encodeURIComponent(window.location.pathname + window.location.search)
: "/"; ... is much better than this: let returnPath;
if (error.httpMethod === "GET") {
returnPath = encodeURIComponent(window.location.pathname + window.location.search);
} else {
returnPath = "/";
} (probably a bad example because the If the rule was smart enough to determine whether the ternary would fit on one line (based on max-line-length), it might be more useful. |
IMO it should only warn/error when there's a simple if/else, same as @styu, @adidahiya and @Martin-Wegner . Anything more complicated should not trigger the warning. Can we get this rolling? |
Bug Report
TypeScript code being linted
with
tslint.json
configuration:Actual behavior
ERROR: Use a conditional expression instead of assigning to 'this.selectedTimeInMillis' in multiple places.
Expected behavior
if - else - code can be replaced by a readable conditional expression but if - elseif - ... - else code cannot be replaced by a readable conditional expression.
So the prefer-conditional-expression rule should only complain about if - else code.
The text was updated successfully, but these errors were encountered: