no-constant-condition
禁止在条件中使用常量表达式
在 配置文件 中使用来自 @eslint/js 的 recommended 配置可以启用此规则
作为测试条件的常量表达式(例如,字面)可能是特定行为的拼写错误或开发触发器。例如,以下代码看起来好像还没有准备好投入生产。
¥A constant expression (for example, a literal) as a test condition might be a typo or development trigger for a specific behavior. For example, the following code looks as if it is not ready for production.
if (false) {
doSomethingUnfinished();
}
规则详情
¥Rule Details
此规则不允许在以下测试条件中使用常量表达式:
¥This rule disallows constant expressions in the test condition of:
-
if、for、while或do...while语句¥
if,for,while, ordo...whilestatement -
?:三元表达式¥
?:ternary expression
此规则的错误代码示例:
¥Examples of incorrect code for this rule:
/*eslint no-constant-condition: "error"*/
if (false) {
doSomethingUnfinished();
}
if (void x) {
doSomethingUnfinished();
}
if (x &&= false) {
doSomethingNever();
}
if (class {}) {
doSomethingAlways();
}
if (new Boolean(x)) {
doSomethingAlways();
}
if (Boolean(1)) {
doSomethingAlways();
}
if (undefined) {
doSomethingUnfinished();
}
if (x ||= true) {
doSomethingAlways();
}
for (;-2;) {
doSomethingForever();
}
while (typeof x) {
doSomethingForever();
}
do {
doSomethingForever();
} while (x = -1);
const result = 0 ? a : b;
if(input === "hello" || "bye"){
output(input);
}
此规则的正确代码示例:
¥Examples of correct code for this rule:
/*eslint no-constant-condition: "error"*/
if (x === 0) {
doSomething();
}
for (;;) {
doSomethingForever();
}
while (typeof x === "undefined") {
doSomething();
}
do {
doSomething();
} while (x);
const result = x !== 0 ? a : b;
if(input === "hello" || input === "bye"){
output(input);
}
选项
¥Options
checkLoops
这是一个具有以下值的字符串选项:
¥This is a string option having following values:
-
"all"- 禁止在所有循环中使用常量表达式。¥
"all"- Disallow constant expressions in all loops. -
"allExceptWhileTrue"(默认) - 除带有表达式true的while循环外,所有循环中不允许使用常量表达式。¥
"allExceptWhileTrue"(default) - Disallow constant expressions in all loops exceptwhileloops with expressiontrue. -
"none"- 允许在循环中使用常量表达式。¥
"none"- Allow constant expressions in loops.
或者,你可以将 checkLoops 值设置为布尔值,其中 true 与 "all" 相同,false 与 "none" 相同。
¥Or instead you can set the checkLoops value to booleans where true is same as "all" and false is same as "none".
当 checkLoops 为 "all" 或 true 时的错误代码示例:
¥Examples of incorrect code for when checkLoops is "all" or true:
/*eslint no-constant-condition: ["error", { "checkLoops": "all" }]*/
while (true) {
doSomething();
};
for (;true;) {
doSomething();
};
/*eslint no-constant-condition: ["error", { "checkLoops": true }]*/
while (true) {
doSomething();
};
do {
doSomething();
} while (true)
checkLoops 为 "all" 或 true 时的正确代码示例:
¥Examples of correct code for when checkLoops is "all" or true:
/*eslint no-constant-condition: ["error", { "checkLoops": "all" }]*/
while (a === b) {
doSomething();
};
/*eslint no-constant-condition: ["error", { "checkLoops": true }]*/
for (let x = 0; x <= 10; x++) {
doSomething();
};
checkLoops 为 "allExceptWhileTrue" 时的正确代码示例:
¥Example of correct code for when checkLoops is "allExceptWhileTrue":
/*eslint no-constant-condition: "error"*/
while (true) {
doSomething();
};
checkLoops 为 "none" 或 false 时的正确代码示例:
¥Examples of correct code for when checkLoops is "none" or false:
/*eslint no-constant-condition: ["error", { "checkLoops": "none" }]*/
while (true) {
doSomething();
if (condition()) {
break;
}
};
do {
doSomething();
if (condition()) {
break;
}
} while (true)
/*eslint no-constant-condition: ["error", { "checkLoops": false }]*/
while (true) {
doSomething();
if (condition()) {
break;
}
};
for (;true;) {
doSomething();
if (condition()) {
break;
}
};
相关规则
版本
此规则是在 ESLint v0.4.1 中引入。