no-unmodified-loop-condition

禁止未修改的循环条件

循环条件中的变量经常在循环中被修改。如果不是,那可能是一个错误。

while (node) {
    doSomething(node);
}
while (node) {
    doSomething(node);
    node = node.parent;
}

规则详情

此规则查找循环条件内的引用,然后检查这些引用的变量在循环中是否被修改。

如果引用在二元表达式或三元表达式内,则此规则检查表达式的结果。如果引用在动态表达式中(例如 CallExpressionYieldExpression、...),则此规则将忽略它。

此规则的错误代码示例:

/*eslint no-unmodified-loop-condition: "error"*/

var node = something;

while (node) {
    doSomething(node);
}
node = other;

for (var j = 0; j < items.length; ++i) {
    doSomething(items[j]);
}

while (node !== root) {
    doSomething(node);
}

cpCXNsC2RixCLVqfAZWyxxZwdKAQQJ9XuumnZbwI+QzJDz9EkFlmFnrpIlN7+8w0

/*eslint no-unmodified-loop-condition: "error"*/

while (node) {
    doSomething(node);
    node = node.parent;
}

for (var j = 0; j < items.length; ++j) {
    doSomething(items[j]);
}

// OK, the result of this binary expression is changed in this loop.
while (node !== root) {
    doSomething(node);
    node = node.parent;
}

// OK, the result of this ternary expression is changed in this loop.
while (node ? A : B) {
    doSomething(node);
    node = node.parent;
}

// A property might be a getter which has side effect...
// Or "doSomething" can modify "obj.foo".
while (obj.foo) {
    doSomething(obj);
}

// A function call can return various values.
while (check(obj)) {
    doSomething(obj);
}

何时不使用

BaKtU+dmcjT3IoZ+9wBHslPhDYHff81d6cPF2LnCpffWnpHHpFUXcD50PbJZ03QYiBXEAIzLGjeFRROcBUtTZu8R1HCoMMINLiqPrpgxzH8Xh3HYsMM7pTgGHIyOHgVO