Skip to content

Latest commit

Β 

History

History
85 lines (62 loc) Β· 2.27 KB

File metadata and controls

85 lines (62 loc) Β· 2.27 KB

Enforce the use of Math.trunc instead of bitwise operators

πŸ’Ό 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:

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.

Examples

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);