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
Reported:
let retries = 0;
retries++;
const next = queue[--position];
Not reported:
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:
for (let i = 0, j = items.length - 1; i < j; i++, j--) {
swap(items, i, j);
}
In the loop body they're still reported:
for (let i = 0; i < items.length; ) {
send(items[i]);
i++;
}