]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/no-await-in-loop.js
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",
63 url
: "https://eslint.org/docs/rules/no-await-in-loop"
69 unexpectedAwait
: "Unexpected `await` inside a loop."
75 * Validate an await expression.
76 * @param {ASTNode} awaitNode An AwaitExpression or ForOfStatement node to validate.
79 function validate(awaitNode
) {
80 if (awaitNode
.type
=== "ForOfStatement" && !awaitNode
.await
) {
85 let parent
= node
.parent
;
87 while (parent
&& !isBoundary(parent
)) {
88 if (isLooped(node
, parent
)) {
91 messageId
: "unexpectedAwait"
96 parent
= parent
.parent
;
101 AwaitExpression
: validate
,
102 ForOfStatement
: validate