noIncrementDecrement
¥Summary
-
规则生效日期:
v2.3.2¥Rule available since:
v2.3.2 -
诊断类别:
lint/nursery/noIncrementDecrement¥Diagnostic Category:
lint/nursery/noIncrementDecrement -
此规则没有修复方案。
¥This rule doesn’t have a fix.
-
此规则的默认严重级别为 information。
¥The default severity of this rule is information.
-
来源:
¥Sources:
-
与
no-plusplus相同¥Same as
no-plusplus
-
¥How to configure
{ "linter": { "rules": { "nursery": { "noIncrementDecrement": "error" } } }}¥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
¥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 -=.
¥Valid
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
allowForLoopAfterthoughts
Section titled “allowForLoopAfterthoughts”允许在 for 循环的后置表达式(最终表达式)中使用一元运算符 ++ 和 —。
¥Allows unary operators ++ and — in the afterthought (final expression) of a for loop.
默认 false
¥Default false
{ "linter": { "rules": { "nursery": { "noIncrementDecrement": { "options": { "allowForLoopAfterthoughts": true } } } } }}¥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 -=.
¥Valid
for (let i = 0; i < 10; i++) { doSomething(i);}for (let i = 0, j = l; i < l; i++, j--) { doSomething(i, j);}¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号