github-pages-deploy-action/node_modules/eslint/lib/rules/require-await.js

114 lines
3.3 KiB
JavaScript
Raw Permalink Normal View History

2020-03-07 11:45:40 +08:00
/**
* @fileoverview Rule to disallow async functions which have no `await` expression.
* @author Toru Nagashima
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
2020-03-31 20:40:00 +08:00
const astUtils = require("./utils/ast-utils");
2020-03-07 11:45:40 +08:00
//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
/**
* Capitalize the 1st letter of the given text.
2020-03-31 20:40:00 +08:00
* @param {string} text The text to capitalize.
2020-03-07 11:45:40 +08:00
* @returns {string} The text that the 1st letter was capitalized.
*/
function capitalizeFirstLetter(text) {
return text[0].toUpperCase() + text.slice(1);
}
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = {
meta: {
type: "suggestion",
docs: {
description: "disallow async functions which have no `await` expression",
category: "Best Practices",
recommended: false,
url: "https://eslint.org/docs/rules/require-await"
},
2020-05-15 05:33:08 +08:00
schema: [],
messages: {
missingAwait: "{{name}} has no 'await' expression."
}
2020-03-07 11:45:40 +08:00
},
create(context) {
const sourceCode = context.getSourceCode();
let scopeInfo = null;
/**
* Push the scope info object to the stack.
* @returns {void}
*/
function enterFunction() {
scopeInfo = {
upper: scopeInfo,
hasAwait: false
};
}
/**
* Pop the top scope info object from the stack.
* Also, it reports the function if needed.
2020-03-31 20:40:00 +08:00
* @param {ASTNode} node The node to report.
2020-03-07 11:45:40 +08:00
* @returns {void}
*/
function exitFunction(node) {
2020-05-15 05:33:08 +08:00
if (!node.generator && node.async && !scopeInfo.hasAwait && !astUtils.isEmptyFunction(node)) {
2020-03-07 11:45:40 +08:00
context.report({
node,
loc: astUtils.getFunctionHeadLoc(node, sourceCode),
2020-05-15 05:33:08 +08:00
messageId: "missingAwait",
2020-03-07 11:45:40 +08:00
data: {
name: capitalizeFirstLetter(
astUtils.getFunctionNameWithKind(node)
)
}
});
}
scopeInfo = scopeInfo.upper;
}
return {
FunctionDeclaration: enterFunction,
FunctionExpression: enterFunction,
ArrowFunctionExpression: enterFunction,
"FunctionDeclaration:exit": exitFunction,
"FunctionExpression:exit": exitFunction,
"ArrowFunctionExpression:exit": exitFunction,
AwaitExpression() {
2020-03-31 20:40:00 +08:00
if (!scopeInfo) {
return;
}
2020-03-07 11:45:40 +08:00
scopeInfo.hasAwait = true;
},
ForOfStatement(node) {
2020-03-31 20:40:00 +08:00
if (!scopeInfo) {
return;
}
2020-03-07 11:45:40 +08:00
if (node.await) {
scopeInfo.hasAwait = true;
}
}
};
}
};