πΌ This rule is enabled in the following configs: β
recommended, βοΈ unopinionated.
π§π‘ This rule is automatically fixable by the --fix CLI option and manually fixable by editor suggestions.
Enforces a convention of using Math.trunc() instead of bitwise operations for clarity and more reliable results.
It prevents the use of the following bitwise operations:
x | 0(bitwise ORwith 0)~~x(twobitwise NOT)x >> 0(Signed Right Shiftwith 0)x << 0(Left Shiftwith 0)x ^ 0(bitwise XOR Shiftwith 0)
These hacks help truncate numbers but they are not clear and do not work in some cases.
This rule is fixable, unless the left-hand side in assignment has side effect.
const foo = 37.4;
// β
console.log(foo | 0);
// β
console.log(~~foo);
// β
console.log(foo << 0);
// β
console.log(foo >> 0);
// β
console.log(foo.bar ^ 0);
// β
console.log(Math.trunc(foo));let foo = 37.4;
// β
foo |= 0;
// β
foo = Math.trunc(foo);// β
const foo = 37.4;
console.log(foo | 3);// β
const foo = 37.4;
console.log(~foo);// β
const foo = 37.4;
console.log(foo >> 3);// β
const foo = 37.4;
console.log(foo << 3);// β
const foo = 37.4;
console.log(foo ^ 3);