Eslint参考手册
规则 | Rules
no-unmodified-loop-condition
循环中的变量经常在循环中修改。如果不是,那可能是一个错误。
while (node) {
doSomething(node);
}
while (node) {
doSomething(node);
node = node.parent;
}
规则细节
该规则查找循环条件内的引用,然后检查这些引用的变量是否在循环中被修改。
如果引用位于二进制表达式或三元表达式中,则此规则将检查表达式的结果。如果参考是一个动态的表达的内部(例如CallExpression
,YieldExpression
,...),该规则将忽略它。
此规则的错误代码示例:
while (node) {
doSomething(node);
}
node = other;
for (var j = 0; j < items.length; ++i) {
doSomething(items[j]);
}
while (node !== root) {
doSomething(node);
}
此规则的正确代码示例:
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);
}
何时不使用它
如果您不想在循环条件内通知引用,那么禁用此规则是安全的。
版本
此规则是在 ESLint 2.0.0-alpha-2 中引入的。
资源
- Rule source
- Documentation source
规则 | Rules相关

ESLint 是一个代码规范和错误检查工具,有以下几个特性。所有东西都是可以插拔的。你可以调用任意的 rule api 或者 formatter api 去打包或者定义 rule or formatter。任意的 rule 都是独立的。没有特定的 coding style,你可以自己配置。
主页 | https://eslint.org/ |
源码 | https://github.com/eslint/eslint |
发布版本 | 4.12.0 |