useValidForDirection
¥Summary
-
规则生效日期:
v1.0.0¥Rule available since:
v1.0.0 -
诊断类别:
lint/correctness/useValidForDirection¥Diagnostic Category:
lint/correctness/useValidForDirection -
此规则为推荐规则,默认启用。
¥This rule is recommended, which means is enabled by default.
-
此规则没有修复方案。
¥This rule doesn’t have a fix.
-
此规则的默认严重级别为 error。
¥The default severity of this rule is error.
-
来源:
¥Sources:
-
与
for-direction相同¥Same as
for-direction
-
¥How to configure
{ "linter": { "rules": { "correctness": { "useValidForDirection": "error" } } }}¥Description
强制 “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
Biome v2.1 中文网 - 粤ICP备13048890号