Eslint参考手册
规则 | Rules
no-constant-condition
配置文件中的"extends": "eslint:recommended"
属性启用此规则。
作为测试条件的常量表达式(例如,文字)可能是特定行为的拼写错误或开发触发器。例如,以下代码看起来好像尚未准备好进行生产。
if (false) {
doSomethingUnfinished();
}
规则细节
此规则在下列测试条件中不允许使用常量表达式:
-
if
,for
,while
,或do...while
语句
-
?:
三元表达
此规则的错误代码示例:
/*eslint no-constant-condition: "error"*/
if (false) {
doSomethingUnfinished();
}
if (void x) {
doSomethingUnfinished();
}
for (;-2;) {
doSomethingForever();
}
while (typeof x) {
doSomethingForever();
}
do {
doSomethingForever();
} while (x = -1);
var result = 0 ? a : b;
此规则的正确代码示例:
/*eslint no-constant-condition: "error"*/
if (x === 0) {
doSomething();
}
for (;;) {
doSomethingForever();
}
while (typeof x === "undefined") {
doSomething();
}
do {
doSomething();
} while (x);
var result = x !== 0 ? a : b;
选项
checkLoops
默认设置为true
。设置此选项false
允许循环中的常量表达式。
正确代码的例子,当checkLoops
是false
:
/*eslint no-constant-condition: ["error", { "checkLoops": false }]*/
while (true) {
doSomething();
if (condition()) {
break;
}
};
for (;true;) {
doSomething();
if (condition()) {
break;
}
};
do {
doSomething();
if (condition()) {
break;
}
} while (true)
版本
这条规则是在 ESLint 0.4.1中引入的。
资源
- 规则资源
- 文档资源
规则 | Rules相关

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