no-constant-condition

禁止条件中的常量表达式

配置文件 中的 "extends": "eslint:recommended" 属性启用了该规则

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

if (false) {
    doSomethingUnfinished();
}

规则详情

此规则不允许在以下测试条件中使用常量表达式:

  • ifforwhiledo...while 语句
  • ?: 三元表达式

zblpFAyoEiDF1GS7EEgT+Fw+zDBsVLJNOcYSNQAXW6dSdz0JClE4rOOJ51S8csCM

/*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);
}

h+1Jf/rEEE6yYiKhWnK/aHl9/UYIfowckm44qhmqvwwaWj0QX+JXnab7YJgxSmDw

/*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);
}

选项

checkLoops

og93CUEbgxrdCPPqj9RS5POVJk01/vFhVV3WfQ/tekpk/2+3uJRtaptKhFBT/sS84VYqb/KiBMmKI1vEb+MReHMnR0oYG6weZaPAZgpZlC3wphRhYV3t4/eLJSZsUeSeuMdLGO78lSw2HoDxV/aGisKyQWEi4wb1MrI9N7AP/hU=

9tjVaWxzOz3aaI6du8qQ7BGtBtVDmPu+wCNWUT93vJqC6ws+NxOPXBgEECsa8C/TPZhJ9c4TZePPIDfXwP1XVWE/TYs4AXxazpvmwOu+6b8=

/*eslint no-constant-condition: ["error", { "checkLoops": false }]*/

while (true) {
    doSomething();
    if (condition()) {
        break;
    }
};

for (;true;) {
    doSomething();
    if (condition()) {
        break;
    }
};

do {
    doSomething();
    if (condition()) {
        break;
    }
} while (true)