analysis

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package analysis provides language-agnostic code analysis through a unified interface. Each language implements the Analyzer interface, producing consistent structural metrics that feed into the certification scoring pipeline.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Languages

func Languages() []string

Languages returns all registered language identifiers.

func Register

func Register(lang string, factory func() Analyzer)

Register adds an analyzer for a language.

Types

type Analyzer

type Analyzer interface {
	// Language returns the language identifier (e.g., "go", "ts", "py").
	Language() string

	// Discover finds all symbols in a source file.
	Discover(path string, src []byte) ([]Symbol, error)

	// Analyze returns structural metrics for a specific symbol.
	Analyze(path string, src []byte, symbol string) (Metrics, error)

	// AnalyzeFile returns file-level metrics.
	AnalyzeFile(path string, src []byte) (FileMetrics, error)
}

Analyzer provides language-agnostic structural analysis.

func ForLanguage

func ForLanguage(lang string) Analyzer

ForLanguage returns an Analyzer for the given language, or nil if unsupported.

type FileMetrics

type FileMetrics struct {
	HasInitFunc        bool
	GlobalMutableCount int
}

FileMetrics holds file-level analysis results.

type GoAnalyzer

type GoAnalyzer struct{}

GoAnalyzer implements Analyzer for Go source code using go/ast.

func NewGoAnalyzer

func NewGoAnalyzer() *GoAnalyzer

NewGoAnalyzer creates a new Go analyzer.

func (*GoAnalyzer) Analyze

func (a *GoAnalyzer) Analyze(path string, src []byte, symbol string) (Metrics, error)

Analyze returns structural metrics for a specific symbol.

func (*GoAnalyzer) AnalyzeFile

func (a *GoAnalyzer) AnalyzeFile(path string, src []byte) (FileMetrics, error)

AnalyzeFile returns file-level metrics.

func (*GoAnalyzer) Discover

func (a *GoAnalyzer) Discover(path string, src []byte) ([]Symbol, error)

Discover finds all functions, methods, and types in Go source.

func (*GoAnalyzer) Language

func (a *GoAnalyzer) Language() string

type Metrics

type Metrics struct {
	// Shape
	ParamCount      int // Number of function parameters
	ReturnCount     int // Number of return values
	FuncLines       int // Lines in function body
	MaxNestingDepth int // Deepest nesting level (if/for/switch/etc.)

	// Documentation
	HasDocComment bool   // Has a doc comment
	IsExported    bool   // Exported / public symbol
	ReceiverName  string // Receiver type name (empty for standalone)
	IsConstructor bool   // Matches constructor pattern (New*, __init__, constructor)

	// Complexity
	CyclomaticComplexity int    // McCabe cyclomatic complexity
	CognitiveComplexity  int    // Sonar-style cognitive complexity
	LoopNestingDepth     int    // Max nested loop depth (for/while only)
	RecursiveCalls       int    // Direct recursive calls
	AlgoComplexity       string // Estimated Big-O: O(1), O(n), O(n²), etc.

	// Error handling
	ErrorsIgnored    int // Discarded error returns
	ErrorsNotWrapped int // Errors returned without wrapping/context
	NakedReturns     int // Bare return in named-return function
	PanicCalls       int // panic(), throw, unwrap() — unrecoverable exits
	EmptyCatchBlocks int // catch/except/recover with empty body

	// Security
	UnsafeImports    []string // Dangerous imports (os/exec, unsafe, eval, etc.)
	HardcodedSecrets int      // String literals matching secret patterns

	// Design
	MethodCount     int  // Methods on a type (type-level only)
	DeferInLoop     int  // Defer/finally inside loops
	ContextNotFirst bool // context.Context not first param (Go-specific)

	// Performance
	NestedLoopPairs   int // Inner loops nested in outer loops
	QuadraticPatterns int // Known O(n²) anti-patterns

	// File-level
	HasInitFunc        bool // File contains init() or equivalent
	GlobalMutableCount int  // Package-level mutable variables
	OsExitCalls        int  // os.Exit() or sys.exit() calls

	// Language-specific extras
	Extra map[string]float64
}

