no-plusplus
Disallow the ++ and -- operators
i++ and ++i change the variable the same way but evaluate to different values: the old one and the new one. That's easy to get wrong inside a larger expression such as queue[--position]. i += 1 as its own statement leaves nothing to mix up.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"no-plusplus" = "error" # or "warn"Every option, set to its default:
bunfig.toml
[lint.rules]
"no-plusplus" = ["error", {
allowForLoopAfterthoughts = false,
}]Examples#
example.js2 problems
let retries = 0;
retries++Unary operator ++ used.;
const next = queue[--positionUnary operator -- used.];example.jsNo problems
let retries = 0;
retries += 1;
position -= 1;
const next = queue[position];Options#
| Option | Default | What it does |
|---|---|---|
allowForLoopAfterthoughts | false | Allow ++ and -- in the last part of a for (;;) header. |
With { allowForLoopAfterthoughts: true }, this is allowed:
example.jswith
{ "allowForLoopAfterthoughts": true }No problemsfor (let i = 0, j = items.length - 1; i < j; i++, j--) {
swap(items, i, j);
}In the loop body they're still reported:
example.jswith
{ "allowForLoopAfterthoughts": true }1 problemfor (let i = 0; i < items.length; ) {
send(items[i]);
i++Unary operator ++ used.;
}