Skip to content

Latest commit

Β 

History

History
57 lines (40 loc) Β· 2.06 KB

File metadata and controls

57 lines (40 loc) Β· 2.06 KB

no-accidental-bitwise-operator

πŸ“ Disallow bitwise operators where a logical operator was likely intended.

πŸ’Ό This rule is enabled in the following configs: βœ… recommended, β˜‘οΈ unopinionated.

πŸ’‘ This rule is manually fixable by editor suggestions.

Bitwise operators (&, |, |=) are rarely used in everyday code, and when they show up in boolean-like contexts they are almost always a typo for the logical operators (&&, ||, ||=).

The bug is easy to miss and hard to debug:

  • Bitwise operators do not short-circuit, so if (object & object.property) evaluates object.property even when object is null or undefined, throwing a TypeError.
  • options | {} coerces both sides to integers, silently producing options | 0 instead of falling back to the default object.

This rule only targets the high-signal patterns where a logical operator was clearly intended, so it does not flag legitimate bitwise math like flags & MASK or value | 0. Because switching the operator changes runtime behavior, the fix is offered as a suggestion rather than applied automatically.

This is more targeted than the built-in no-bitwise rule, which disallows all bitwise operators. See also prefer-math-trunc, which covers the value | 0 truncation idiom.

Examples

// ❌
if (object & object.property) {
	// …
}

// βœ…
if (object && object.property) {
	// …
}
// ❌
options = options | {};

// βœ…
options = options || {};
// ❌
input |= '';

// βœ…
input ||= '';
// βœ… Not flagged: legitimate bitwise math
const masked = flags & MASK;
const truncated = value | 0;