require_yield

package
v0.5.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 14, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var RequireYieldRule = rule.Rule{
	Name: "require-yield",
	Run: func(ctx rule.RuleContext, options any) rule.RuleListeners {
		stack := make([]stackFrame, 0, 8)

		enter := func(node *ast.Node) {
			stack = append(stack, stackFrame{node: node})
		}

		exit := func(node *ast.Node) {
			n := len(stack)
			if n == 0 {
				return
			}
			top := stack[n-1]
			stack = stack[:n-1]

			if top.node.Kind == ast.KindPropertyDeclaration ||
				top.node.Kind == ast.KindClassStaticBlockDeclaration {
				return
			}
			if isGenerator(top.node) && top.count == 0 && hasNonEmptyBody(top.node) {
				ctx.ReportRange(
					utils.GetFunctionHeadLoc(ctx.SourceFile, top.node),
					buildMissingYieldMessage(),
				)
			}
		}

		countYield := func(node *ast.Node) {
			for i := len(stack) - 1; i >= 0; i-- {
				bp, be, ok := bodyLikeRange(stack[i].node)
				if !ok {
					continue
				}
				if node.Pos() >= bp && node.End() <= be {
					stack[i].count++
					return
				}
			}
		}

		return rule.RuleListeners{
			ast.KindFunctionDeclaration:                              enter,
			rule.ListenerOnExit(ast.KindFunctionDeclaration):         exit,
			ast.KindFunctionExpression:                               enter,
			rule.ListenerOnExit(ast.KindFunctionExpression):          exit,
			ast.KindMethodDeclaration:                                enter,
			rule.ListenerOnExit(ast.KindMethodDeclaration):           exit,
			ast.KindArrowFunction:                                    enter,
			rule.ListenerOnExit(ast.KindArrowFunction):               exit,
			ast.KindGetAccessor:                                      enter,
			rule.ListenerOnExit(ast.KindGetAccessor):                 exit,
			ast.KindSetAccessor:                                      enter,
			rule.ListenerOnExit(ast.KindSetAccessor):                 exit,
			ast.KindConstructor:                                      enter,
			rule.ListenerOnExit(ast.KindConstructor):                 exit,
			ast.KindPropertyDeclaration:                              enter,
			rule.ListenerOnExit(ast.KindPropertyDeclaration):         exit,
			ast.KindClassStaticBlockDeclaration:                      enter,
			rule.ListenerOnExit(ast.KindClassStaticBlockDeclaration): exit,

			ast.KindYieldExpression: countYield,
		}
	},
}

https://eslint.org/docs/latest/rules/require-yield

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL