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":允许将位运算符列表用作例外。"int32Hint":允许在|0模式中使用按位或进行类型转换。
allow
使用 { "allow": ["~"] } 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the { "allow": ["~"] } option:
/*eslint no-bitwise: ["error", { "allow": ["~"] }] */
~[1,2,3].indexOf(1) === -1;
int32提示
🌐 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 中引入。