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 语句

    ¥if, for, while, or do...while statement

  • ?: 三元表达式

    ¥?: 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);

var 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);

var 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"(默认) - 除带有表达式 truewhile 循环外,所有循环中不允许使用常量表达式。

    ¥"allExceptWhileTrue" (default) - Disallow constant expressions in all loops except while loops with expression true.

  • "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 中引入。

资源

ESLint 中文网
粤ICP备13048890号