Documentation
¶
Overview ¶
Package ceplx calculates cyclomatic and cognitive complexities of CEL expressions.
CEL is a purely expression-based language — there are no statements or function declarations. The unit of analysis is a single expression.
In the CEL AST all operators are function calls with well-known names (e.g. "_&&_", "_?_:_") and macros like .map, .filter, .exists desugar into ComprehensionKind nodes. This package uses SourceInfo.MacroCalls to walk user-visible source structure rather than synthetic macro expansions.
For accurate results, the AST should be parsed with macro call tracking enabled (cel.EnableMacroCallTracking or parser.PopulateMacroCalls(true)). Without it, synthetic nodes inside macro expansions may inflate scores.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Cognitive ¶
Cognitive returns the cognitive complexity of a CEL expression.
The rules follow the gocognit model adapted for CEL:
- Ternary (_?_:_): nesting+1 increment; both branches increase nesting
- Chained ternary in else position: flat +1 (else-if discount)
- Comprehension: nesting+1 increment; loop body increases nesting
- Logical operators: +1 per sequence of identical operators; switching between && and || adds another +1
func Cyclomatic ¶
Cyclomatic returns the cyclomatic complexity of a CEL expression.
The base complexity is 1. Each of the following adds 1:
- ternary conditional (_?_:_)
- logical && or ||
- comprehension (macro-expanded .map, .filter, .exists, .all, etc.)
func Parse ¶
Parse parses a CEL expression with macro call tracking enabled, returning an AST suitable for complexity analysis.
Standard CEL macros (has, all, exists, exists_one, map, filter) are registered by default. Additional parser options can be passed to register custom macros or enable additional syntax features.
Types ¶
type Diagnostic ¶
type Diagnostic struct {
ExprID int64 `json:"expr_id"`
Inc int `json:"inc"`
Nesting int `json:"nesting"`
Text string `json:"text"`
}
Diagnostic records a single complexity increment with its cause.
func CognitiveWithDiagnostics ¶
func CognitiveWithDiagnostics(a *ast.AST) (int, []Diagnostic)
CognitiveWithDiagnostics is like Cognitive but also returns per-node diagnostic records explaining each increment.