eslint/no-constant-condition Correctness
What it does
Disallow constant expressions in conditions
Why is this bad?
A constant expression (for example, a literal) as a test condition might be a typo or development trigger for a specific behavior.
This rule disallows constant expressions in the test condition of:
if,for,while, ordo...whilestatement?: ternary expression
Examples
Examples of incorrect code for this rule:
js
if (false) {
doSomethingUnfinished();
}
if (new Boolean(x)) {
doSomethingAlways();
}
if (x ||= true) {
doSomethingAlways();
}
do {
doSomethingForever();
} while (x = -1);Examples of correct code for this rule:
js
if (x === 0) {
doSomething();
}
while (typeof x === "undefined") {
doSomething();
}Options
checkLoops
{ type: "all" | "allExceptWhileTrue" | "none" | boolean, default: "allExceptWhileTrue" }
"all"ortruedisallows constant expressions in loops"allExceptWhileTrue"disallows constant expressions in loops except while loops with expressiontrue"none"orfalseallows constant expressions in loops
How to use
To enable this rule in the CLI or using the config file, you can use:
bash
oxlint --deny no-constant-conditionjson
{
"rules": {
"no-constant-condition": "error"
}
}