array_callback_return

package
v0.1.14 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2025 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ArrayCallbackReturnRule = rule.CreateRule(rule.Rule{
	Name: "array-callback-return",
	Run: func(ctx rule.RuleContext, options any) rule.RuleListeners {
		opts := parseOptions(options)

		return rule.RuleListeners{
			ast.KindCallExpression: func(node *ast.Node) {
				methodName := getMethodName(node)
				isArrayFrom := false

				if isArrayFromCall(node) {
					methodName = "from"
					isArrayFrom = true
				}

				if methodName == "" {
					return
				}

				isTarget := isTargetMethod(methodName)
				isForEach := opts.CheckForEach && isForEachMethod(methodName)

				if !isTarget && !isForEach && !isArrayFrom {
					return
				}

				args := node.Arguments()
				if len(args) == 0 {
					return
				}

				var callbackArg *ast.Node
				if isArrayFrom {

					if len(args) >= 2 {
						callbackArg = args[1]
					}
				} else {

					callbackArg = args[0]
				}

				if callbackArg == nil {
					return
				}

				if !isFunctionNode(callbackArg) {
					return
				}

				checkCallbackReturn(ctx, callbackArg, methodName, opts, isForEach)
			},
		}
	},
})

ArrayCallbackReturnRule enforces return statements in callbacks of array methods

Functions

This section is empty.

Types

type Options

type Options struct {
	AllowImplicit bool `json:"allowImplicit"`
	CheckForEach  bool `json:"checkForEach"`
	AllowVoid     bool `json:"allowVoid"`
}

Options for array-callback-return rule

Jump to

Keyboard shortcuts

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