Back to Biomejs

noEqualsToNull

src/content/docs/linter/rules/no-equals-to-null.mdx

latest5.4 KB
Original Source

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

<Tabs> <TabItem label="JavaScript (and super languages)" icon="seti:javascript"> :::caution This rule is part of the [nursery](/linter/#nursery) group. This means that it is experimental and the behavior can change at any time. ::: ## Summary - Rule available since: `v2.3.8` - Diagnostic Category: [`lint/nursery/noEqualsToNull`](/reference/diagnostics#diagnostic-category) - This rule has an [**unsafe**](/linter/#unsafe-fixes) fix. - The default severity of this rule is [**error**](/reference/diagnostics#error). - Sources: - Same as [`no-eq-null`](https://eslint.org/docs/latest/rules/no-eq-null)

How to configure

json
{
	"linter": {
		"rules": {
			"nursery": {
				"noEqualsToNull": "error"
			}
		}
	}
}

Description

Require the use of === or !== for comparison with null.

Comparing to null with == or != may have unintended results as the expression evaluates to true when comparing null to undefined.

Examples

Invalid

js
foo == null;
<pre class="language-text"><code class="language-text">code-block.js:1:5 <a href="https://biomejs.dev/linter/rules/no-equals-to-null">lint/nursery/noEqualsToNull</a> <span style="color: #000; background-color: #ddd;"> FIXABLE </span> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Tomato;">✖</span></strong> <span style="color: Tomato;"><strong>null</strong></span><span style="color: Tomato;"> comparison with </span><span style="color: Tomato;"><strong>==</strong></span><span style="color: Tomato;"> is disallowed.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>foo == null; <strong> │ </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;">This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit </span><span style="color: lightgreen;"><a href="https://biomejs.dev/linter/#nursery">https://biomejs.dev/linter/#nursery</a></span><span style="color: lightgreen;"> for more information.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Unsafe fix</span><span style="color: lightgreen;">: </span><span style="color: lightgreen;">Use </span><span style="color: lightgreen;"><strong>===</strong></span><span style="color: lightgreen;"> instead.</span> <strong> 1 │ </strong>foo<span style="opacity: 0.8;">·</span>==<span style="color: MediumSeaGreen;">=</span><span style="opacity: 0.8;">·</span>null; <strong> │ </strong> <span style="color: MediumSeaGreen;">+</span> </code></pre>
js
foo != null;
<pre class="language-text"><code class="language-text">code-block.js:1:5 <a href="https://biomejs.dev/linter/rules/no-equals-to-null">lint/nursery/noEqualsToNull</a> <span style="color: #000; background-color: #ddd;"> FIXABLE </span> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Tomato;">✖</span></strong> <span style="color: Tomato;"><strong>null</strong></span><span style="color: Tomato;"> comparison with </span><span style="color: Tomato;"><strong>!=</strong></span><span style="color: Tomato;"> is disallowed.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>foo != null; <strong> │ </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;">This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit </span><span style="color: lightgreen;"><a href="https://biomejs.dev/linter/#nursery">https://biomejs.dev/linter/#nursery</a></span><span style="color: lightgreen;"> for more information.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Unsafe fix</span><span style="color: lightgreen;">: </span><span style="color: lightgreen;">Use </span><span style="color: lightgreen;"><strong>!==</strong></span><span style="color: lightgreen;"> instead.</span> <strong> 1 │ </strong>foo<span style="opacity: 0.8;">·</span>!=<span style="color: MediumSeaGreen;">=</span><span style="opacity: 0.8;">·</span>null; <strong> │ </strong> <span style="color: MediumSeaGreen;">+</span> </code></pre>

Valid

js
foo === null;
js
foo !== null;
</TabItem> </Tabs>