parser

package
v0.8.9 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	LabelGoAhead         = "Go Ahead"
	LabelProceedWithCare = "Proceed with Care"
	LabelNeedsWork       = "Needs Work"
	LabelStopRefactor    = "Stop & Refactor"
)

Tiers for code quality score classification.

View Source
const (
	GoAheadThreshold         = 80
	ProceedWithCareThreshold = 60
	NeedsWorkThreshold       = 40
)

Score tier thresholds — single source of truth for the classify() function. Templates reference these so they auto-update when tiers change.

View Source
const (
	VulnLabelClean     = "Clean"
	VulnLabelMonitor   = "Monitor"
	VulnLabelRemediate = "Remediate"
)

Tiers for vulnerability severity classification.

Variables

View Source
var ChainPattern = regexp.MustCompile(`\.\w+\([^)]*\)\.\w+\(`)

ChainPattern matches method chains like a.b().c()

Functions

func Classify

func Classify(score int) string

Classify returns the tier label for a given score.

func DetectIndentSize

func DetectIndentSize(lines []string) int

DetectIndentSize auto-detects whether a file uses spaces or tabs for indentation.

func DetectedLanguage

func DetectedLanguage(ext string) string

DetectedLanguage maps a file extension to a language name.

func TestClassifyHelper

func TestClassifyHelper(score int) string

TestClassifyHelper is exported for testing the classify function.

func TierReferenceTable added in v0.8.9

func TierReferenceTable() string

TierReferenceTable returns a markdown table of score tiers for inclusion in AGENTS.md and agent configuration templates.

func VulnClassify added in v0.8.2

func VulnClassify(findings []struct {
	Severity string
}) string

VulnClassify returns a vulnerability tier based on findings.

Types

type BumpRange

type BumpRange struct {
	StartLine int `json:"start_line"`
	EndLine   int `json:"end_line"`
	MaxDepth  int `json:"max_depth"`
	LineCount int `json:"line_count"`
}

BumpRange represents one "bump" in a nesting sprawl.

type BumpyRoadResult

type BumpyRoadResult struct {
	Bumps       []BumpRange `json:"bumps"`
	IsBumpyRoad bool        `json:"is_bumpy_road"`
	Severity    string      `json:"severity"` // "ok", "warning", "alert", "critical"
	MaxDepth    int         `json:"max_depth"`
}

BumpyRoadResult holds the output of a nesting sprawl analysis on one function.

func DetectBumpyRoadForFunction

func DetectBumpyRoadForFunction(lines []string, bumpDepthThreshold int, bumpsWarning int) BumpyRoadResult

DetectBumpyRoadForFunction analyzes indentation within a function body.

type CohesionResult

type CohesionResult struct {
	TotalFuncs      int
	SharedTypeCount int
	IsolatedFuncs   int
	CohesionScore   float64 // 0-1, higher = more cohesive
	IsLowCohesion   bool
}

CohesionResult describes module-level cohesion.

func AnalyzeCohesion

func AnalyzeCohesion(bloats []FunctionBloat, lines []string) CohesionResult

AnalyzeCohesion estimates module cohesion by checking how many functions share common types in their signatures (parameters, returns). Low cohesion: many isolated functions that don't share types with others.

type ComplexityCount

type ComplexityCount struct {
	Branches int
	Line     int
}

Complexity counts branches (if, for, while, &&, ||) in source lines.

func CountBranches

func CountBranches(lines []string) []ComplexityCount

CountBranches counts control-flow branches in a set of lines (e.g., a function body).

type DuplicationPair

type DuplicationPair struct {
	FuncA      string
	FuncB      string
	LineA      int
	LineB      int
	Similarity float64
	LineCount  int
}

DuplicationPair represents a pair of duplicated code regions.

func DetectDuplications

func DetectDuplications(bloats []FunctionBloat, lines []string, minLines int, minSimilarity float64) []DuplicationPair

DetectDuplications checks for duplicated code across function bodies. Uses normalized fingerprint comparison with configurable minimum similarity.

type FunctionBloat

type FunctionBloat struct {
	Name      string
	LineCount int
	LineStart int
}

FunctionBloat holds per-function length information.

func DetectFunctionBloats

func DetectFunctionBloats(lines []string) []FunctionBloat

