1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- "use strict";
- module.exports = {
- meta: {
- type: "suggestion",
- docs: {
- description: "Require generator functions to contain `yield`",
- recommended: true,
- url: "https://eslint.org/docs/latest/rules/require-yield"
- },
- schema: [],
- messages: {
- missingYield: "This generator function does not have 'yield'."
- }
- },
- create(context) {
- const stack = [];
-
- function beginChecking(node) {
- if (node.generator) {
- stack.push(0);
- }
- }
-
- function endChecking(node) {
- if (!node.generator) {
- return;
- }
- const countYield = stack.pop();
- if (countYield === 0 && node.body.body.length > 0) {
- context.report({ node, messageId: "missingYield" });
- }
- }
- return {
- FunctionDeclaration: beginChecking,
- "FunctionDeclaration:exit": endChecking,
- FunctionExpression: beginChecking,
- "FunctionExpression:exit": endChecking,
-
- YieldExpression() {
- if (stack.length > 0) {
- stack[stack.length - 1] += 1;
- }
- }
- };
- }
- };
|