Back to Biomejs

noExtraBooleanCast

src/content/docs/linter/rules/no-extra-boolean-cast.mdx

latest11.8 KB
Original Source

import { Tabs, TabItem } from '@astrojs/starlight/components';

<Tabs> <TabItem label="JavaScript (and super languages)" icon="seti:javascript"> ## Summary - Rule available since: `v1.0.0` - Diagnostic Category: [`lint/complexity/noExtraBooleanCast`](/reference/diagnostics#diagnostic-category) - This rule is **recommended**, meaning it is enabled by default. - This rule has a [**safe**](/linter/#safe-fixes) fix. - The default severity of this rule is [**information**](/reference/diagnostics#information). - Sources: - Same as [`no-extra-boolean-cast`](https://eslint.org/docs/latest/rules/no-extra-boolean-cast)

How to configure

json
{
	"linter": {
		"rules": {
			"complexity": {
				"noExtraBooleanCast": "error"
			}
		}
	}
}

Description

Disallow unnecessary boolean casts

Examples

Invalid

js
if (!Boolean(foo)) {
}
<pre class="language-text"><code class="language-text">code-block.js:1:6 <a href="https://biomejs.dev/linter/rules/no-extra-boolean-cast">lint/complexity/noExtraBooleanCast</a> <span style="color: #000; background-color: #ddd;"> FIXABLE </span> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Avoid redundant &#96;Boolean&#96; call</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>if (!Boolean(foo)) &#123; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>2 │ </strong>&#125; <strong>3 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">It is not necessary to use &#96;Boolean&#96; call when a value will already be coerced to a boolean.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Safe fix</span><span style="color: lightgreen;">: </span><span style="color: lightgreen;">Remove redundant &#96;Boolean&#96; call</span> <strong> 1 │ </strong>if<span style="opacity: 0.8;">·</span>(!<span style="color: Tomato;">B</span><span style="color: Tomato;">o</span><span style="color: Tomato;">o</span><span style="color: Tomato;">l</span><span style="color: Tomato;">e</span><span style="color: Tomato;">a</span><span style="color: Tomato;">n</span><span style="color: Tomato;">(</span>foo<span style="color: Tomato;">)</span>)<span style="opacity: 0.8;">·</span>&#123; <strong> │ </strong> <span style="color: Tomato;">-</span><span style="color: Tomato;">-</span><span style="color: Tomato;">-</span><span style="color: Tomato;">-</span><span style="color: Tomato;">-</span><span style="color: Tomato;">-</span><span style="color: Tomato;">-</span><span style="color: Tomato;">-</span> <span style="color: Tomato;">-</span> </code></pre>
js
while (!!foo) {}
<pre class="language-text"><code class="language-text">code-block.js:1:8 <a href="https://biomejs.dev/linter/rules/no-extra-boolean-cast">lint/complexity/noExtraBooleanCast</a> <span style="color: #000; background-color: #ddd;"> FIXABLE </span> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Avoid redundant double-negation.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>while (!!foo) &#123;&#125; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>2 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">It is not necessary to use double-negation when a value will already be coerced to a boolean.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Safe fix</span><span style="color: lightgreen;">: </span><span style="color: lightgreen;">Remove redundant double-negation</span> <strong> 1 │ </strong>while<span style="opacity: 0.8;">·</span>(<span style="color: Tomato;">!</span><span style="color: Tomato;">!</span>foo)<span style="opacity: 0.8;">·</span>&#123;&#125; <strong> │ </strong> <span style="color: Tomato;">-</span><span style="color: Tomato;">-</span> </code></pre>
js
let x = 1;
do {
1 + 1;
} while (Boolean(x));
<pre class="language-text"><code class="language-text">code-block.js:4:10 <a href="https://biomejs.dev/linter/rules/no-extra-boolean-cast">lint/complexity/noExtraBooleanCast</a> <span style="color: #000; background-color: #ddd;"> FIXABLE </span> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Avoid redundant &#96;Boolean&#96; call</span> <strong>2 │ </strong>do &#123; <strong>3 │ </strong>1 + 1; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>4 │ </strong>&#125; while (Boolean(x)); <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>5 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">It is not necessary to use &#96;Boolean&#96; call when a value will already be coerced to a boolean.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Safe fix</span><span style="color: lightgreen;">: </span><span style="color: lightgreen;">Remove redundant &#96;Boolean&#96; call</span> <strong> 4 │ </strong>&#125;<span style="opacity: 0.8;">·</span>while<span style="opacity: 0.8;">·</span>(<span style="color: Tomato;">B</span><span style="color: Tomato;">o</span><span style="color: Tomato;">o</span><span style="color: Tomato;">l</span><span style="color: Tomato;">e</span><span style="color: Tomato;">a</span><span style="color: Tomato;">n</span><span style="color: Tomato;">(</span>x<span style="color: Tomato;">)</span>); <strong> │ </strong> <span style="color: Tomato;">-</span><span style="color: Tomato;">-</span><span style="color: Tomato;">-</span><span style="color: Tomato;">-</span><span style="color: Tomato;">-</span><span style="color: Tomato;">-</span><span style="color: Tomato;">-</span><span style="color: Tomato;">-</span> <span style="color: Tomato;">-</span> </code></pre>
js
for (; !!foo; ) {}
<pre class="language-text"><code class="language-text">code-block.js:1:8 <a href="https://biomejs.dev/linter/rules/no-extra-boolean-cast">lint/complexity/noExtraBooleanCast</a> <span style="color: #000; background-color: #ddd;"> FIXABLE </span> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Avoid redundant double-negation.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>for (; !!foo; ) &#123;&#125; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>2 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">It is not necessary to use double-negation when a value will already be coerced to a boolean.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Safe fix</span><span style="color: lightgreen;">: </span><span style="color: lightgreen;">Remove redundant double-negation</span> <strong> 1 │ </strong>for<span style="opacity: 0.8;">·</span>(;<span style="opacity: 0.8;">·</span><span style="color: Tomato;">!</span><span style="color: Tomato;">!</span>foo;<span style="opacity: 0.8;">·</span>)<span style="opacity: 0.8;">·</span>&#123;&#125; <strong> │ </strong> <span style="color: Tomato;">-</span><span style="color: Tomato;">-</span> </code></pre>
js
new Boolean(!!x);
<pre class="language-text"><code class="language-text">code-block.js:1:13 <a href="https://biomejs.dev/linter/rules/no-extra-boolean-cast">lint/complexity/noExtraBooleanCast</a> <span style="color: #000; background-color: #ddd;"> FIXABLE </span> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Avoid redundant double-negation.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>new Boolean(!!x); <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>2 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">It is not necessary to use double-negation when a value will already be coerced to a boolean.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Safe fix</span><span style="color: lightgreen;">: </span><span style="color: lightgreen;">Remove redundant double-negation</span> <strong> 1 │ </strong>new<span style="opacity: 0.8;">·</span>Boolean(<span style="color: Tomato;">!</span><span style="color: Tomato;">!</span>x); <strong> │ </strong> <span style="color: Tomato;">-</span><span style="color: Tomato;">-</span> </code></pre>

Valid

js
Boolean(!x);
!x;
!!x;
</TabItem> </Tabs>