eslint/no-constant-condition 正确性
它做了什么
禁止在条件中使用常量表达式
为什么这是不好的?
在测试条件中使用常量表达式(例如字面量)可能是拼写错误,或是用于触发特定行为的开发标记。
此规则禁止在以下结构的测试条件中使用常量表达式:
if、for、while或do...while语句?:三元表达式
示例
此规则的 错误 代码示例:
js
if (false) {
doSomethingUnfinished();
}
if (new Boolean(x)) {
doSomethingAlways();
}
if ((x ||= true)) {
doSomethingAlways();
}
do {
doSomethingForever();
} while ((x = -1));此规则的 正确 代码示例:
js
if (x === 0) {
doSomething();
}
while (typeof x === "undefined") {
doSomething();
}配置
此规则接受一个配置对象,包含以下属性:
checkLoops
type: "all" | "allExceptWhileTrue" | "none"
default: "allExceptWhileTrue"
配置选项,用于指定是否检查循环中的常量条件。
"all"或true:禁止在循环中使用常量表达式"allExceptWhileTrue":禁止在循环中使用常量表达式,但允许while循环中表达式为true的情况"none"或false:允许在循环中使用常量表达式
如何使用
要通过配置文件或 CLI 启用 此规则,可以使用:
json
{
"rules": {
"no-constant-condition": "error"
}
}bash
oxlint --deny no-constant-condition