analyzer

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 10, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package analyzer defines the core types and interfaces for krait's static analysis framework.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MatchesIgnoreExport

func MatchesIgnoreExport(name string, patterns []string) bool

MatchesIgnoreExport checks if a symbol name matches any ignore export pattern.

Types

type Analyzer

type Analyzer interface {
	Name() string
	Description() string
	Analyze(project *Project, cfg *Config) (*Result, error)
}

Analyzer is the interface all analyzers implement.

type Category

type Category string

Category represents the category of analysis.

const (
	CategoryDeadCode     Category = "dead-code"
	CategoryDuplication  Category = "duplication"
	CategoryComplexity   Category = "complexity"
	CategoryArchitecture Category = "architecture"
	CategoryDependency   Category = "dependency"
	CategoryCircular     Category = "circular"
	CategoryHealth       Category = "health"
	CategorySuppression  Category = "suppression"
)

Category constants define analysis categories.

type Config

type Config struct {
	IgnorePatterns      []string            `json:"ignore_patterns"`
	IncludeTests        bool                `json:"include_tests"`
	Rules               map[string]Severity `json:"rules"`
	CyclomaticThreshold int                 `json:"cyclomatic_threshold"`
	CognitiveThreshold  int                 `json:"cognitive_threshold"`
	MinDuplicateLines   int                 `json:"min_duplicate_lines"`
	MinDuplicateTokens  int                 `json:"min_duplicate_tokens"`
	ArchitectureLayers  []LayerConfig       `json:"architecture_layers"`
	IgnoreExports       []string            `json:"ignore_exports"`
	GodPackageThreshold int                 `json:"god_package_threshold"`
	ChurnPeriod         string              `json:"churn_period"`
	MinHealthScore      int                 `json:"min_health_score"`
	HealthWeights       *HealthWeights      `json:"health_weights"`
}

Config holds all configuration for analyzer execution.

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns a Config with sensible defaults.

type Finding

type Finding struct {
	Rule             string         `json:"rule"`
	Category         Category       `json:"category"`
	Severity         Severity       `json:"severity"`
	Message          string         `json:"message"`
	Location         Location       `json:"location"`
	RelatedLocations []Location     `json:"related_locations,omitempty"`
	Meta             map[string]any `json:"meta,omitempty"`
}

Finding represents a single issue found by an analyzer.

type GoModDep

type GoModDep struct {
	Path     string
	Version  string
	Indirect bool
	Line     int
}

GoModDep represents a dependency line from go.mod.

type HealthWeights added in v0.2.0

type HealthWeights struct {
	DeadCode     int `json:"dead_code"`
	Duplication  int `json:"duplication"`
	Complexity   int `json:"complexity"`
	Architecture int `json:"architecture"`
	Dependencies int `json:"dependencies"`
}

HealthWeights configures the weight of each signal in the health score.

func DefaultHealthWeights added in v0.2.0

func DefaultHealthWeights() *HealthWeights

DefaultHealthWeights returns the default health score weights.

type LayerConfig

type LayerConfig struct {
	Name      string   `json:"name"`
	Packages  []string `json:"packages"`
	CanImport []string `json:"can_import"`
}

LayerConfig defines an architecture layer for violation checking.

type Location

type Location struct {
	File    string `json:"file"`
	Line    int    `json:"line"`
	Column  int    `json:"column,omitempty"`
	EndLine int    `json:"end_line,omitempty"`
}

Location identifies a position in source code.

type PackageInfo

type PackageInfo struct {
	ImportPath string
	RelPath    string
	Name       string
	Dir        string
	Files      []*ast.File
	FilePaths  []string
}

PackageInfo groups files belonging to the same Go package.

type PostAnalyzer added in v0.2.0

type PostAnalyzer interface {
	Name() string
	Description() string
	PostAnalyze(project *Project, cfg *Config, results []*Result) (*Result, error)
}

PostAnalyzer processes results from other analyzers. Unlike Analyzer, it receives the results of all prior analyzer runs.

type Project

type Project struct {
	RootDir    string
	ModulePath string
	Fset       *token.FileSet
	Packages   map[string]*PackageInfo
	Files      map[string]*ast.File
	GoModDeps  []GoModDep
	// Warnings holds non-fatal messages from parsing (e.g. individual files that
	// failed to parse). Callers may surface these to the user.
	Warnings []string
}

Project holds all parsed data for the Go module being analyzed.

type Report

type Report struct {
	Version       string        `json:"version"`
	Timestamp     string        `json:"timestamp"`
	RootDir       string        `json:"root_dir"`
	TotalDuration string        `json:"total_duration"`
	Summary       ReportSummary `json:"summary"`
	Results       []*Result     `json:"results"`
}

Report is the aggregate output of all analyzers.

type ReportSummary

type ReportSummary struct {
	TotalFindings int              `json:"total_findings"`
	BySeverity    map[Severity]int `json:"by_severity"`
	ByCategory    map[Category]int `json:"by_category"`
}

ReportSummary holds aggregate counts for a report.

type Result

type Result struct {
	Analyzer   string         `json:"analyzer"`
	Duration   time.Duration  `json:"-"`
	DurationMs int64          `json:"duration_ms"`
	Findings   []*Finding     `json:"findings"`
	Stats      map[string]any `json:"stats,omitempty"`
}

Result is the output of a single analyzer run.

type Severity

type Severity string

Severity represents the severity level of a finding.

const (
	SeverityError   Severity = "error"
	SeverityWarning Severity = "warning"
	SeverityInfo    Severity = "info"
	SeverityOff     Severity = "off"
)

Severity constants define finding severity levels.

func ResolveSeverity

func ResolveSeverity(rule string, defaultSev Severity, cfg *Config) (Severity, bool)

ResolveSeverity applies the config's severity override for a rule. Returns the resolved severity and whether the finding should be included.

Jump to

Keyboard shortcuts

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