detectors

package
v1.2.0-beta Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package detectors provides anti-pattern detection for Go code.

The detectors package implements a modular system for detecting code anti-patterns and quality issues. Each detector focuses on a specific pattern and can be enabled/disabled via configuration.

Available Detectors

- ParameterCountDetector: Detects functions with too many parameters - NestingDepthDetector: Detects deeply nested code blocks - ReturnStatementDetector: Detects functions with too many return statements - MagicNumberDetector: Detects magic numbers in code - DuplicateErrorDetector: Detects repetitive error handling patterns

Detector Interface

All detectors implement the Detector interface:

type Detector interface {
    Name() string
    Enabled(cfg *config.AnalysisConfig) bool
    Detect(cfg *config.AnalysisConfig, file *parser.FileMetrics,
           fn *parser.FunctionMetrics, fset *token.FileSet,
           funcDecl *ast.FuncDecl) []*Issue
}

Usage

Create a detector registry:

cfg := config.Default()
registry := detectors.NewRegistry(&cfg.Analysis)

Run all enabled detectors on a function:

issues := registry.RunAll(fileMetrics, funcMetrics, fset, funcDecl)

The registry automatically skips disabled detectors based on configuration.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Detector

type Detector interface {
	// Name returns the detector name
	Name() string

	// Detect analyzes a function and returns any issues found
	Detect(cfg *config.AnalysisConfig, file *parser.FileMetrics, fn *parser.FunctionMetrics, fset *token.FileSet, funcDecl *ast.FuncDecl) []*Issue

	// Enabled returns whether this detector is enabled
	Enabled(cfg *config.AnalysisConfig) bool
}

Detector defines the interface for anti-pattern detection

type DuplicateErrorDetector

type DuplicateErrorDetector struct{}

DuplicateErrorDetector detects repetitive error handling patterns

func NewDuplicateErrorDetector

func NewDuplicateErrorDetector() *DuplicateErrorDetector

NewDuplicateErrorDetector creates a new duplicate error detector

func (*DuplicateErrorDetector) Detect

Detect checks for repetitive error handling patterns

func (*DuplicateErrorDetector) Enabled

Enabled returns whether this detector is enabled

func (*DuplicateErrorDetector) Name

func (d *DuplicateErrorDetector) Name() string

Name returns the detector name

type Issue

type Issue struct {
	Severity  string `json:"severity"`  // "warning", "info", "error"
	Type      string `json:"type"`      // "large_file", "long_function", etc.
	File      string `json:"file"`      // File path
	Line      int    `json:"line"`      // Line number (0 if not applicable)
	Function  string `json:"function"`  // Function name (empty if not applicable)
	Message   string `json:"message"`   // Human-readable message
	Value     int    `json:"value"`     // Actual value (e.g., line count)
	Threshold int    `json:"threshold"` // Threshold value
}

Issue represents a code quality issue found during analysis

type MagicNumberDetector

type MagicNumberDetector struct{}

MagicNumberDetector detects magic numbers (numeric literals)

func NewMagicNumberDetector

func NewMagicNumberDetector() *MagicNumberDetector

NewMagicNumberDetector creates a new magic number detector

func (*MagicNumberDetector) Detect

Detect checks for magic numbers in a function

func (*MagicNumberDetector) Enabled

func (d *MagicNumberDetector) Enabled(cfg *config.AnalysisConfig) bool

Enabled returns whether this detector is enabled

func (*MagicNumberDetector) Name

func (d *MagicNumberDetector) Name() string

Name returns the detector name

type NestingDepthDetector

type NestingDepthDetector struct{}

NestingDepthDetector detects functions with excessive nesting

func NewNestingDepthDetector

func NewNestingDepthDetector() *NestingDepthDetector

NewNestingDepthDetector creates a new nesting depth detector

func (*NestingDepthDetector) Detect

Detect checks if a function has excessive nesting depth

func (*NestingDepthDetector) Enabled

Enabled returns whether this detector is enabled

func (*NestingDepthDetector) Name

func (d *NestingDepthDetector) Name() string

Name returns the detector name

type ParameterCountDetector

type ParameterCountDetector struct{}

ParameterCountDetector detects functions with too many parameters

func NewParameterCountDetector

func NewParameterCountDetector() *ParameterCountDetector

NewParameterCountDetector creates a new parameter count detector

func (*ParameterCountDetector) Detect

Detect checks if a function has too many parameters

func (*ParameterCountDetector) Enabled

Enabled returns whether this detector is enabled

func (*ParameterCountDetector) Name

func (d *ParameterCountDetector) Name() string

Name returns the detector name

type Registry

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

Registry manages all available detectors

func NewRegistry

func NewRegistry(cfg *config.AnalysisConfig) *Registry

NewRegistry creates a new detector registry with all detectors

func (*Registry) RunAll

func (r *Registry) RunAll(file *parser.FileMetrics, fn *parser.FunctionMetrics, fset *token.FileSet, funcDecl *ast.FuncDecl) []*Issue

RunAll runs all enabled detectors on a function

type ReturnCountDetector

type ReturnCountDetector struct{}

ReturnCountDetector detects functions with too many return statements

func NewReturnCountDetector

func NewReturnCountDetector() *ReturnCountDetector

NewReturnCountDetector creates a new return count detector

func (*ReturnCountDetector) Detect

Detect checks if a function has too many return statements

func (*ReturnCountDetector) Enabled

func (d *ReturnCountDetector) Enabled(cfg *config.AnalysisConfig) bool

Enabled returns whether this detector is enabled

func (*ReturnCountDetector) Name

func (d *ReturnCountDetector) Name() string

Name returns the detector name

Jump to

Keyboard shortcuts

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