no-extra-boolean-cast
Disallow unnecessary boolean casts
Using the recommended
config from @eslint/js
in a configuration file
enables this rule
Some problems reported by this rule are automatically fixable by the --fix
command line option
This rule is currently frozen and is not accepting changes.
In contexts such as an if
statement’s test where the result of the expression will already be coerced to a Boolean, casting to a Boolean via double negation (!!
) or a Boolean
call is unnecessary. For example, these if
statements are equivalent:
if (!!foo) {
// ...
}
if (Boolean(foo)) {
// ...
}
if (foo) {
// ...
}
Rule Details
This rule disallows unnecessary boolean casts.
Examples of incorrect code for this rule:
/*eslint no-extra-boolean-cast: "error"*/
var foo = !;
var foo = ? baz : bat;
var foo = Boolean();
var foo = new Boolean();
if () {
// ...
}
if () {
// ...
}
while () {
// ...
}
do {
// ...
} while ();
for (; ; ) {
// ...
}
Examples of correct code for this rule:
/*eslint no-extra-boolean-cast: "error"*/
var foo = !!bar;
var foo = Boolean(bar);
function qux() {
return !!bar;
}
var foo = bar ? !!baz : !!bat;
Options
This rule has an object option:
"enforceForInnerExpressions"
when set totrue
, in addition to checking default contexts, checks whether extra boolean casts are present in expressions whose result is used in a boolean context. See examples below. Default isfalse
, meaning that this rule by default does not warn about extra booleans cast inside inner expressions.
Deprecated: The object property enforceForLogicalOperands
is deprecated (eslint#18222). Please use enforceForInnerExpressions
instead.
enforceForInnerExpressions
Examples of incorrect code for this rule with "enforceForInnerExpressions"
option set to true
:
/*eslint no-extra-boolean-cast: ["error", {"enforceForInnerExpressions": true}]*/
if ( || bar) {
//...
}
while ( && bar) {
//...
}
if (( || bar) && ) {
//...
}
var foo = new Boolean( || baz);
foo && ? baz : bat;
const ternaryBranches = Boolean(bar ? : bat);
const nullishCoalescingOperator = Boolean(bar ?? );
const commaOperator = Boolean((bar, baz, ));
// another comma operator example
for (let i = 0; console.log(i), ; i++) {
// ...
}
Examples of correct code for this rule with "enforceForInnerExpressions"
option set to true
:
/*eslint no-extra-boolean-cast: ["error", {"enforceForInnerExpressions": true}]*/
// Note that `||` and `&&` alone aren't a boolean context for either operand
// since the resultant value need not be a boolean without casting.
var foo = !!bar || baz;
if (foo || bar) {
//...
}
while (foo && bar) {
//...
}
if ((foo || bar) && baz) {
//...
}
var foo = new Boolean(bar || baz);
foo && bar ? baz : bat;
const ternaryBranches = Boolean(bar ? baz : bat);
const nullishCoalescingOperator = Boolean(bar ?? baz);
const commaOperator = Boolean((bar, baz, bat));
// another comma operator example
for (let i = 0; console.log(i), i < 10; i++) {
// ...
}
// comma operator in non-final position
Boolean((Boolean(bar), baz, bat));
Version
This rule was introduced in ESLint v0.4.0.