# no-unmodified-loop-condition
禁止未修改的循环条件
循环条件中的变量经常在循环中被修改。如果不是,那可能是一个错误。
while (node) {
doSomething(node);
}
while (node) {
doSomething(node);
node = node.parent;
}
# 规则详情
此规则查找循环条件内的引用,然后检查这些引用的变量在循环中是否被修改。
如果引用在二元表达式或三元表达式内,则此规则检查表达式的结果。如果引用在动态表达式中(例如 CallExpression
、YieldExpression
、...),则此规则将忽略它。
此规则的错误代码示例:
/*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);
}
此规则的正确代码示例:
/*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);
}
# 何时不使用
如果您不想通知循环条件内的引用,那么禁用此规则是安全的。