require-await.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**
  2. * @fileoverview Rule to disallow async functions which have no `await` expression.
  3. * @author Toru Nagashima
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const astUtils = require("./utils/ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Helpers
  12. //------------------------------------------------------------------------------
  13. /**
  14. * Capitalize the 1st letter of the given text.
  15. * @param {string} text The text to capitalize.
  16. * @returns {string} The text that the 1st letter was capitalized.
  17. */
  18. function capitalizeFirstLetter(text) {
  19. return text[0].toUpperCase() + text.slice(1);
  20. }
  21. //------------------------------------------------------------------------------
  22. // Rule Definition
  23. //------------------------------------------------------------------------------
  24. module.exports = {
  25. meta: {
  26. type: "suggestion",
  27. docs: {
  28. description: "disallow async functions which have no `await` expression",
  29. category: "Best Practices",
  30. recommended: false,
  31. url: "https://eslint.org/docs/rules/require-await"
  32. },
  33. schema: [],
  34. messages: {
  35. missingAwait: "{{name}} has no 'await' expression."
  36. }
  37. },
  38. create(context) {
  39. const sourceCode = context.getSourceCode();
  40. let scopeInfo = null;
  41. /**
  42. * Push the scope info object to the stack.
  43. * @returns {void}
  44. */
  45. function enterFunction() {
  46. scopeInfo = {
  47. upper: scopeInfo,
  48. hasAwait: false
  49. };
  50. }
  51. /**
  52. * Pop the top scope info object from the stack.
  53. * Also, it reports the function if needed.
  54. * @param {ASTNode} node The node to report.
  55. * @returns {void}
  56. */
  57. function exitFunction(node) {
  58. if (!node.generator && node.async && !scopeInfo.hasAwait && !astUtils.isEmptyFunction(node)) {
  59. context.report({
  60. node,
  61. loc: astUtils.getFunctionHeadLoc(node, sourceCode),
  62. messageId: "missingAwait",
  63. data: {
  64. name: capitalizeFirstLetter(
  65. astUtils.getFunctionNameWithKind(node)
  66. )
  67. }
  68. });
  69. }
  70. scopeInfo = scopeInfo.upper;
  71. }
  72. return {
  73. FunctionDeclaration: enterFunction,
  74. FunctionExpression: enterFunction,
  75. ArrowFunctionExpression: enterFunction,
  76. "FunctionDeclaration:exit": exitFunction,
  77. "FunctionExpression:exit": exitFunction,
  78. "ArrowFunctionExpression:exit": exitFunction,
  79. AwaitExpression() {
  80. if (!scopeInfo) {
  81. return;
  82. }
  83. scopeInfo.hasAwait = true;
  84. },
  85. ForOfStatement(node) {
  86. if (!scopeInfo) {
  87. return;
  88. }
  89. if (node.await) {
  90. scopeInfo.hasAwait = true;
  91. }
  92. }
  93. };
  94. }
  95. };