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

const next = queue[--position];
example.jsNo problems
let retries = 0;
retries += 1;

position -= 1;
const next = queue[position];

Options#

OptionDefaultWhat it does
allowForLoopAfterthoughtsfalseAllow ++ and -- in the last part of a for (;;) header.

With { allowForLoopAfterthoughts: true }, this is allowed:

example.jswith { "allowForLoopAfterthoughts": true }No problems
for (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 problem
for (let i = 0; i < items.length; ) {
  send(items[i]);
  i++;
}