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: boolean | "all" | "allExceptWhileTrue" | "none"
如何使用
To enable this rule using the config file or in the CLI, you can use:
json
{
"rules": {
"no-constant-condition": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
rules: {
"no-constant-condition": "error",
},
});bash
oxlint --deny no-constant-condition版本
此规则在 v0.0.3 中添加。
