Skip to content

Commit

Permalink
Improve suggestion messages for 'prefer-single-boolean-return' (Sonar…
Browse files Browse the repository at this point in the history
  • Loading branch information
vilchik-elena authored Mar 28, 2022
1 parent ea8c431 commit 9cf80c9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 29 deletions.
33 changes: 12 additions & 21 deletions src/rules/prefer-single-boolean-return.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ 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',
suggestUnsafeIfThenElseReplacement:
'Replace with single return statement (beware of return value usages)',
suggest: 'Replace with single return statement',
suggestCast: 'Replace with single return statement using "!!" cast',
suggestBoolean:
'Replace with single return statement without cast (condition should be boolean!)',
},
schema: [],
type: 'suggestion',
Expand Down Expand Up @@ -115,27 +116,17 @@ const rule: TSESLint.RuleModule<string, string[]> = {
const shouldNegate = isReturningFalse(ifStmt.consequent);
const shouldCast = !isBooleanExpression(ifStmt.test);
const testText = context.getSourceCode().getText(ifStmt.test);
let safeCondition: string;

if (shouldNegate) {
safeCondition = `!(${testText})`;
} else if (shouldCast) {
safeCondition = `!!(${testText})`;
return [{ messageId: 'suggest', fix: getFix(`!(${testText})`) }];
} else if (!shouldCast) {
return [{ messageId: 'suggest', fix: getFix(testText) }];
} else {
safeCondition = testText;
}
const suggestions: TSESLint.ReportSuggestionArray<string> = [
{
messageId: 'suggestIfThenElseReplacement',
fix: getFix(safeCondition),
},
];
if (shouldCast && !shouldNegate) {
suggestions.push({
messageId: 'suggestUnsafeIfThenElseReplacement',
fix: getFix(testText),
});
return [
{ messageId: 'suggestCast', fix: getFix(`!!(${testText})`) },
{ messageId: 'suggestBoolean', fix: getFix(testText) },
];
}
return suggestions;
}

function isReturningFalse(stmt: TSESTree.Statement): boolean {
Expand Down
16 changes: 8 additions & 8 deletions tests/rules/prefer-single-boolean-return.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ function foo() {
messageId: 'replaceIfThenElseByReturn',
suggestions: [
{
messageId: 'suggestIfThenElseReplacement',
messageId: 'suggestCast',
output: `
function foo() {
if (bar()) {
Expand All @@ -254,7 +254,7 @@ function foo() {
}`,
},
{
messageId: 'suggestUnsafeIfThenElseReplacement',
messageId: 'suggestBoolean',
output: `
function foo() {
if (bar()) {
Expand Down Expand Up @@ -283,7 +283,7 @@ function foo() {
messageId: 'replaceIfThenElseByReturn',
suggestions: [
{
messageId: 'suggestIfThenElseReplacement',
messageId: 'suggestCast',
output: `
function foo() {
if (bar()) {
Expand All @@ -293,7 +293,7 @@ function foo() {
}`,
},
{
messageId: 'suggestUnsafeIfThenElseReplacement',
messageId: 'suggestBoolean',
output: `
function foo() {
if (bar()) {
Expand All @@ -320,7 +320,7 @@ function foo() {
messageId: 'replaceIfThenElseByReturn',
suggestions: [
{
messageId: 'suggestIfThenElseReplacement',
messageId: 'suggest',
output: `
function foo() {
return !bar();
Expand All @@ -344,7 +344,7 @@ function foo() {
messageId: 'replaceIfThenElseByReturn',
suggestions: [
{
messageId: 'suggestIfThenElseReplacement',
messageId: 'suggest',
output: `
function foo() {
return bar() > 0;
Expand All @@ -368,7 +368,7 @@ function foo() {
messageId: 'replaceIfThenElseByReturn',
suggestions: [
{
messageId: 'suggestIfThenElseReplacement',
messageId: 'suggest',
output: `
function foo() {
return !(baz() > 0);
Expand All @@ -392,7 +392,7 @@ function foo() {
messageId: 'replaceIfThenElseByReturn',
suggestions: [
{
messageId: 'suggestIfThenElseReplacement',
messageId: 'suggest',
output: `
function foo() {
return !(baz());
Expand Down

0 comments on commit 9cf80c9

Please sign in to comment.