Skip to content

Commit

Permalink
Add suggestion for 'prefer-single-boolean-return' (#337)
Browse files Browse the repository at this point in the history
  • Loading branch information
yassin-kammoun-sonarsource committed Mar 24, 2022
1 parent b980329 commit d3c2f8f
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 1 deletion.
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();
}`,
},
],
},
],
},
],
});

0 comments on commit d3c2f8f

Please sign in to comment.