max_depth

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 MaxDepthRule = rule.Rule{
	Name: "max-depth",
	Run: func(ctx rule.RuleContext, options any) rule.RuleListeners {
		maxDepth := parseMaxDepth(options)

		stack := []int{0}

		startFunction := func(node *ast.Node) {
			stack = append(stack, 0)
		}
		endFunction := func(node *ast.Node) {
			if len(stack) > 1 {
				stack = stack[:len(stack)-1]
			}
		}
		pushBlock := func(node *ast.Node) {
			n := len(stack)
			if n == 0 {
				return
			}
			stack[n-1]++
			depth := stack[n-1]
			if depth > maxDepth {
				ctx.ReportNode(node, buildTooDeeplyMessage(depth, maxDepth))
			}
		}
		popBlock := func(node *ast.Node) {
			n := len(stack)
			if n == 0 {
				return
			}
			stack[n-1]--
		}

		ifEnter := func(node *ast.Node) {
			if node.Parent != nil && node.Parent.Kind == ast.KindIfStatement {
				return
			}
			pushBlock(node)
		}

		return rule.RuleListeners{
			ast.KindFunctionDeclaration:                              startFunction,
			rule.ListenerOnExit(ast.KindFunctionDeclaration):         endFunction,
			ast.KindFunctionExpression:                               startFunction,
			rule.ListenerOnExit(ast.KindFunctionExpression):          endFunction,
			ast.KindArrowFunction:                                    startFunction,
			rule.ListenerOnExit(ast.KindArrowFunction):               endFunction,
			ast.KindClassStaticBlockDeclaration:                      startFunction,
			rule.ListenerOnExit(ast.KindClassStaticBlockDeclaration): endFunction,

			ast.KindMethodDeclaration:                      startFunction,
			rule.ListenerOnExit(ast.KindMethodDeclaration): endFunction,
			ast.KindGetAccessor:                            startFunction,
			rule.ListenerOnExit(ast.KindGetAccessor):       endFunction,
			ast.KindSetAccessor:                            startFunction,
			rule.ListenerOnExit(ast.KindSetAccessor):       endFunction,
			ast.KindConstructor:                            startFunction,
			rule.ListenerOnExit(ast.KindConstructor):       endFunction,

			ast.KindIfStatement:                          ifEnter,
			rule.ListenerOnExit(ast.KindIfStatement):     popBlock,
			ast.KindSwitchStatement:                      pushBlock,
			rule.ListenerOnExit(ast.KindSwitchStatement): popBlock,
			ast.KindTryStatement:                         pushBlock,
			rule.ListenerOnExit(ast.KindTryStatement):    popBlock,
			ast.KindDoStatement:                          pushBlock,
			rule.ListenerOnExit(ast.KindDoStatement):     popBlock,
			ast.KindWhileStatement:                       pushBlock,
			rule.ListenerOnExit(ast.KindWhileStatement):  popBlock,
			ast.KindWithStatement:                        pushBlock,
			rule.ListenerOnExit(ast.KindWithStatement):   popBlock,
			ast.KindForStatement:                         pushBlock,
			rule.ListenerOnExit(ast.KindForStatement):    popBlock,
			ast.KindForInStatement:                       pushBlock,
			rule.ListenerOnExit(ast.KindForInStatement):  popBlock,
			ast.KindForOfStatement:                       pushBlock,
			rule.ListenerOnExit(ast.KindForOfStatement):  popBlock,
		}
	},
}

MaxDepthRule enforces a maximum depth that blocks can be nested in a function. https://eslint.org/docs/latest/rules/max-depth

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