src/content/docs/linter/rules/no-increment-decrement.mdx
import { Tabs, TabItem } from '@astrojs/starlight/components';
<Tabs> <TabItem label="JavaScript (and super languages)" icon="seti:javascript"> :::caution This rule is part of the [nursery](/linter/#nursery) group. This means that it is experimental and the behavior can change at any time. ::: ## Summary - Rule available since: `v2.3.2` - Diagnostic Category: [`lint/nursery/noIncrementDecrement`](/reference/diagnostics#diagnostic-category) - This rule doesn't have a fix. - The default severity of this rule is [**information**](/reference/diagnostics#information). - Sources: - Same as [`no-plusplus`](https://eslint.org/docs/latest/rules/no-plusplus){
"linter": {
"rules": {
"nursery": {
"noIncrementDecrement": "error"
}
}
}
}
Disallows the usage of the unary operators ++ and --.
Because the unary ++ and -- operators are subject to automatic semicolon insertion, differences in whitespace can change semantics of source code.
let i = 10;
let j = 20;
i ++
j
// i = 11, j = 20
let i = 10;
let j = 20;
i
++
j
// i = 10, j = 21
let foo = 0;
foo++;
let bar = 42;
bar--;
for (let i = 0; i < 10; i++) {
doSomething(i);
}
for (let i = 0; i < 10;) {
doSomething(i);
i++;
}
let foo = 0;
foo += 1;
let bar = 42;
bar -= 1;
for (let i = 0; i < 10; i += 1) {
doSomething(i);
}
for (let i = 0; i < 10;) {
doSomething(i);
i += 1;
}
allowForLoopAfterthoughtsAllows unary operators ++ and -- in the afterthought (final expression) of a for loop.
Default false
{
"linter": {
"rules": {
"nursery": {
"noIncrementDecrement": {
"options": {
"allowForLoopAfterthoughts": true
}
}
}
}
}
}
for (let i = 0; i < j; j = i++) {
doSomething(i, j);
}
for (let i = 10; i--;) {
doSomething(i);
}
for (let i = 0; i < 10;) i++;
for (let i = 0; i < 10; i++) {
doSomething(i);
}
for (let i = 0, j = l; i < l; i++, j--) {
doSomething(i, j);
}