no_loop_func

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 NoLoopFuncRule = rule.Rule{
	Name:             "no-loop-func",
	RequiresTypeInfo: true,
	Run: func(ctx rule.RuleContext, options any) rule.RuleListeners {

		if ctx.TypeChecker == nil {
			return rule.RuleListeners{}
		}
		s := &RunState{
			Ctx:          ctx,
			SkippedIIFEs: map[*ast.Node]bool{},
		}
		check := func(node *ast.Node) {
			s.checkForLoopsCore(node)
		}
		return rule.RuleListeners{
			ast.KindArrowFunction:       check,
			ast.KindFunctionExpression:  check,
			ast.KindFunctionDeclaration: check,

			ast.KindMethodDeclaration: check,
			ast.KindGetAccessor:       check,
			ast.KindSetAccessor:       check,
			ast.KindConstructor:       check,
		}
	},
}

NoLoopFuncRule disallows function declarations that contain unsafe references to variable(s) inside loop statements.

Functions

func BuildUnsafeRefsMessage added in v0.5.2

func BuildUnsafeRefsMessage(varNames string) rule.RuleMessage

func GetDeclListForSymbolDecl added in v0.5.2

func GetDeclListForSymbolDecl(decl *ast.Node) *ast.Node

getDeclListForSymbolDecl returns the VariableDeclarationList associated with a declaration node, or nil if the declaration is not a variable-like binding.

func GetVarDeclListKind added in v0.5.2

func GetVarDeclListKind(declList *ast.Node) string

GetVarDeclListKind is a thin wrapper around utils.GetVarDeclListKind kept for backwards compatibility with the typescript-eslint counterpart in internal/plugins/typescript/rules/no_loop_func, which imports this symbol. New callers should use utils.GetVarDeclListKind directly.

func IsAsyncOrGenerator added in v0.5.2

func IsAsyncOrGenerator(node *ast.Node) bool

isAsyncOrGenerator reports whether a function-like node is declared `async` or a generator (`function*` / `*m()`).

func IsIIFE added in v0.5.2

func IsIIFE(node *ast.Node) bool

isIIFE reports whether `node` is a function directly invoked as the callee of a call expression (e.g. `(function () {})()`). The function may be wrapped in one or more parenthesized expressions.

func IsWriteRef added in v0.5.2

func IsWriteRef(node *ast.Node) bool

isWriteRef reports whether an identifier participates in a write to its variable. Extends utils.IsWriteReference with the cases ESLint's scope manager marks as writes but we don't otherwise detect: the binding names of `var/let/const` declarations with initializers, the bindings introduced by `for (var/let/const ... in/of ...)` iteration, and catch-clause parameters (bound anew per thrown exception).

Types

type ReferenceEntry added in v0.5.2

type ReferenceEntry struct {
	// contains filtered or unexported fields
}

ReferenceEntry captures a single identifier occurrence and its resolved variable symbol, preserved in source order for first-seen deduplication.

func (ReferenceEntry) Name added in v0.5.2

func (r ReferenceEntry) Name() string

Name returns the textual name of the identifier reference.

func (ReferenceEntry) Node added in v0.5.2

func (r ReferenceEntry) Node() *ast.Node

Node returns the identifier AST node.

func (ReferenceEntry) Symbol added in v0.5.2

func (r ReferenceEntry) Symbol() *ast.Symbol

Symbol returns the resolved symbol for the reference, or nil if unresolved.

type RunState added in v0.5.2

type RunState struct {
	Ctx          rule.RuleContext
	SkippedIIFEs map[*ast.Node]bool
	// contains filtered or unexported fields
}

func NewRunState added in v0.5.2

func NewRunState(ctx rule.RuleContext) *RunState

NewRunState constructs a RunState ready for one source-file pass.

func (*RunState) CollectThroughReferences added in v0.5.2

func (s *RunState) CollectThroughReferences(funcNode *ast.Node) []ReferenceEntry

collectThroughReferences walks the function-like node's parameters and body and returns all identifier references whose resolved symbol has at least one declaration outside the function subtree. The function's own name node is excluded from the walk (it's the declaration, not a reference).

func (*RunState) ForEachReference added in v0.5.2

func (s *RunState) ForEachReference(sym *ast.Symbol, cb func(*ast.Node) bool)

forEachReference invokes `cb` for every identifier node resolving to `sym`, in source order. Returns early when `cb` returns true. The underlying index is built on first use via buildRefIndex so subsequent calls are O(refs).

func (*RunState) GetContainingLoopNode added in v0.5.2

func (s *RunState) GetContainingLoopNode(node *ast.Node) *ast.Node

getContainingLoopNode walks up from `node` and returns the nearest enclosing loop statement. Returns nil if no loop is encountered before a non-skipped function-like ancestor is reached.

func (*RunState) GetTopLoopNode added in v0.5.2

func (s *RunState) GetTopLoopNode(node *ast.Node, excludedNode *ast.Node) *ast.Node

getTopLoopNode walks up through containing loops until it finds the outermost loop whose start is not before `excludedNode`'s end. If `excludedNode` is nil, walks to the outermost loop.

Jump to

Keyboard shortcuts

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