DetectFunctionBloats identifies function boundaries for brace-based languages.

func DetectFunctionBloatsCPP

func DetectFunctionBloatsCPP(lines []string) []FunctionBloat

DetectFunctionBloatsCPP detects C/C++ function boundaries.

func DetectFunctionBloatsIndent

func DetectFunctionBloatsIndent(lines []string, indentSize int) []FunctionBloat

DetectFunctionBloatsIndent detects Python functions using indentation. Handles: def, async def, @decorators, class methods, nested functions, tabs/spaces.

func DetectFunctionBloatsJava

func DetectFunctionBloatsJava(lines []string) []FunctionBloat

DetectFunctionBloatsJava detects Java/C# method boundaries.

func DetectFunctionBloatsKotlin

func DetectFunctionBloatsKotlin(lines []string) []FunctionBloat

DetectFunctionBloatsKotlin detects Kotlin fun boundaries.

func DetectFunctionBloatsRuby

func DetectFunctionBloatsRuby(lines []string) []FunctionBloat

DetectFunctionBloatsRuby detects Ruby def/end boundaries.

func DetectFunctionBloatsRust

func DetectFunctionBloatsRust(lines []string) []FunctionBloat

DetectFunctionBloatsRust detects Rust fn boundaries.

func DetectFunctionBloatsSwift

func DetectFunctionBloatsSwift(lines []string) []FunctionBloat

DetectFunctionBloatsSwift detects Swift func boundaries.

func DetectFunctionBloatsTS

func DetectFunctionBloatsTS(lines []string) []FunctionBloat

