no_unused_vars

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NoUnusedVarsRule = rule.CreateRule(rule.Rule{
	Name: "no-unused-vars",
	Run: func(ctx rule.RuleContext, options any) rule.RuleListeners {
		opts := parseOptions(options)

		allUsages := make(map[string][]*ast.Node)
		collected := false

		getRootSourceFile := func(node *ast.Node) *ast.Node {
			current := node
			for current.Parent != nil {
				current = current.Parent
			}
			return current
		}

		return rule.RuleListeners{

			ast.KindVariableDeclaration: func(node *ast.Node) {
				varDecl := node.AsVariableDeclaration()
				if varDecl == nil {
					return
				}
				if ast.IsIdentifier(varDecl.Name()) {
					nameNode := varDecl.Name()
					identifier := nameNode.AsIdentifier()
					if identifier == nil {
						return
					}
					name := identifier.Text

					if !collected {
						sourceFile := getRootSourceFile(node)
						collectVariableUsages(sourceFile, allUsages)
						collected = true
					}

					processVariable(ctx, nameNode, name, node, opts, allUsages)
				}
			},

			ast.KindFunctionDeclaration: func(node *ast.Node) {
				funcDecl := node.AsFunctionDeclaration()
				if funcDecl == nil {
					return
				}
				if funcDecl.Name() != nil && ast.IsIdentifier(funcDecl.Name()) {
					nameNode := funcDecl.Name()
					identifier := nameNode.AsIdentifier()
					if identifier == nil {
						return
					}
					name := identifier.Text

					if !collected {
						sourceFile := getRootSourceFile(node)
						collectVariableUsages(sourceFile, allUsages)
						collected = true
					}

					processVariable(ctx, nameNode, name, node, opts, allUsages)
				}
			},

			ast.KindParameter: func(node *ast.Node) {
				paramDecl := node.AsParameterDeclaration()
				if paramDecl == nil {
					return
				}
				if paramDecl.Name() != nil && ast.IsIdentifier(paramDecl.Name()) {
					nameNode := paramDecl.Name()
					identifier := nameNode.AsIdentifier()
					if identifier == nil {
						return
					}
					name := identifier.Text

					if !collected {
						sourceFile := getRootSourceFile(node)
						collectVariableUsages(sourceFile, allUsages)
						collected = true
					}

					processVariable(ctx, nameNode, name, node, opts, allUsages)
				}
			},

			ast.KindCatchClause: func(node *ast.Node) {
				catchClause := node.AsCatchClause()
				if catchClause == nil {
					return
				}
				if catchClause.VariableDeclaration != nil && ast.IsIdentifier(catchClause.VariableDeclaration) {
					nameNode := catchClause.VariableDeclaration
					identifier := nameNode.AsIdentifier()
					if identifier == nil {
						return
					}
					name := identifier.Text

					if !collected {
						sourceFile := getRootSourceFile(node)
						collectVariableUsages(sourceFile, allUsages)
						collected = true
					}

					processVariable(ctx, nameNode, name, nameNode, opts, allUsages)
				}
			},
		}
	},
})

Functions

This section is empty.

Types

type Config

type Config struct {
	Vars                      string `json:"vars"`
	VarsIgnorePattern         string `json:"varsIgnorePattern"`
	Args                      string `json:"args"`
	ArgsIgnorePattern         string `json:"argsIgnorePattern"`
	CaughtErrors              string `json:"caughtErrors"`
	CaughtErrorsIgnorePattern string `json:"caughtErrorsIgnorePattern"`
	IgnoreRestSiblings        bool   `json:"ignoreRestSiblings"`
	ReportUsedIgnorePattern   bool   `json:"reportUsedIgnorePattern"`
}

type VariableInfo

type VariableInfo struct {
	Variable       *ast.Node
	Used           bool
	OnlyUsedAsType bool
	References     []*ast.Node
	Definition     *ast.Node
}

Jump to

Keyboard shortcuts

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