Disallow Bitwise Operators (no-bitwise)
The use of bitwise operators in JavaScript is very rare and often &
or |
is simply a mistyped &&
or ||
, which will lead to unexpected behavior.
var x = y | z;
Rule Details
This rule is aimed at catching typos that end up as bitwise operators, but are meant to be the much more common && , ‘ |
’, < , > operators. As such, it will warn whenever it encounters a bitwise operator: |
The following patterns are considered problems:
/*eslint no-bitwise: 2*/
var x = y | z; /*error Unexpected use of '|'.*/
var x = y & z; /*error Unexpected use of '&'.*/
var x = y ^ z; /*error Unexpected use of '^'.*/
var x = ~ z; /*error Unexpected use of '~'.*/
var x = y << z; /*error Unexpected use of '<<'.*/
var x = y >> z; /*error Unexpected use of '>>'.*/
var x = y >>> z; /*error Unexpected use of '>>>'.*/
x |= y; /*error Unexpected use of '|='.*/
x &= y; /*error Unexpected use of '&='.*/
x ^= y; /*error Unexpected use of '^='.*/
x <<= y; /*error Unexpected use of '<<='.*/
x >>= y; /*error Unexpected use of '>>='.*/
x >>>= y; /*error Unexpected use of '>>>='.*/
The following patterns are not considered problems:
/*eslint no-bitwise: 2*/
var x = y || z;
var x = y && z;
var x = y > z;
var x = y < z;
x += y;
Options
This rule supports the following options:
allow
: The list of bitwise operators to be used as exceptions to the rule. For example:
/*eslint no-bitwise: [2, { allow: ["~"] }] */
~[1,2,3].indexOf(1) === -1;
int32Hint
: Allows the use of bitwise OR in |0
pattern for type casting:
/*eslint no-bitwise: [2, { int32Hint: true }] */
var b = a|0;
Version
This rule was introduced in ESLint 0.0.2.