Skip to main content

cccc_core/
lib.rs

1//! Language-agnostic Cognitive (SonarSource) and Cyclomatic (McCabe) complexity.
2//!
3//! This crate computes complexity over a normalized intermediate representation
4//! ([`ir::Node`]) instead of any particular language's AST. A language adapter
5//! (such as `cccc-typescript`) lowers its parse tree into `Vec<ir::Node>`, and
6//! [`engine::analyze`] scores it. All scoring rules live here, so adding a
7//! language means writing an adapter — not reimplementing the metrics.
8//!
9//! ```
10//! use cccc_core::ir::Node;
11//! use cccc_core::engine::analyze;
12//!
13//! // `if (cond) {}` inside `fn f` — one cognitive point, cyclomatic base 1 + 1.
14//! let f = Node::Function {
15//!     name: "f".into(),
16//!     kind: "function".into(),
17//!     line: 1,
18//!     body: vec![Node::Branch { test: vec![], then: vec![], alternate: None }],
19//! };
20//! let report = analyze("example.ts", &[f], vec![]);
21//! assert_eq!(report.functions[0].cognitive, 1);
22//! assert_eq!(report.functions[0].cyclomatic, 2);
23//! ```
24
25pub mod engine;
26pub mod ir;
27pub mod report;