2 * @fileoverview Rule to disallow async functions which have no `await` expression.
3 * @author Toru Nagashima
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const astUtils = require("./utils/ast-utils");
14 //------------------------------------------------------------------------------
16 //------------------------------------------------------------------------------
19 * Capitalize the 1st letter of the given text.
20 * @param {string} text The text to capitalize.
21 * @returns {string} The text that the 1st letter was capitalized.
23 function capitalizeFirstLetter(text) {
24 return text[0].toUpperCase() + text.slice(1);
27 //------------------------------------------------------------------------------
29 //------------------------------------------------------------------------------
36 description: "disallow async functions which have no `await` expression",
37 category: "Best Practices",
39 url: "https://eslint.org/docs/rules/require-await"
46 const sourceCode = context.getSourceCode();
50 * Push the scope info object to the stack.
53 function enterFunction() {
61 * Pop the top scope info object from the stack.
62 * Also, it reports the function if needed.
63 * @param {ASTNode} node The node to report.
66 function exitFunction(node) {
67 if (node.async && !scopeInfo.hasAwait && !astUtils.isEmptyFunction(node)) {
70 loc: astUtils.getFunctionHeadLoc(node, sourceCode),
71 message: "{{name}} has no 'await' expression.",
73 name: capitalizeFirstLetter(
74 astUtils.getFunctionNameWithKind(node)
80 scopeInfo = scopeInfo.upper;
84 FunctionDeclaration: enterFunction,
85 FunctionExpression: enterFunction,
86 ArrowFunctionExpression: enterFunction,
87 "FunctionDeclaration:exit": exitFunction,
88 "FunctionExpression:exit": exitFunction,
89 "ArrowFunctionExpression:exit": exitFunction,
96 scopeInfo.hasAwait = true;
98 ForOfStatement(node) {
104 scopeInfo.hasAwait = true;