项目原始demo,不改动
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
Це архівний репозитарій. Ви можете переглядати і клонувати файли, але не можете робити пуш або відкривати питання/запити.
 
 
 
 

102 рядки
3.0 KiB

  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("../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. 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. },
  35. create(context) {
  36. const sourceCode = context.getSourceCode();
  37. let scopeInfo = null;
  38. /**
  39. * Push the scope info object to the stack.
  40. *
  41. * @returns {void}
  42. */
  43. function enterFunction() {
  44. scopeInfo = {
  45. upper: scopeInfo,
  46. hasAwait: false
  47. };
  48. }
  49. /**
  50. * Pop the top scope info object from the stack.
  51. * Also, it reports the function if needed.
  52. *
  53. * @param {ASTNode} node - The node to report.
  54. * @returns {void}
  55. */
  56. function exitFunction(node) {
  57. if (node.async && !scopeInfo.hasAwait && !astUtils.isEmptyFunction(node)) {
  58. context.report({
  59. node,
  60. loc: astUtils.getFunctionHeadLoc(node, sourceCode),
  61. message: "{{name}} has no 'await' expression.",
  62. data: {
  63. name: capitalizeFirstLetter(
  64. astUtils.getFunctionNameWithKind(node)
  65. )
  66. }
  67. });
  68. }
  69. scopeInfo = scopeInfo.upper;
  70. }
  71. return {
  72. FunctionDeclaration: enterFunction,
  73. FunctionExpression: enterFunction,
  74. ArrowFunctionExpression: enterFunction,
  75. "FunctionDeclaration:exit": exitFunction,
  76. "FunctionExpression:exit": exitFunction,
  77. "ArrowFunctionExpression:exit": exitFunction,
  78. AwaitExpression() {
  79. scopeInfo.hasAwait = true;
  80. },
  81. ForOfStatement(node) {
  82. if (node.await) {
  83. scopeInfo.hasAwait = true;
  84. }
  85. }
  86. };
  87. }
  88. };