noExtraBooleanCast (since v0.9.0)
This rule is recommended by Rome.
Disallow unnecessary boolean casts
Examples
Invalid
if (!Boolean(foo)) {
} error[js/noExtraBooleanCast]: Avoid redundant `Boolean` call
┌─ js/noExtraBooleanCast.js:1:6
│
1 │ if (!Boolean(foo)) {
│ ^^^^^^^^^^^^
Suggested fix: Remove redundant `Boolean` call
| @@ -1,2 +1,2 @@
0 | - if (!Boolean(foo)) {
0 | + if (!foo) {
1 1 | }
= note: It is not necessary to use `Boolean` call when a value will already be coerced to a boolean.
while (!!foo) {} error[js/noExtraBooleanCast]: Avoid redundant double-negation.
┌─ js/noExtraBooleanCast.js:1:8
│
1 │ while (!!foo) {}
│ ^^^^^
Suggested fix: Remove redundant double-negation
| @@ -1 +1 @@
0 | - while (!!foo) {}
0 | + while (foo) {}
= note: It is not necessary to use double-negation when a value will already be coerced to a boolean.
let x = 1;
do {
1 + 1;
} while (Boolean(x)); error[js/noExtraBooleanCast]: Avoid redundant `Boolean` call
┌─ js/noExtraBooleanCast.js:4:10
│
4 │ } while (Boolean(x));
│ ^^^^^^^^^^
Suggested fix: Remove redundant `Boolean` call
| @@ -1,4 +1,4 @@
0 0 | let x = 1;
1 1 | do {
2 2 | 1 + 1;
3 | - } while (Boolean(x));
3 | + } while (x);
= note: It is not necessary to use `Boolean` call when a value will already be coerced to a boolean.
for (; !!foo; ) {} error[js/noExtraBooleanCast]: Avoid redundant double-negation.
┌─ js/noExtraBooleanCast.js:1:8
│
1 │ for (; !!foo; ) {}
│ ^^^^^
Suggested fix: Remove redundant double-negation
| @@ -1 +1 @@
0 | - for (; !!foo; ) {}
0 | + for (; foo; ) {}
= note: It is not necessary to use double-negation when a value will already be coerced to a boolean.
new Boolean(!!x); error[js/noExtraBooleanCast]: Avoid redundant double-negation.
┌─ js/noExtraBooleanCast.js:1:13
│
1 │ new Boolean(!!x);
│ ^^^
Suggested fix: Remove redundant double-negation
| @@ -1 +1 @@
0 | - new Boolean(!!x);
0 | + new Boolean(x);
= note: It is not necessary to use double-negation when a value will already be coerced to a boolean.
Valid
Boolean(!x);
!x;
!!x;