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
New rule proposal: prefer-ternary #12716
Comments
How often are if/else blocks reducible to a single expression such that this rule will provide value? |
Hi @jmoore914, thanks for the issue. To expand on @ljharb's point, it seems that the rule would need to detect not only that the consequent and alternate blocks have one statement, but that they have the same basic type and form. It might be good to provide some more examples with other types of statements that could be reducible in the manner you described. |
I can't comment on exactly how often this pattern is used but I know I have used it in the past and seen it in other codebases. @platinumazure yes, it would detect whether the bodies are one line and the same type (ExpressionStatement, ReturnStatement, or CallExpression) and there would be options for which type of expression this would flag (e.g. flag ReturnStatements but not CallExpressions). |
Both would be helpful :) |
Here's the repo: https://github.com/jmoore914/eslint-plugin-jmoore914/tree/prefer-ternary I can try to come up with some more examples sometime soon! |
Here's some examples that are actually from the eslint source code:
There are a ton more but does that give you an idea of what this rule would be looking at? |
Thanks for sharing these. I personally don't think this reaches the high bar we have to set for core rules now that we have so many of them. |
Unfortunately, it looks like there wasn't enough interest from the team Thanks for contributing to ESLint and we appreciate your understanding. |
Hey @jmoore914 I would be interested in this rule; did you end up publishing it somewhere? |
It hasn't quite been finished yet but take a look here: sindresorhus/eslint-plugin-unicorn#514 |
Please describe what the rule should do:
Replace an if/then/else statement with a single-line consequence and alternate with a ternary expression.
What category of rule is this? (place an "X" next to just one item)
[ ] Warns about a potential error (problem)
[X ] Suggests an alternate way of doing something (suggestion)
[ ] Enforces code style (layout)
[ ] Other (please specify:)
Provide 2-3 code examples that this rule will warn about:
Why should this rule be included in ESLint (instead of a plugin)?
Using an if/then/else statement typically results in more lines of code than a single lined ternary expression, which leads to an unnecessarily larger codebase that is more difficult to maintain.
Additionally, as in the first example, using an if/then/else statement can result in defining variables using
let
orvar
solely to be reassigned within the blocks. This leads to varaibles being unnecessarily mutable and preventsprefer-const
from flagging the variable.Are you willing to submit a pull request to implement this rule?
Yes
The text was updated successfully, but these errors were encountered: