Skip to content

useValidForDirection

诊断类别:lint/correctness/useValidForDirection

¥Diagnostic Category: lint/correctness/useValidForDirection

自从:v1.0.0

¥Since: v1.0.0

来源:

¥Sources:

强制 “for” 循环更新子句将计数器移向正确的方向。

¥Enforce “for” loop update clause moving the counter in the right direction.

具有永远无法达到的停止条件的 for 循环(例如带有朝错误方向移动的计数器的循环)将无限运行。虽然有时需要无限循环,但惯例是将此类循环构造为 while 循环。更典型的是,无限 for 循环是一个错误。

¥A for loop with a stop condition that can never be reached, such as one with a counter that moves in the wrong direction, will run infinitely. While there are occasions when an infinite loop is intended, the convention is to construct such loops as while loops. More typically, an infinite for loop is a bug.

¥Examples

¥Invalid

for (var i = 0; i < 10; i--) {
}
code-block.js:1:5 lint/correctness/useValidForDirection ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

The update clause in this loop moves the variable in the wrong direction.

> 1 │ for (var i = 0; i < 10; i—) {
^^^^^^^^^^^^^^^^^^^^^^^^
2 │ }
3 │

for (var i = 10; i >= 0; i++) {
}
code-block.js:1:5 lint/correctness/useValidForDirection ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

The update clause in this loop moves the variable in the wrong direction.

> 1 │ for (var i = 10; i >= 0; i++) {
^^^^^^^^^^^^^^^^^^^^^^^^^
2 │ }
3 │

for (var i = 0; i > 10; i++) {
}
code-block.js:1:5 lint/correctness/useValidForDirection ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

The update clause in this loop moves the variable in the wrong direction.

> 1 │ for (var i = 0; i > 10; i++) {
^^^^^^^^^^^^^^^^^^^^^^^^
2 │ }
3 │

¥Valid

for (var i = 0; i < 10; i++) {
}

¥Related links