no-bitwise
Disallow bitwise operators
Most application code never needs &, |, ^, ~, <<, >> or >>>, so a single & or | is usually a typo for && or ||. The code still runs, but it evaluates both sides every time and gives a number instead of one of the operands.
The assignment forms such as |= and <<= are reported too.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"no-bitwise" = "error" # or "warn"Every option, set to its default:
bunfig.toml
[lint.rules]
"no-bitwise" = ["error", {
allow = [], # list of strings
int32Hint = false,
}]Examples#
example.js1 problem
if (user.active & user.verifiedUnexpected use of &.) {
grant(user);
}example.jsNo problems
if (user.active && user.verified) {
grant(user);
}Options#
bunfig.toml
[lint.rules]
no-bitwise = ["error", { allow = ["&", "|"] }]| Option | Default | What it does |
|---|---|---|
allow | [] | Operators that aren't reported. |
int32Hint | false | Allow value | 0, a way to cut a number down to a 32-bit integer. |
With allow: ["&", "|"], this isn't reported:
example.jswith
{ "allow": ["&", "|"] }No problemsconst mode = READ | WRITE;
const canWrite = (mode & WRITE) !== 0;With int32Hint, neither is this:
example.jswith
{ "int32Hint": true }No problemsconst whole = price | 0;Any other number is still reported:
example.jswith
{ "int32Hint": true }1 problemconst odd = price | 1Unexpected use of |.;When not to use it#
Code that works with flags, binary formats, hashes or colors needs these operators. Turn the rule off for those files, or list the operators you use in allow.