DetectFunctionBloatsTS detects functions in JavaScript and TypeScript. Handles: function, async function, export function, class methods, arrow => {. Tracks paren-depth to avoid miscounting destructuring {} inside parameter lists.

type GitHotspotResult

type GitHotspotResult struct {
	Entries  []HotspotEntry
	RepoPath string
	Error    string
}

GitHotspotResult holds the full hotspot analysis for a repo.

func AnalyzeGitHotspots

func AnalyzeGitHotspots(repoPath string, maxDepth int) GitHotspotResult

AnalyzeGitHotspots scans the git log to find frequently-changed files. Returns files ranked by composite priority (high commits + low health).

func GetCachedHotspots

func GetCachedHotspots(repoPath string, maxDepth int) GitHotspotResult

GetCachedHotspots returns cached hotspot analysis or runs a new one.

type HotspotEntry

type HotspotEntry struct {
	FilePath     string
	CommitCount  int
	Authors      int
	Lines        int
	QualityScore float64
	Priority     float64 // composite score: commits * (11 - qualityScore)
}

HotspotEntry represents a file with its commit frequency.

type NestingResult

type NestingResult struct {
	MaxDepth    int
	DeepestLine int
}

NestingResult holds the outcome of nesting depth analysis.

func AnalyzeNesting

func AnalyzeNesting(lines []string) NestingResult

AnalyzeNesting scans lines and returns the maximum nesting depth found.

type QualityResult

type QualityResult struct {
	Score       int     `json:"score"` // 0-100
	Label       string  `json:"label"` // one of LabelGoAhead, LabelProceedWithCare, LabelNeedsWork, LabelStopRefactor
	Smells      []Smell `json:"smells"`
	FilePath    string  `json:"file_path"`
	Language    string  `json:"language"`
	LinesOfCode int     `json:"lines_of_code"`
}

QualityResult is the full output of an analysis.

type ScoreTier added in v0.8.9

type ScoreTier struct {
	MinScore int
	MaxScore int
	Label    string
	Guidance string
}

ScoreTier describes one score tier for documentation and templates.

func ScoreTiers added in v0.8.9

func ScoreTiers() []ScoreTier

ScoreTiers returns all quality score tiers in descending order. Templates call this to generate reference tables that always match the actual classify() function.

type Smell

type Smell struct {
	Name      string `json:"name"`     // e.g. "deep_nesting", "brain_method", "bumpy_road"
	Severity  string `json:"severity"` // "warning", "alert", "critical"
	LineStart int    `json:"line_start"`
	LineEnd   int    `json:"line_end"`
	Message   string `json:"message"`   // human-readable description
	AIPrompt  string `json:"ai_prompt"` // injection text for the LLM
}

Smell represents a detected code smell.

func DetectBrainMethodSmell

func DetectBrainMethodSmell(bloats []FunctionBloat, warningThreshold int, alertThreshold int) []Smell

DetectBrainMethodSmell checks function lengths against thresholds.

func DetectBumpyRoadSmell

func DetectBumpyRoadSmell(lines []string, bumpDepth int, bumpsWarning int) *Smell

DetectBumpyRoadSmell wraps the nesting sprawl result into a Smell.

func DetectBumpyRoadSmellAt

func DetectBumpyRoadSmellAt(lines []string, bumpDepth int, bumpsWarning int, funcStartLine int) *Smell

func DetectComplexConditional

func DetectComplexConditional(lines []string, warningThreshold int, alertThreshold int) []Smell

DetectComplexConditional checks for if/while conditions with many boolean operators.

func DetectDuplicationSmells

func DetectDuplicationSmells(pairs []DuplicationPair) []Smell

DetectDuplicationSmells converts duplication pairs into Smell entries.

func DetectExcessiveComments

func DetectExcessiveComments(lines []string, ratioThreshold float64) *Smell

DetectExcessiveComments flag files where comment lines exceed a ratio of total lines.

func DetectFileBloat

func DetectFileBloat(totalLoc int, warningThreshold int, alertThreshold int, criticalThreshold int) *Smell

FileBloatSmell returns a smell if the file exceeds line thresholds.

func DetectGlobalData

func DetectGlobalData(lines []string, minGlobals int) *Smell

DetectGlobalData flags file-level mutable variable declarations outside functions.

func DetectHotspotSmell

func DetectHotspotSmell(entry HotspotEntry) *Smell

DetectHotspotSmell identifies files that are both frequently changed AND unhealthy.

func DetectLazyElements

func DetectLazyElements(bloats []FunctionBloat, minLines int) []Smell

func DetectLongParameterList

func DetectLongParameterList(lines []string, warningThreshold int, alertThreshold int) []Smell

DetectLongParameterList checks function signatures for too many parameters.

func DetectLongScopeVariables

func DetectLongScopeVariables(lines []string, bloats []FunctionBloat, minLineGap int) []Smell

DetectLongScopeVariables flags variables declared far from their last usage.

func DetectLongSwitch

func DetectLongSwitch(lines []string, warnThreshold int, alertThreshold int) []Smell

func DetectLowCohesionSmell

func DetectLowCohesionSmell(result CohesionResult, warningThreshold int, badThreshold int) *Smell

DetectLowCohesionSmell converts cohesion analysis to a Smell.

func DetectMessageChains

func DetectMessageChains(lines []string) []Smell

func DetectNestingSmell

func DetectNestingSmell(lines []string, warningThreshold int, alertThreshold int) *Smell

DetectNestingSmell checks whether nesting exceeds a threshold and returns a smell if so.

func DetectParagraphOfCode

func DetectParagraphOfCode(lines []string, maxConsecutive int) []Smell

func DetectPrimitiveObsession

func DetectPrimitiveObsession(lines []string) []Smell

type Thresholds

type Thresholds struct {
	NestingWarning           int
	NestingAlert             int
	FileLOCWarning           int
	FileLOCAlert             int
	FileLOCCritical          int
	FuncLOCWarning           int
	FuncLOCAlert             int
	FuncCCWarning            int
	FuncCCAlert              int
	FuncCountWarning         int
	FuncCountAlert           int
	BumpyRoadBumpsWarning    int
	BumpyRoadNestingDepth    int
	ComplexCondBranchesWarn  int
	ComplexCondBranchesAlert int
	MaxArgumentsWarn         int
	MaxArgumentsAlert        int
	BrainClassMinFunc        int
	ParagraphMaxConsecutive  int
	LazyMinLines             int
	DupMinLines              int
	DupMinSimilarity         float64
	CohesionIsolationPct     float64
	CommentRatioWarning      float64
	GlobalDataWarning        int
	LongScopeVarLines        int
	LongSwitchWarn           int
	LongSwitchAlert          int
}

Thresholds holds the configurable thresholds for all detectors.

func DefaultThresholds

func DefaultThresholds(lang string) Thresholds

DefaultThresholds returns per-language default thresholds.

Jump to

Keyboard shortcuts

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