Skip to content
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

Closed
jmoore914 opened this issue Dec 29, 2019 · 10 comments
Closed

New rule proposal: prefer-ternary #12716

jmoore914 opened this issue Dec 29, 2019 · 10 comments
Labels
archived due to age This issue has been archived; please open a new issue for any further discussion auto closed The bot closed this issue evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion feature This change adds a new feature to ESLint needs bikeshedding Minor details about this change need to be discussed rule Relates to ESLint's core rules

Comments

@jmoore914
Copy link
Contributor

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:

let a =''
if(conditional){
  a=3
}
else{
  a=4
}
if(conditional){
  return 3
}
else{
  return 4
}

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 or var solely to be reassigned within the blocks. This leads to varaibles being unnecessarily mutable and prevents prefer-const from flagging the variable.

Are you willing to submit a pull request to implement this rule?
Yes

@jmoore914 jmoore914 added feature This change adds a new feature to ESLint rule Relates to ESLint's core rules triage An ESLint team member will look at this issue soon labels Dec 29, 2019
@ljharb
Copy link
Sponsor Contributor

ljharb commented Dec 29, 2019

How often are if/else blocks reducible to a single expression such that this rule will provide value?

@platinumazure
Copy link
Member

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.

@platinumazure platinumazure added evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion needs bikeshedding Minor details about this change need to be discussed and removed triage An ESLint team member will look at this issue soon labels Dec 29, 2019
@jmoore914
Copy link
Contributor Author

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).
I've already written the rule for my own use, but I thought others might find it useful. Would you like me to link the github or would you prefer just more examples?

@kaicataldo
Copy link
Member

Would you like me to link the github or would you prefer just more examples?

Both would be helpful :)

@jmoore914
Copy link
Contributor Author

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!

@jmoore914
Copy link
Contributor Author

Here's some examples that are actually from the eslint source code:

if (Array.isArray(sourceDef)) {
     target[key] = [...sourceDef];
} else {
     target[key] = [sourceDef];
}
let messageType;

if (message.fatal || message.severity === 2) {
     messageType = chalk.red("error");
} else {
     messageType = chalk.yellow("warning");
}
if (isLexDef) {
     expected = true;
} else {
     expected = leadingComments.length > 0;
}
if (switchNode.cases.length > 0 && options.SwitchCase === 0) {
     caseIndent = switchIndent;
} else {
     caseIndent = switchIndent + (indentSize * options.SwitchCase);
}
if (iterator.valid) {
     this._rbTree = iterator.update(value);
} else {
     this._rbTree = this._rbTree.insert(key, value);
}
if (node.arguments.length) {
     openingParen = sourceCode.getFirstTokenBetween(node.callee, node.arguments[0], astUtils.isOpeningParenToken);
} else {
     openingParen = sourceCode.getLastToken(node, 1);
}
if (switchNode.cases.length > 0 && options.SwitchCase === 0) {
     caseIndent = switchIndent;
} else {
     caseIndent = switchIndent + (indentSize * options.SwitchCase);
}

There are a ton more but does that give you an idea of what this rule would be looking at?

@kaicataldo
Copy link
Member

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.

@eslint-deprecated eslint-deprecated bot added the auto closed The bot closed this issue label Feb 25, 2020
@eslint-deprecated
Copy link

Unfortunately, it looks like there wasn't enough interest from the team
or community to implement this change. While we wish we'd be able to
accommodate everyone's requests, we do need to prioritize. We've found
that issues failing to reach accepted status after 21 days tend to
never be accepted, and as such, we close those issues.
This doesn't mean the idea isn't interesting or useful, just that it's
not something the team can commit to.

Thanks for contributing to ESLint and we appreciate your understanding.

@seansfkelley
Copy link

Hey @jmoore914 I would be interested in this rule; did you end up publishing it somewhere?

@jmoore914
Copy link
Contributor Author

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

@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Aug 25, 2020
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Aug 25, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
archived due to age This issue has been archived; please open a new issue for any further discussion auto closed The bot closed this issue evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion feature This change adds a new feature to ESLint needs bikeshedding Minor details about this change need to be discussed rule Relates to ESLint's core rules
Projects
None yet
Development

No branches or pull requests

5 participants