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();
}Configuration
This rule accepts a configuration object with the following properties:
checkLoops
type: boolean | "all" | "allExceptWhileTrue" | "none"
How to use
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-conditionVersion
This rule was added in v0.0.3.
