useSimplifiedBooleanReturn (JavaScript)
Language JavaScript (and super languages)
Summary
Section titled “Summary”- Rule available since:
v2.6.0 - Diagnostic Category:
lint/nursery/useSimplifiedBooleanReturn - This rule has a safe fix.
- The default severity of this rule is information.
- Sources:
- Same as
unicorn/prefer-boolean-return
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "useSimplifiedBooleanReturn": "error" } } }}Description
Section titled “Description”Return conditions directly instead of using if statements that only return true or false.
An if statement whose branches only return true and false is a long way of
returning the condition itself. The same applies to an if statement that returns
true or false, followed directly by a return of the opposite value.
Returning the condition directly is shorter and easier to read.
When the condition doesn’t always produce a boolean, the fix converts it with !!
so the function still returns true or false.
A sequence of early returns that all return the same value is not reported, because rewriting only the last one would break up the group:
function isEmpty(value) { if (value === null) { return true; } if (value === "") { return true; } return false;}Examples
Section titled “Examples”Invalid
Section titled “Invalid”function isPositive(value) { if (value > 0) { return true; } return false;}code-block.js:2:5 lint/nursery/useSimplifiedBooleanReturn FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This if statement only returns true or false.
1 │ function isPositive(value) {
> 2 │ if (value > 0) {
│ ^^^^^^^^^^^^^^^^
> 3 │ return true;
> 4 │ }
> 5 │ return false;
│ ^^^^^^^^^^^^^
6 │ }
7 │
ℹ Returning the condition directly is shorter and easier to read.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Safe fix: Return the condition directly.
1 1 │ function isPositive(value) {
2 │ - ····if·(value·>·0)·{
3 │ - ········return·true;
4 │ - ····}
5 │ - ····return·false;
2 │ + ····return·value·>·0;
6 3 │ }
7 4 │
function hasItems(items) { if (items.length) { return true; } else { return false; }}code-block.js:2:5 lint/nursery/useSimplifiedBooleanReturn FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This if statement only returns true or false.
1 │ function hasItems(items) {
> 2 │ if (items.length) {
│ ^^^^^^^^^^^^^^^^^^^
> 3 │ return true;
> 4 │ } else {
> 5 │ return false;
> 6 │ }
│ ^
7 │ }
8 │
ℹ Returning the condition directly is shorter and easier to read.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Safe fix: Return the condition directly.
1 1 │ function hasItems(items) {
2 │ - ····if·(items.length)·{
3 │ - ········return·true;
4 │ - ····}·else·{
5 │ - ········return·false;
6 │ - ····}
2 │ + ····return·!!items.length;
7 3 │ }
8 4 │
function isNotPositive(value) { if (value > 0) { return false; } return true;}code-block.js:2:5 lint/nursery/useSimplifiedBooleanReturn FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This if statement only returns true or false.
1 │ function isNotPositive(value) {
> 2 │ if (value > 0) {
│ ^^^^^^^^^^^^^^^^
> 3 │ return false;
> 4 │ }
> 5 │ return true;
│ ^^^^^^^^^^^^
6 │ }
7 │
ℹ Returning the condition directly is shorter and easier to read.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Safe fix: Return the condition directly.
1 1 │ function isNotPositive(value) {
2 │ - ····if·(value·>·0)·{
3 │ - ········return·false;
4 │ - ····}
5 │ - ····return·true;
2 │ + ····return·!(value·>·0);
6 3 │ }
7 4 │
function isPositive(value) { return value > 0;}
function hasItems(items) { return !!items.length;}
function check(value) { if (value > 0) { return true; } log(value); return false;}Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.