cyclomatic

package
v0.0.0-...-39dfbbb Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2023 License: BSD-3-Clause Imports: 11 Imported by: 0

Documentation

Overview

Package cyclomatic calculates the cyclomatic complexities of functions and methods in Go source code.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Complexity

func Complexity(fn ast.Node) int

Complexity calculates the cyclomatic complexity of a function. The 'fn' node is either a *ast.FuncDecl or a *ast.FuncLit.

Types

type Stat

type Stat struct {
	PkgName    string
	FuncName   string
	Complexity int
	Pos        token.Position
}

Stat holds the cyclomatic complexity of a function, along with its package and and function name and its position in the source code.

func (Stat) String

func (s Stat) String() string

String formats the cyclomatic complexity information of a function in the following format: "<complexity> <package> <function> <file:line:column>"

type Stats

type Stats []Stat

Stats hold the cyclomatic complexities of many functions.

func Analyze

func Analyze(paths []string, ignore *regexp.Regexp) Stats

Analyze calculates the cyclomatic complexities of the functions and methods in the Go source code files in the given paths. If a path is a directory all Go files under that directory are analyzed recursively. Files with paths matching the 'ignore' regular expressions are skipped. The 'ignore' parameter can be nil, meaning that no files are skipped.

func AnalyzeASTFile

func AnalyzeASTFile(f *ast.File, fs *token.FileSet, s Stats) Stats

AnalyzeASTFile calculates the cyclomatic complexities of the functions and methods in the abstract syntax tree (AST) of a parsed Go file and appends the results to the given Stats slice.

func (Stats) AverageComplexity

func (s Stats) AverageComplexity() float64

AverageComplexity calculates the average cyclomatic complexity of the cyclomatic complexities in s.

func (Stats) SortAndFilter

func (s Stats) SortAndFilter(top, over int) Stats

SortAndFilter sorts the cyclomatic complexities in s in descending order and returns a slice of s limited to the 'top' N entries with a cyclomatic complexity greater than 'over'. If 'top' is negative, i.e. -1, it does not limit the result. If 'over' is <= 0 it does not limit the result either, because a function has a base cyclomatic complexity of at least 1.

func (Stats) TotalComplexity

func (s Stats) TotalComplexity() uint64

TotalComplexity calculates the total sum of all cyclomatic complexities in s.

Jump to

Keyboard shortcuts

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