Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

require-await.js 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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("../util/ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Helpers
  12. //------------------------------------------------------------------------------
  13. /**
  14. * Capitalize the 1st letter of the given text.
  15. *
  16. * @param {string} text - The text to capitalize.
  17. * @returns {string} The text that the 1st letter was capitalized.
  18. */
  19. function capitalizeFirstLetter(text) {
  20. return text[0].toUpperCase() + text.slice(1);
  21. }
  22. //------------------------------------------------------------------------------
  23. // Rule Definition
  24. //------------------------------------------------------------------------------
  25. module.exports = {
  26. meta: {
  27. type: "suggestion",
  28. docs: {
  29. description: "disallow async functions which have no `await` expression",
  30. category: "Best Practices",
  31. recommended: false,
  32. url: "https://eslint.org/docs/rules/require-await"
  33. },
  34. schema: []
  35. },
  36. create(context) {
  37. const sourceCode = context.getSourceCode();
  38. let scopeInfo = null;
  39. /**
  40. * Push the scope info object to the stack.
  41. *
  42. * @returns {void}
  43. */
  44. function enterFunction() {
  45. scopeInfo = {
  46. upper: scopeInfo,
  47. hasAwait: false
  48. };
  49. }
  50. /**
  51. * Pop the top scope info object from the stack.
  52. * Also, it reports the function if needed.
  53. *
  54. * @param {ASTNode} node - The node to report.
  55. * @returns {void}
  56. */
  57. function exitFunction(node) {
  58. if (node.async && !scopeInfo.hasAwait && !astUtils.isEmptyFunction(node)) {
  59. context.report({
  60. node,
  61. loc: astUtils.getFunctionHeadLoc(node, sourceCode),
  62. message: "{{name}} has no 'await' expression.",
  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. scopeInfo.hasAwait = true;
  81. },
  82. ForOfStatement(node) {
  83. if (node.await) {
  84. scopeInfo.hasAwait = true;
  85. }
  86. }
  87. };
  88. }
  89. };