Skip to content

noBitwiseOperators

¥Summary

¥How to configure

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

¥Description

禁止使用位运算符。

¥Disallow bitwise operators.

JavaScript 中很少使用位运算符,&| 通常只是 &&|| 的拼写错误,这会导致意外行为。

¥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

let x = y | z;
code-block.js:1:9 lint/suspicious/noBitwiseOperators ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Unexpected use of ’|‘.

> 1 │ let x = y | z;
^^^^^
2 │

Did you mean || instead? If you want to use the bitwise operator, consider suppressing this diagnostic.

x |= y;
code-block.js:1:1 lint/suspicious/noBitwiseOperators ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Unexpected use of ’|=‘.

> 1 │ x |= y;
^^^^^^
2 │

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.

¥Valid

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

¥Options

该规则提供了如下所述的选项。

¥The rule provides the options described below.

允许使用位运算符列表作为异常。

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

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

¥Related links