Index

no-constant-condition

禁止在条件中使用常量表达式

Recommended

配置文件 中使用来自 @eslint/jsrecommended 配置可以启用此规则

一个常量表达式(例如,一个字面量)作为测试条件可能是一个拼写错误或用于触发特定行为的开发手段。例如,以下代码看起来似乎还没有准备好投入生产。

🌐 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:

  • ifforwhiledo...while声明
  • ?: 三元表达式

此规则的错误代码示例:

🌐 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" - 禁止在所有循环中使用常量表达式。
  • "allExceptWhileTrue"(default)- 除带有表达式 truewhile 循环外,在所有循环中禁止使用常量表达式。
  • "none" - 允许在循环中使用常量表达式。

或者,你可以将 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 中引入。

资源