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

Add suggestion for 'prefer-single-boolean-return' #337

Merged
merged 1 commit into from Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -43,7 +43,7 @@ Code Smells, or maintainability issues, are raised for places of code which migh
* "catch" clauses should do more than rethrow ([`no-useless-catch`])
* Local variables should not be declared and then immediately returned or thrown ([`prefer-immediate-return`]) (:wrench: *fixable*)
* Object literal syntax should be used ([`prefer-object-literal`])
* Return of boolean expressions should not be wrapped into an "if-then-else" statement ([`prefer-single-boolean-return`])
* Return of boolean expressions should not be wrapped into an "if-then-else" statement ([`prefer-single-boolean-return`]) (:wrench: *fixable*)
* A "while" loop should be used instead of a "for" loop ([`prefer-while`]) (:wrench: *fixable*)

[`cognitive-complexity`]: ./docs/rules/cognitive-complexity.md
Expand Down
2 changes: 2 additions & 0 deletions docs/rules/prefer-single-boolean-return.md
@@ -1,5 +1,7 @@
# prefer-single-boolean-return

:wrench: *fixable*

Return of boolean literal statements wrapped into `if-then-else` flow should be simplified.

## Noncompliant Code Example
Expand Down
23 changes: 23 additions & 0 deletions src/rules/prefer-single-boolean-return.ts
Expand Up @@ -31,9 +31,11 @@ const rule: TSESLint.RuleModule<string, string[]> = {
meta: {
messages: {
replaceIfThenElseByReturn: 'Replace this if-then-else flow by a single return statement.',
suggestIfThenElseReplacement: 'Replace with single return statement',
},
schema: [],
type: 'suggestion',
hasSuggestions: true,
docs: {
description:
'Return of boolean expressions should not be wrapped into an "if-then-else" statement',
Expand All @@ -53,6 +55,7 @@ const rule: TSESLint.RuleModule<string, string[]> = {
context.report({
messageId: 'replaceIfThenElseByReturn',
node,
suggest: getSuggestion(node),
});
}
},
Expand Down Expand Up @@ -91,6 +94,26 @@ const rule: TSESLint.RuleModule<string, string[]> = {
// `statement.argument` can be `null`, replace it with `undefined` in this case
return isReturnStatement(statement) && isBooleanLiteral(statement.argument || undefined);
}

function getSuggestion(ifStmt: TSESTree.IfStatement): TSESLint.ReportSuggestionArray<string> {
return [
{
messageId: 'suggestIfThenElseReplacement',
fix: fixer => {
const singleReturn = `return ${context.getSourceCode().getText(ifStmt.test)};`;
if (ifStmt.alternate) {
return fixer.replaceText(ifStmt, singleReturn);
} else {
const parent = ifStmt.parent as TSESTree.BlockStatement;
const ifStmtIndex = parent.body.findIndex(stmt => stmt === ifStmt);
const returnStmt = parent.body[ifStmtIndex + 1];
const range: [number, number] = [ifStmt.range[0], returnStmt.range[1]];
return fixer.replaceTextRange(range, singleReturn);
}
},
},
];
}
},
};

Expand Down
59 changes: 59 additions & 0 deletions tests/rules/prefer-single-boolean-return.test.ts
Expand Up @@ -227,5 +227,64 @@ ruleTester.run('prefer-single-boolean-return', rule, {
},
],
},
{
code: `
function foo() {
if (bar()) {
if (baz()) {
return true;
} else {
return false;
}
}
return qux();
}`,
errors: [
{
messageId: 'replaceIfThenElseByReturn',
suggestions: [
{
messageId: 'suggestIfThenElseReplacement',
output: `
function foo() {
if (bar()) {
return baz();
}
return qux();
}`,
},
],
},
],
},
{
code: `
function foo() {
if (bar()) {
if (baz()) {
return true;
}
return false;
}
return qux();
}`,
errors: [
{
messageId: 'replaceIfThenElseByReturn',
suggestions: [
{
messageId: 'suggestIfThenElseReplacement',
output: `
function foo() {
if (bar()) {
return baz();
}
return qux();
}`,
},
],
},
],
},
],
});