Documentation
¶
Overview ¶
Package gocognit defines Analyzer other utilities to checks and calculate the complexity of function based on "cognitive complexity" methods.
Index ¶
- Constants
- Variables
- func Complexity(fn *ast.FuncDecl) int
- func CountDown(start int) iter.Seq[int]
- func DoCount()
- func DoFilter()
- func FilteredMap(m map[string]int, threshold int) iter.Seq2[string, int]
- func ForRangeIter(all iter.Seq[int])
- func PossibleMethodRecursion(files []*ast.File) bool
- func RecursiveFuncs(files []*ast.File, info *types.Info) map[*ast.FuncDecl]bool
- type Diagnostic
- type DiagnosticPosition
- type ScanResult
- type Stat
- func ComplexityStats(f *ast.File, fset *token.FileSet, stats []Stat) []Stat
- func ComplexityStatsForFiles(files []*ast.File, fset *token.FileSet, info *types.Info, stats []Stat) []Stat
- func ComplexityStatsForFilesWithDiagnostic(files []*ast.File, fset *token.FileSet, info *types.Info, stats []Stat, ...) []Stat
- func ComplexityStatsWithDiagnostic(f *ast.File, fset *token.FileSet, stats []Stat, enableDiagnostics bool) []Stat
Constants ¶
const Doc = `` /* 169-byte string literal not displayed */
Variables ¶
var Analyzer = &analysis.Analyzer{ Name: "gocognit", Doc: Doc, Requires: []*analysis.Analyzer{inspect.Analyzer}, Run: run, }
Analyzer reports a diagnostic for every function or method which is too complex specified by its -over flag.
Functions ¶
func Complexity ¶
Complexity calculates the cognitive complexity of a function.
func FilteredMap ¶ added in v1.2.1
func ForRangeIter ¶ added in v1.2.1
func PossibleMethodRecursion ¶ added in v1.3.0
PossibleMethodRecursion reports whether the type-less recursion analysis may have missed a cycle. Without type information a call such as x.M(...) cannot be attributed, so two declarations that each call a same-named method can look unrelated. When those unresolved calls would close a cycle that the resolved analysis did not find, this returns true.
It is a heuristic intended for user guidance (for example, suggesting a type-aware run); it never affects complexity scores.
func RecursiveFuncs ¶ added in v1.3.0
RecursiveFuncs reports which top-level functions and methods in files take part in a recursion cycle, whether direct or indirect.
A function is reported when it lies on a cycle of mutually recursive calls (a strongly connected component with more than one member) or when it calls itself directly. All declarations of the given files are expected to belong to the same package.
When info is non-nil it is used to resolve calls with type information. That covers methods and references across files of the same package. When info is nil a syntactic approximation is used: package-level functions and direct calls through a method's own receiver are recognized, but mutual method recursion may go undetected. Calls into other packages are never resolved.
See also ScanComplexityWithRecursion, which scores a declaration with the result, and ComplexityStatsForFiles, which builds the statistics for a whole package.
Types ¶
type Diagnostic ¶ added in v1.2.0
type Diagnostic struct {
Inc int
Nesting int `json:",omitempty"`
Text string
Pos DiagnosticPosition
}
Diagnostic contains information how the complexity increase.
func (Diagnostic) String ¶ added in v1.2.0
func (d Diagnostic) String() string
type DiagnosticPosition ¶ added in v1.2.0
type DiagnosticPosition struct {
Offset int // offset, starting at 0
Line int // line number, starting at 1
Column int // column number, starting at 1 (byte count)
}
DiagnosticPosition is the position of the diagnostic.
func (DiagnosticPosition) String ¶ added in v1.2.0
func (pos DiagnosticPosition) String() string
type ScanResult ¶ added in v1.2.0
type ScanResult struct {
Diagnostics []diagnostic
Complexity int
}
func ScanComplexity ¶ added in v1.2.0
func ScanComplexity(fn *ast.FuncDecl, includeDiagnostics bool) ScanResult
ScanComplexity scans the function declaration. Recursion is detected for direct self-calls only: a lone declaration carries no information about the surrounding package, so indirect cycles cannot be seen. Prefer a package-aware entry point such as ComplexityStatsForFiles, or combine RecursiveFuncs with ScanComplexityWithRecursion.
func ScanComplexityWithRecursion ¶ added in v1.3.0
func ScanComplexityWithRecursion(fn *ast.FuncDecl, recursive bool, includeDiagnostics bool) ScanResult
ScanComplexityWithRecursion scans the function declaration with recursion information from a package-level analysis (see RecursiveFuncs). When recursive is true, exactly one fundamental increment is added for the recursion cycle, regardless of how many recursive calls the function makes.
type Stat ¶
type Stat struct {
PkgName string
FuncName string
Complexity int
Pos token.Position
Diagnostics []Diagnostic `json:",omitempty"`
}
Stat is statistic of the complexity.
func ComplexityStats ¶
ComplexityStats builds the complexity statistics.
func ComplexityStatsForFiles ¶ added in v1.3.0
func ComplexityStatsForFiles(files []*ast.File, fset *token.FileSet, info *types.Info, stats []Stat) []Stat
ComplexityStatsForFiles builds the complexity statistics for a package made up of the given files. Recursion is detected across the whole set, so indirect recursion is counted. When info is non-nil it is used to resolve calls precisely; otherwise a syntactic approximation is used.
func ComplexityStatsForFilesWithDiagnostic ¶ added in v1.3.0
func ComplexityStatsForFilesWithDiagnostic(files []*ast.File, fset *token.FileSet, info *types.Info, stats []Stat, enableDiagnostics bool) []Stat
ComplexityStatsForFilesWithDiagnostic builds the complexity statistics for a package made up of the given files, optionally with diagnostic output.