SonarSource / eslint-plugin-sonarjs

SonarJS rules for ESLint

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

prefer-single-boolean-return fails on `if` without `else`

lostfictions opened this issue · comments

commented

I want to report a bug.

prefer-single-boolean-return warns on cases like the following:

function f() {
  if (condition) {
    return true;
  } else {
    return false;
  }
}

However, this control flow is arguably unidiomatic JS regardless of whether it's returning a boolean. These cases are already warned against by eslint's built-in no-else-return rule. If we accept the fix no-else-return automatically offers for the above case, we end up with this:

function f() {
  if (condition) {
    return true;
  }
  return false;
}

This code still exhibits the issue that I would expect prefer-single-boolean-return to warn about. However, prefer-single-boolean-return does not handle this case, and no warning is emitted.

Expected behavior

prefer-single-boolean-return should warn about the following code:

function f() {
  if (condition) {
    return true;
  }
  return false;
}

A fix could look like the following:

function f() {
  return Boolean(condition);
}

eslint-plugin-sonarjs version: 0.11.0
eslint version: 8.5.0
Node.js version: 16.13.1

Rule key: prefer-single-boolean-return

I was fixed it in this PR #318, @vilchik-elena let me know what do you think :)