max_nested_callbacks

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: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var MaxNestedCallbacksRule = rule.Rule{
	Name: "max-nested-callbacks",
	Run: func(ctx rule.RuleContext, options any) rule.RuleListeners {
		threshold := parseThreshold(options)

		// `callbackStack` mirrors ESLint's stack of FunctionExpression /
		// ArrowFunctionExpression nodes that sit *directly* under a
		// CallExpression — i.e. function-likes used as call arguments or as
		// the callee of an immediately-invoked call. Push happens only when
		// the (paren-flattened) parent is a CallExpression; pop fires on
		// every function-like exit, even when the entry didn't push. The
		// asymmetry is preserved verbatim from ESLint to keep diagnostic
		// counts byte-for-byte identical with upstream.
		var callbackStack []*ast.Node

		check := func(node *ast.Node) {

			parent := ast.WalkUpParenthesizedExpressions(node.Parent)
			if parent != nil && ast.IsCallExpression(parent) {
				callbackStack = append(callbackStack, node)
			}
			if len(callbackStack) > threshold {
				ctx.ReportNode(node, buildExceedMessage(len(callbackStack), threshold))
			}
		}
		pop := func(node *ast.Node) {

			if len(callbackStack) > 0 {
				callbackStack = callbackStack[:len(callbackStack)-1]
			}
		}

		return rule.RuleListeners{

			ast.KindFunctionExpression:                      check,
			rule.ListenerOnExit(ast.KindFunctionExpression): pop,
			ast.KindArrowFunction:                           check,
			rule.ListenerOnExit(ast.KindArrowFunction):      pop,

			ast.KindMethodDeclaration:                      check,
			rule.ListenerOnExit(ast.KindMethodDeclaration): pop,
			ast.KindGetAccessor:                            check,
			rule.ListenerOnExit(ast.KindGetAccessor):       pop,
			ast.KindSetAccessor:                            check,
			rule.ListenerOnExit(ast.KindSetAccessor):       pop,
			ast.KindConstructor:                            check,
			rule.ListenerOnExit(ast.KindConstructor):       pop,
		}
	},
}

MaxNestedCallbacksRule enforces a maximum depth of nested callbacks. https://eslint.org/docs/latest/rules/max-nested-callbacks

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