Back to Biomejs

noBitwiseOperators

src/content/docs/linter/rules/no-bitwise-operators.mdx

latest4.4 KB
Original Source

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

<Tabs> <TabItem label="JavaScript (and super languages)" icon="seti:javascript"> ## Summary - Rule available since: `v2.0.0` - Diagnostic Category: [`lint/suspicious/noBitwiseOperators`](/reference/diagnostics#diagnostic-category) - This rule isn't recommended, so you need to enable it. - This rule doesn't have a fix. - The default severity of this rule is [**information**](/reference/diagnostics#information). - Sources: - Same as [`no-bitwise`](https://eslint.org/docs/latest/rules/no-bitwise)

How to configure

json
{
	"linter": {
		"rules": {
			"suspicious": {
				"noBitwiseOperators": "error"
			}
		}
	}
}

Description

Disallow bitwise operators.

The use of bitwise operators in JavaScript is very rare and often & or | is simply a mistyped && or ||, which will lead to unexpected behavior.

Examples

Invalid

js
let x = y | z;
<pre class="language-text"><code class="language-text">code-block.js:1:9 <a href="https://biomejs.dev/linter/rules/no-bitwise-operators">lint/suspicious/noBitwiseOperators</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Unexpected use of '</span><span style="color: lightgreen;"><strong>|</strong></span><span style="color: lightgreen;">'.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>let x = y | z; <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;">Did you mean || instead? If you want to use the bitwise operator, consider suppressing this diagnostic.</span> </code></pre>
js
x |= y;
<pre class="language-text"><code class="language-text">code-block.js:1:1 <a href="https://biomejs.dev/linter/rules/no-bitwise-operators">lint/suspicious/noBitwiseOperators</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Unexpected use of '</span><span style="color: lightgreen;"><strong>|=</strong></span><span style="color: lightgreen;">'.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>x |= y; <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>2 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Bitwise operators are prohibited because their use can be confusing or unintended. If you did want to use the bitwise operator, consider suppressing this diagnostic.</span> </code></pre>

Valid

js
let x = y || z;
js
let x = y && z;

Options

The rule provides the options described below.

allow

Allows a list of bitwise operators to be used as exceptions.

json
{
	"linter": {
		"rules": {
			"suspicious": {
				"noBitwiseOperators": {
					"options": {
						"allow": [
							"&",
							"|",
							"^",
							"~",
							"<<",
							">>",
							">>>"
						]
					}
				}
			}
		}
	}
}

</TabItem> </Tabs>