Documentation
¶
Overview ¶
Package codemetrics computes per-function complexity metrics from source code: McCabe cyclomatic complexity and SonarSource cognitive complexity.
Cyclomatic complexity counts decision points flatly (1 + one per branch: if / for / range / case / comm-clause / && / ||). Cognitive complexity weights *nested* control flow more heavily, so deeply-nested logic scores higher than a flat sequence of the same number of branches — it tracks how hard code is to understand rather than how many paths it has.
The Go analyzer is built on the standard library's go/ast and has no external dependencies. The cognitive-complexity algorithm follows the SonarSource specification (reference implementation: github.com/uudashr/gocognit).
The public API is shaped so additional languages can be added behind Parse without breaking callers; today only Go is supported and FunctionMetrics.Cognitive is always populated. For languages that do not (yet) support cognitive complexity, Cognitive is nil — distinct from a genuine zero.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrUnsupportedLanguage = errors.New("codemetrics: unsupported language")
ErrUnsupportedLanguage is returned by Parse for a language with no analyzer. Test for it with errors.Is.
Functions ¶
func Cognitive ¶ added in v0.2.0
Cognitive returns the SonarSource cognitive complexity of a Go function declaration. Use this when you already hold a parsed AST; ParseGo derives the same value from source. A nil declaration (or one without a name) returns 0.
See the package documentation for the increment rules.
func Cyclomatic ¶ added in v0.2.0
Cyclomatic returns the McCabe cyclomatic complexity of a function body — 1 + one per branch point (if / for / range / case / comm-clause / && / ||).
Use this when you already hold a parsed AST (e.g. from your own go/parser pass); ParseGo derives the same value from source. A nil body returns 0.
func SupportedLanguages ¶
func SupportedLanguages() []string
SupportedLanguages returns the language identifiers accepted by Parse.
Types ¶
type FunctionMetrics ¶
type FunctionMetrics struct {
// Name is the bare function or method name (e.g. "Write"), without the
// receiver. For methods, Receiver carries the receiver type.
Name string
// Receiver is the receiver type of a method (e.g. "*Buffer" or "Buffer"),
// or "" for a plain function.
Receiver string
// Cyclomatic is the McCabe cyclomatic complexity (1 + branch points).
Cyclomatic int
// Cognitive is the SonarSource cognitive complexity, or nil when the
// language's analyzer does not compute it. For Go it is always set.
Cognitive *int
// StartLine and EndLine are the 1-based inclusive line span of the
// function declaration.
StartLine int
EndLine int
}
FunctionMetrics holds the complexity metrics for a single function or method, together with its 1-based inclusive line span.
func Parse ¶
func Parse(language string, src []byte) ([]FunctionMetrics, error)
Parse computes complexity metrics for every function in src, dispatching on language. Recognised identifiers: "go" (alias "golang"). Unknown languages return a wrapped ErrUnsupportedLanguage.
Parsing is best-effort: input that still yields a partial syntax tree is tolerated and metrics are computed for every recovered function. A hard parse failure returns a non-nil error and no metrics.
func ParseGo ¶
func ParseGo(src []byte) ([]FunctionMetrics, error)
ParseGo computes cyclomatic and cognitive complexity for every function and method declaration with a body in a Go source file. Functions without a body (e.g. external or interface declarations) are skipped.
Parsing is best-effort: if the parser recovers a partial syntax tree from malformed input, metrics are still computed for every function it found and the returned error is nil. A total parse failure (no tree) returns the parse error and no metrics.
func (FunctionMetrics) Lines ¶
func (m FunctionMetrics) Lines() int
Lines returns the number of source lines the function spans (inclusive).
func (FunctionMetrics) QualifiedName ¶
func (m FunctionMetrics) QualifiedName() string
QualifiedName returns the receiver-qualified name for a method (e.g. "(*Buffer).Write") or the bare name for a plain function.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
codemetrics
command
Command codemetrics reports per-function cyclomatic and cognitive complexity for Go source files.
|
Command codemetrics reports per-function cyclomatic and cognitive complexity for Go source files. |
|
Package treesitter computes cyclomatic and cognitive complexity for the non-Go languages that go-codemetrics supports, using the pure-Go tree-sitter runtime github.com/odvcencio/gotreesitter and its bundled grammars.
|
Package treesitter computes cyclomatic and cognitive complexity for the non-Go languages that go-codemetrics supports, using the pure-Go tree-sitter runtime github.com/odvcencio/gotreesitter and its bundled grammars. |