项目原始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.

require-yield.js 2.0 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * @fileoverview Rule to flag the generator functions that does not have yield.
  3. * @author Toru Nagashima
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. module.exports = {
  10. meta: {
  11. docs: {
  12. description: "require generator functions to contain `yield`",
  13. category: "ECMAScript 6",
  14. recommended: true,
  15. url: "https://eslint.org/docs/rules/require-yield"
  16. },
  17. schema: []
  18. },
  19. create(context) {
  20. const stack = [];
  21. /**
  22. * If the node is a generator function, start counting `yield` keywords.
  23. * @param {Node} node - A function node to check.
  24. * @returns {void}
  25. */
  26. function beginChecking(node) {
  27. if (node.generator) {
  28. stack.push(0);
  29. }
  30. }
  31. /**
  32. * If the node is a generator function, end counting `yield` keywords, then
  33. * reports result.
  34. * @param {Node} node - A function node to check.
  35. * @returns {void}
  36. */
  37. function endChecking(node) {
  38. if (!node.generator) {
  39. return;
  40. }
  41. const countYield = stack.pop();
  42. if (countYield === 0 && node.body.body.length > 0) {
  43. context.report({ node, message: "This generator function does not have 'yield'." });
  44. }
  45. }
  46. return {
  47. FunctionDeclaration: beginChecking,
  48. "FunctionDeclaration:exit": endChecking,
  49. FunctionExpression: beginChecking,
  50. "FunctionExpression:exit": endChecking,
  51. // Increases the count of `yield` keyword.
  52. YieldExpression() {
  53. /* istanbul ignore else */
  54. if (stack.length > 0) {
  55. stack[stack.length - 1] += 1;
  56. }
  57. }
  58. };
  59. }
  60. };