noIncrementDecrement
Este conteúdo não está disponível em sua língua ainda.
Summary
Section titled “Summary”- Rule available since:
v2.3.2 - Diagnostic Category:
lint/nursery/noIncrementDecrement - This rule doesn’t have a fix.
- The default severity of this rule is information.
- Sources:
- Same as
no-plusplus
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noIncrementDecrement": "error" } } }}Description
Section titled “Description”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 = 20code-block.js:4:1 lint/nursery/noIncrementDecrement ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Unexpected use of increment/decrement unary operator.
2 │ let j = 20;
3 │
> 4 │ i ++
│ ^^^^
5 │ j
6 │ // i = 11, j = 20
ℹ The unary ++ and — operators are subject to automatic semicolon insertion, differences in whitespace can change semantics of source code. Instead use += or -=.
let i = 10;let j = 20;
i++j// i = 10, j = 21code-block.js:5:1 lint/nursery/noIncrementDecrement ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Unexpected use of increment/decrement unary operator.
4 │ i
> 5 │ ++
│ ^^
> 6 │ j
│ ^
7 │ // i = 10, j = 21
8 │
ℹ The unary ++ and — operators are subject to automatic semicolon insertion, differences in whitespace can change semantics of source code. Instead use += or -=.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”let foo = 0;foo++;code-block.js:2:1 lint/nursery/noIncrementDecrement ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Unexpected use of increment/decrement unary operator.
1 │ let foo = 0;
> 2 │ foo++;
│ ^^^^^
3 │
ℹ The unary ++ and — operators are subject to automatic semicolon insertion, differences in whitespace can change semantics of source code. Instead use += or -=.
let bar = 42;bar--;code-block.js:2:1 lint/nursery/noIncrementDecrement ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Unexpected use of increment/decrement unary operator.
1 │ let bar = 42;
> 2 │ bar—;
│ ^^^^^
3 │
ℹ The unary ++ and — operators are subject to automatic semicolon insertion, differences in whitespace can change semantics of source code. Instead use += or -=.
for (let i = 0; i < 10; i++) { doSomething(i);}code-block.js:1:25 lint/nursery/noIncrementDecrement ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Unexpected use of increment/decrement unary operator.
> 1 │ for (let i = 0; i < 10; i++) {
│ ^^^
2 │ doSomething(i);
3 │ }
ℹ The unary ++ and — operators are subject to automatic semicolon insertion, differences in whitespace can change semantics of source code. Instead use += or -=.
for (let i = 0; i < 10;) { doSomething(i); i++;}code-block.js:3:5 lint/nursery/noIncrementDecrement ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Unexpected use of increment/decrement unary operator.
1 │ for (let i = 0; i < 10;) {
2 │ doSomething(i);
> 3 │ i++;
│ ^^^
4 │ }
5 │
ℹ The unary ++ and — operators are subject to automatic semicolon insertion, differences in whitespace can change semantics of source code. Instead use += or -=.
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;}Options
Section titled “Options”allowForLoopAfterthoughts
Section titled “allowForLoopAfterthoughts”Allows unary operators ++ and — in the afterthought (final expression) of a for loop.
Default false
{ "linter": { "rules": { "nursery": { "noIncrementDecrement": { "options": { "allowForLoopAfterthoughts": true } } } } }}Invalid
Section titled “Invalid”for (let i = 0; i < j; j = i++) { doSomething(i, j);}code-block.js:1:28 lint/nursery/noIncrementDecrement ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Unexpected use of increment/decrement unary operator.
> 1 │ for (let i = 0; i < j; j = i++) {
│ ^^^
2 │ doSomething(i, j);
3 │ }
ℹ The unary ++ and — operators are subject to automatic semicolon insertion, differences in whitespace can change semantics of source code. Instead use += or -=.
for (let i = 10; i--;) { doSomething(i);}code-block.js:1:18 lint/nursery/noIncrementDecrement ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Unexpected use of increment/decrement unary operator.
> 1 │ for (let i = 10; i—;) {
│ ^^^
2 │ doSomething(i);
3 │ }
ℹ The unary ++ and — operators are subject to automatic semicolon insertion, differences in whitespace can change semantics of source code. Instead use += or -=.
for (let i = 0; i < 10;) i++;code-block.js:1:26 lint/nursery/noIncrementDecrement ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Unexpected use of increment/decrement unary operator.
> 1 │ for (let i = 0; i < 10;) i++;
│ ^^^
2 │
ℹ The unary ++ and — operators are subject to automatic semicolon insertion, differences in whitespace can change semantics of source code. Instead use += or -=.
for (let i = 0; i < 10; i++) { doSomething(i);}for (let i = 0, j = l; i < l; i++, j--) { doSomething(i, j);}Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.