项目原始demo,不改动
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

пре 4 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * @fileoverview Rule to disallow uses of await inside of loops.
  3. * @author Nat Mote (nmote)
  4. */
  5. "use strict";
  6. /**
  7. * Check whether it should stop traversing ancestors at the given node.
  8. * @param {ASTNode} node A node to check.
  9. * @returns {boolean} `true` if it should stop traversing.
  10. */
  11. function isBoundary(node) {
  12. const t = node.type;
  13. return (
  14. t === "FunctionDeclaration" ||
  15. t === "FunctionExpression" ||
  16. t === "ArrowFunctionExpression" ||
  17. /*
  18. * Don't report the await expressions on for-await-of loop since it's
  19. * asynchronous iteration intentionally.
  20. */
  21. (t === "ForOfStatement" && node.await === true)
  22. );
  23. }
  24. /**
  25. * Check whether the given node is in loop.
  26. * @param {ASTNode} node A node to check.
  27. * @param {ASTNode} parent A parent node to check.
  28. * @returns {boolean} `true` if the node is in loop.
  29. */
  30. function isLooped(node, parent) {
  31. switch (parent.type) {
  32. case "ForStatement":
  33. return (
  34. node === parent.test ||
  35. node === parent.update ||
  36. node === parent.body
  37. );
  38. case "ForOfStatement":
  39. case "ForInStatement":
  40. return node === parent.body;
  41. case "WhileStatement":
  42. case "DoWhileStatement":
  43. return node === parent.test || node === parent.body;
  44. default:
  45. return false;
  46. }
  47. }
  48. module.exports = {
  49. meta: {
  50. docs: {
  51. description: "disallow `await` inside of loops",
  52. category: "Possible Errors",
  53. recommended: false,
  54. url: "https://eslint.org/docs/rules/no-await-in-loop"
  55. },
  56. schema: [],
  57. messages: {
  58. unexpectedAwait: "Unexpected `await` inside a loop."
  59. }
  60. },
  61. create(context) {
  62. /**
  63. * Validate an await expression.
  64. * @param {ASTNode} awaitNode An AwaitExpression or ForOfStatement node to validate.
  65. * @returns {void}
  66. */
  67. function validate(awaitNode) {
  68. if (awaitNode.type === "ForOfStatement" && !awaitNode.await) {
  69. return;
  70. }
  71. let node = awaitNode;
  72. let parent = node.parent;
  73. while (parent && !isBoundary(parent)) {
  74. if (isLooped(node, parent)) {
  75. context.report({
  76. node: awaitNode,
  77. messageId: "unexpectedAwait"
  78. });
  79. return;
  80. }
  81. node = parent;
  82. parent = parent.parent;
  83. }
  84. }
  85. return {
  86. AwaitExpression: validate,
  87. ForOfStatement: validate
  88. };
  89. }
  90. };