no-bitwise
禁用位运算符
在 JavaScript 中使用位运算符非常罕见,通常 &
或 |
只是 &&
或 ||
的错误输入,这会导致意外的行为。
const x = y | z;
规则详情
此规则禁用位运算符。
此规则的 错误 代码示例
在 Playground 中打开
/*eslint no-bitwise: "error"*/
let x = ;
const x1 = ;
const x2 = ;
const x3 = ;
const x4 = ;
const x5 = ;
const x6 = ;
;
;
;
;
;
;
此规则的 正确 代码示例
在 Playground 中打开
/*eslint no-bitwise: "error"*/
let x = y || z;
const x1 = y && z;
const x2 = y > z;
const x3 = y < z;
x += y;
选项
此规则有一个对象选项
"allow"
: 允许使用位运算符列表作为例外。"int32Hint"
: 允许在|0
模式中使用按位或进行类型转换。
allow
使用 { "allow": ["~"] }
选项时,此规则的 正确 代码示例
在 Playground 中打开
/*eslint no-bitwise: ["error", { "allow": ["~"] }] */
~[1,2,3].indexOf(1) === -1;
int32Hint
使用 { "int32Hint": true }
选项时,此规则的 正确 代码示例
在 Playground 中打开
/*eslint no-bitwise: ["error", { "int32Hint": true }] */
const b = a|0;
版本
此规则在 ESLint v0.0.2 中引入。