no-bitwise
禁止按位运算符
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.
const x = y | z;
规则详情
¥Rule Details
此规则不允许按位运算符。
¥This rule disallows bitwise operators.
此规则的错误代码示例:
¥Examples of incorrect code for this rule:
/*eslint no-bitwise: "error"*/
let x = y | z;
const x1 = y & z;
const x2 = y ^ z;
const x3 = ~ z;
const x4 = y << z;
const x5 = y >> z;
const x6 = y >>> z;
x |= y;
x &= y;
x ^= y;
x <<= y;
x >>= y;
x >>>= y;
此规则的正确代码示例:
¥Examples of correct code for this rule:
/*eslint no-bitwise: "error"*/
let x = y || z;
const x1 = y && z;
const x2 = y > z;
const x3 = y < z;
x += y;
选项
¥Options
此规则有一个对象选项:
¥This rule has an object option:
-
"allow"
:允许将位运算符列表用作例外。¥
"allow"
: Allows a list of bitwise operators to be used as exceptions. -
"int32Hint"
:允许在|0
模式中使用按位 OR 进行类型转换。¥
"int32Hint"
: Allows the use of bitwise OR in|0
pattern for type casting.
allow
使用 { "allow": ["~"] }
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the { "allow": ["~"] }
option:
/*eslint no-bitwise: ["error", { "allow": ["~"] }] */
~[1,2,3].indexOf(1) === -1;
int32Hint
使用 { "int32Hint": true }
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the { "int32Hint": true }
option:
/*eslint no-bitwise: ["error", { "int32Hint": true }] */
const b = a|0;
版本
此规则是在 ESLint v0.0.2 中引入。