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
类型:"all" | "allExceptWhileTrue" | "none"
默认值:"allExceptWhileTrue"
配置选项,用于指定是否检查循环中的常量条件。
"all"或true禁止循环中的常量表达式"allExceptWhileTrue"禁止循环中的常量表达式,但表达式为true的 while 循环除外"none"或false允许循环中的常量表达式
如何使用
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 中添加。
