require-await.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. /** @type {import('../shared/types').Rule} */
  25. module.exports = {
  26. meta: {
  27. type: "suggestion",
  28. docs: {
  29. description: "Disallow async functions which have no `await` expression",
  30. recommended: false,
  31. url: "https://eslint.org/docs/latest/rules/require-await"
  32. },
  33. schema: [],
  34. messages: {
  35. missingAwait: "{{name}} has no 'await' expression.",
  36. removeAsync: "Remove 'async'."
  37. },
  38. hasSuggestions: true
  39. },
  40. create(context) {
  41. const sourceCode = context.sourceCode;
  42. let scopeInfo = null;
  43. /**
  44. * Push the scope info object to the stack.
  45. * @returns {void}
  46. */
  47. function enterFunction() {
  48. scopeInfo = {
  49. upper: scopeInfo,
  50. hasAwait: false
  51. };
  52. }
  53. /**
  54. * Pop the top scope info object from the stack.
  55. * Also, it reports the function if needed.
  56. * @param {ASTNode} node The node to report.
  57. * @returns {void}
  58. */
  59. function exitFunction(node) {
  60. if (!node.generator && node.async && !scopeInfo.hasAwait && !astUtils.isEmptyFunction(node)) {
  61. /*
  62. * If the function belongs to a method definition or
  63. * property, then the function's range may not include the
  64. * `async` keyword and we should look at the parent instead.
  65. */
  66. const nodeWithAsyncKeyword =
  67. (node.parent.type === "MethodDefinition" && node.parent.value === node) ||
  68. (node.parent.type === "Property" && node.parent.method && node.parent.value === node)
  69. ? node.parent
  70. : node;
  71. const asyncToken = sourceCode.getFirstToken(nodeWithAsyncKeyword, token => token.value === "async");
  72. const asyncRange = [asyncToken.range[0], sourceCode.getTokenAfter(asyncToken, { includeComments: true }).range[0]];
  73. /*
  74. * Removing the `async` keyword can cause parsing errors if the current
  75. * statement is relying on automatic semicolon insertion. If ASI is currently
  76. * being used, then we should replace the `async` keyword with a semicolon.
  77. */
  78. const nextToken = sourceCode.getTokenAfter(asyncToken);
  79. const addSemiColon =
  80. nextToken.type === "Punctuator" &&
  81. (nextToken.value === "[" || nextToken.value === "(") &&
  82. (nodeWithAsyncKeyword.type === "MethodDefinition" || astUtils.isStartOfExpressionStatement(nodeWithAsyncKeyword)) &&
  83. astUtils.needsPrecedingSemicolon(sourceCode, nodeWithAsyncKeyword);
  84. context.report({
  85. node,
  86. loc: astUtils.getFunctionHeadLoc(node, sourceCode),
  87. messageId: "missingAwait",
  88. data: {
  89. name: capitalizeFirstLetter(
  90. astUtils.getFunctionNameWithKind(node)
  91. )
  92. },
  93. suggest: [{
  94. messageId: "removeAsync",
  95. fix: fixer => fixer.replaceTextRange(asyncRange, addSemiColon ? ";" : "")
  96. }]
  97. });
  98. }
  99. scopeInfo = scopeInfo.upper;
  100. }
  101. return {
  102. FunctionDeclaration: enterFunction,
  103. FunctionExpression: enterFunction,
  104. ArrowFunctionExpression: enterFunction,
  105. "FunctionDeclaration:exit": exitFunction,
  106. "FunctionExpression:exit": exitFunction,
  107. "ArrowFunctionExpression:exit": exitFunction,
  108. AwaitExpression() {
  109. if (!scopeInfo) {
  110. return;
  111. }
  112. scopeInfo.hasAwait = true;
  113. },
  114. ForOfStatement(node) {
  115. if (!scopeInfo) {
  116. return;
  117. }
  118. if (node.await) {
  119. scopeInfo.hasAwait = true;
  120. }
  121. }
  122. };
  123. }
  124. };