2 * @fileoverview Rule to disallow uses of await inside of loops.
3 * @author Nat Mote (nmote)
8 * Check whether it should stop traversing ancestors at the given node.
9 * @param {ASTNode} node A node to check.
10 * @returns {boolean} `true` if it should stop traversing.
12 function isBoundary(node) {
16 t === "FunctionDeclaration" ||
17 t === "FunctionExpression" ||
18 t === "ArrowFunctionExpression" ||
21 * Don't report the await expressions on for-await-of loop since it's
22 * asynchronous iteration intentionally.
24 (t === "ForOfStatement" && node.await === true)
29 * Check whether the given node is in loop.
30 * @param {ASTNode} node A node to check.
31 * @param {ASTNode} parent A parent node to check.
32 * @returns {boolean} `true` if the node is in loop.
34 function isLooped(node, parent) {
35 switch (parent.type) {
38 node === parent.test ||
39 node === parent.update ||
43 case "ForOfStatement":
44 case "ForInStatement":
45 return node === parent.body;
47 case "WhileStatement":
48 case "DoWhileStatement":
49 return node === parent.test || node === parent.body;
61 description: "disallow `await` inside of loops",
62 category: "Possible Errors",
64 url: "https://eslint.org/docs/rules/no-await-in-loop"
70 unexpectedAwait: "Unexpected `await` inside a loop."
76 * Validate an await expression.
77 * @param {ASTNode} awaitNode An AwaitExpression or ForOfStatement node to validate.
80 function validate(awaitNode) {
81 if (awaitNode.type === "ForOfStatement" && !awaitNode.await) {
86 let parent = node.parent;
88 while (parent && !isBoundary(parent)) {
89 if (isLooped(node, parent)) {
92 messageId: "unexpectedAwait"
97 parent = parent.parent;
102 AwaitExpression: validate,
103 ForOfStatement: validate