no-unreachable-loop

禁止带有仅允许一次迭代的主体的循环

永远无法到达第二次迭代的循环可能是代码中的错误。

for (let i = 0; i < arr.length; i++) {
    if (arr[i].name === myName) {
        doSomething(arr[i]);
        // break was supposed to be here
    }
    break;
}

在只有一次迭代(或最多一次迭代)是预期行为的极少数情况下,应重构代码以使用 if 条件而不是 whiledo-whilefor 循环。在这种情况下避免使用循环构造被认为是最佳实践。

规则详情

此规则旨在通过对循环体执行静态代码路径分析来检测和禁止最多可以进行一次迭代的循环。

特别是,该规则将禁止循环体的主体在所有代码路径中都退出循环。如果循环体中的所有代码路径都以 breakreturnthrow 语句结尾,那么无论循环的条件如何,这种循环的第二次迭代肯定是无法到达的。

gPQ8UBvT5t4xkwu3YwcKbA9wvldYxW/tCLMyl+i/xtlSFsqSX5Irqx9yc6X4GJ/BS1xIzZulSLt/57JQ4EygIEa9mrfBRSADEmXj2TouoW4s17t0xDUSxzTZ9jt06cz79Y5SGiazoSl2AUkPn7lYEptPabVXPi/NkCfC3FPROmARjltkmmKid9s4l4skZTZBiuOJszV8n0lZvb3W7jap/rnF3TWlUPujxI+7UEI1Fbx+k0vA8C0pSg3cWGDt481FckoYBXcmHdoWxlxde6q4pg==

BBJWkULQKXOKIgBNo1Kf75ZIz4Bid3wdSxQL2AwoPrKMXjKPkT5hdLtnrWwkCnu5

/*eslint no-unreachable-loop: "error"*/

while (foo) {
    doSomething(foo);
    foo = foo.parent;
    break;
}

function verifyList(head) {
    let item = head;
    do {
        if (verify(item)) {
            return true;
        } else {
            return false;
        }
    } while (item);
}

function findSomething(arr) {
    for (var i = 0; i < arr.length; i++) {
        if (isSomething(arr[i])) {
            return arr[i];
        } else {
            throw new Error("Doesn't exist.");
        }
    }
}

for (key in obj) {
    if (key.startsWith("_")) {
        break;
    }
    firstKey = key;
    firstValue = obj[key];
    break;
}

for (foo of bar) {
    if (foo.id === id) {
        doSomething(foo);
    }
    break;
}

PwR5DrJrN80min96hKNCvTqBajX6zEp21bd6ZYasO8zgB97mz8MBbdVstv/4Rxmy

/*eslint no-unreachable-loop: "error"*/

while (foo) {
    doSomething(foo);
    foo = foo.parent;
}

function verifyList(head) {
    let item = head;
    do {
        if (verify(item)) {
            item = item.next;
        } else {
            return false;
        }
    } while (item);

    return true;
}

function findSomething(arr) {
    for (var i = 0; i < arr.length; i++) {
        if (isSomething(arr[i])) {
            return arr[i];
        }
    }
    throw new Error("Doesn't exist.");
}

for (key in obj) {
    if (key.startsWith("_")) {
        continue;
    }
    firstKey = key;
    firstValue = obj[key];
    break;
}

for (foo of bar) {
    if (foo.id === id) {
        doSomething(foo);
        break;
    }
}

ustR0CtWeQodAZSlsWhHGUR6thO1VY3Lp49DC1N4oQyG+GNnThp+SuMCcTM1wc056aJvRk1SrZ4kOtVpz1lXHVvY5tSt8Y+a+4AulIkgK8b5MJvciXrZ0gwXJZmP0I0Ja7fF34B+Kq6dr3LYA3RrHA==

Vi1MA0gX0hb9ey5ubXoJyavOivFHx2CWAJxFMsR/4eB06b06vdpCnfD6SIi57xkl

/*eslint no-unreachable-loop: "error"*/

do {
    doSomething();
} while (false)

for (let i = 0; i < 1; i++) {
    doSomething(i);
}

for (const a of [1]) {
    doSomething(a);
}

选项

p1sIsUzWj7eKM+EUv2HNlnsnvE1bT5JSf47c560J9CAQteljDG52BP05C7KXb/BZQkUFMTnFpXWNrfdkSbFLIQ==

    27oQYMpxofsbVaZ5ZG5b1uTR4VFxwiWFSS5ltJymk2n2k/nJl/yAj6qHWPR6HNAXKmBflS8g8eVw2IotIfmvW97JHBWKa9pCTElYenru0f+Dle2PczNwNCvf4otCJzHH

ignore

oRAZz9WkpUAzE7EbhkUy2xtRve34Cap4W4MLxhHt3LiNDYxH/JSt8SqV6ISPpt/XjDTg5u9Y8BYuRdxsW0PTlGZZMz+xY7omY/duxxeqwMgg2qrAu05i46LimTMY4TDL

    I0NvQEb9QeVmO9e3LhsoSLHWiYID6akBjTtwPQX3Y4Ngp7kTZTUdsajuCn24LEHKaWQQ9gD3VcJMSN2Aw24HZiU+56tw8XjuB6pZghtoxhZrRXzOZf2m5KCzehuIyV2CE7F6gSNR2J4mpaU/hJPDs1yEOYCqcUuLgEMhiNfuCU8Sa9KX1fRnefxrK7o1cwkC705JJzMY1XQiCu54tT5l1nC1eq490wf7Yzj6fI/SFaYgJw1cOkoM6BRKIU+2Ji2wwFUYBU2/4qZJn/v6jyydevOP+iyhHAud9OjnZBMvL4em8U7VaKqWQTxpaSag708DTI1K9OIY9BM+yu4/xHj7lCCpNZsIU6BIAucnOAH6f9FpPZs5zChDnTTNppH/aJipHq2HNOGgO0elyf26bJOJsDEscjsGzNH/9l+FfTD1azjQtWZinkdESZdICyy+jRbMeeYEofqsuBfleJ75jEncMVZt0tBbOEXpI/oXpGen8KVziNC4DB4wlDI53yy3qzOWptwsB/nAmNtpPxC12gf0DOZ8VBuwUcyAE5rgDaFkXmdQJTKe/OHp3hSA0YVuoC5F+6dGWGijmLrE0td5BZoavY8JQzXMHkz00w+yiEFOR95DcWrVlPbnwjS64kVbF+QbLBzo2Mc/7o883ezcUYC28g==

cHf/0vFMNJSQMO1JxuxkQFsBjcMr3jeTYO/8Jwqcs1xAsNl6eYbed5yV+sgkBJ9z+UNNUkiMd8X7CJ5T9hGUEBME49tqm0L8hGfs79QP2eY=

/*eslint no-unreachable-loop: ["error", { "ignore": ["ForInStatement", "ForOfStatement"] }]*/

for (var key in obj) {
  hasEnumerableProperties = true;
  break;
}

for (const a of b) break;

已知限制

RVg79PjjGB1dneqxWzADSGJhv6PI68EinBAL3zGyHAz1AtF6oVZTuK/QAnGuwxcB5SiT/TvbmqQRBeXquYdK5IgGiP7GcyS0JXkG4jbekyv3705KxJcfCk1Gc64RUO4G7/jdgn5XAhgrTJXtAguWV3IXplH2sC5IBIvS+ssuPYw=

for (let i = 0; i < 10; i++) {
    doSomething(i);
    if (true) {
        break;
    }
}