Metrics holds language-agnostic structural analysis results for a code unit.

func (Metrics) ToEvidence

func (m Metrics) ToEvidence() domain.Evidence

ToEvidence converts Metrics to a domain.Evidence for the scoring pipeline.

type PythonAnalyzer

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

PythonAnalyzer implements Analyzer for Python using tree-sitter.

func NewPythonAnalyzer

func NewPythonAnalyzer() *PythonAnalyzer

NewPythonAnalyzer creates a new Python analyzer.

func (*PythonAnalyzer) Analyze

func (a *PythonAnalyzer) Analyze(path string, src []byte, symbol string) (Metrics, error)

Analyze returns structural metrics for a symbol.

func (*PythonAnalyzer) AnalyzeFile

func (a *PythonAnalyzer) AnalyzeFile(path string, src []byte) (FileMetrics, error)

AnalyzeFile returns file-level metrics.

func (*PythonAnalyzer) Discover

func (a *PythonAnalyzer) Discover(path string, src []byte) ([]Symbol, error)

Discover finds all symbols in Python source.

func (*PythonAnalyzer) Language

func (a *PythonAnalyzer) Language() string

type RustAnalyzer

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

RustAnalyzer implements Analyzer for Rust using tree-sitter.

func NewRustAnalyzer

func NewRustAnalyzer() *RustAnalyzer

NewRustAnalyzer creates a new Rust analyzer.

func (*RustAnalyzer) Analyze

func (a *RustAnalyzer) Analyze(path string, src []byte, symbol string) (Metrics, error)

Analyze returns structural metrics for a symbol.

func (*RustAnalyzer) AnalyzeFile

func (a *RustAnalyzer) AnalyzeFile(path string, src []byte) (FileMetrics, error)

AnalyzeFile returns file-level metrics.

func (*RustAnalyzer) Discover

func (a *RustAnalyzer) Discover(path string, src []byte) ([]Symbol, error)

Discover finds all symbols in Rust source.

func (*RustAnalyzer) Language

func (a *RustAnalyzer) Language() string

type Symbol

type Symbol struct {
	Name      string     // Symbol name
	Kind      SymbolKind // Function, method, class, etc.
	StartLine int        // 1-indexed start line
	EndLine   int        // 1-indexed end line (inclusive)
	Parent    string     // Enclosing type/class name (empty for top-level)
	Exported  bool       // Whether the symbol is exported/public
}

Symbol represents a discovered code unit within a source file.

type SymbolKind

type SymbolKind int

SymbolKind identifies the type of a discovered code symbol.

const (
	SymbolFunction  SymbolKind = iota // Standalone function
	SymbolMethod                      // Method on a type/class
	SymbolClass                       // Type, class, struct, interface
	SymbolConstant                    // Exported constant or variable
	SymbolInterface                   // Interface (separate from class for languages that distinguish)
)

func (SymbolKind) String

func (k SymbolKind) String() string

type TSAnalyzer

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

TSAnalyzer implements Analyzer for TypeScript using tree-sitter.

func NewTSAnalyzer

func NewTSAnalyzer() *TSAnalyzer

NewTSAnalyzer creates a new TypeScript analyzer.

func (*TSAnalyzer) Analyze

func (a *TSAnalyzer) Analyze(path string, src []byte, symbol string) (Metrics, error)

Analyze returns structural metrics for a specific symbol.

func (*TSAnalyzer) AnalyzeFile

func (a *TSAnalyzer) AnalyzeFile(path string, src []byte) (FileMetrics, error)

AnalyzeFile returns file-level metrics.

func (*TSAnalyzer) Discover

func (a *TSAnalyzer) Discover(path string, src []byte) ([]Symbol, error)

Discover finds all symbols in a TypeScript source file.

func (*TSAnalyzer) Language

func (a *TSAnalyzer) Language() string

Jump to

Keyboard shortcuts

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