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-yield.js 2.0KB

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