Expand description
Language-independent source code metrics.
srcmetrics parses source code with tree-sitter, converts it
to a small language-independent intermediate representation (ir), and computes metrics
from that representation only (metrics). Every metric is reported separately under its id
(metrics::specs); none are combined into a single readability score. What each metric means
is documented at https://lunainsidious.github.io/srcmetrics/metrics/.
Supported languages: C, C++, Go, Java, JavaScript (JSX), Python, Rust, TypeScript (TSX).
§Analyze source text
let result = srcmetrics::analyze::analyze_source(
"example.py",
"def greet(name):\n if name:\n return 'Hello, ' + name\n return 'Hello'\n",
)?;
let srcmetrics::result::FileResult::Ok { functions, .. } = &result.files[0] else { unreachable!() };
assert_eq!(functions[0].name.as_deref(), Some("greet"));
assert_eq!(functions[0].metrics.metrics["complexity.cyclomatic"], Some(2.0));§Analyze a directory
analyze::analyze walks a directory (respecting .gitignore), analyzes every file with a
supported extension and records run metadata (commit, parser and metric definition versions).
Files that fail to parse are kept in the result with status: "error" instead of being dropped.
let result = srcmetrics::analyze::analyze(std::path::Path::new("src"), None)?;
println!("{}", serde_json::to_string_pretty(&result).unwrap());A metric that cannot be computed (e.g. an average over zero functions) is null in
result::MetricsOutput::metrics, with the reason in result::MetricsOutput::unavailable;
it is never reported as 0.
Modules§
- analyze
- Analysis pipeline: path -> IR -> metrics ->
AnalysisResult(ADR-0010). - csv
- CSV output (ADR-0016): one row per project / file / function.
- error
- Analysis errors (ADR-0005, design principle P7).
- ir
- Common Intermediate Representation (ADR-0003).
- lang
- Language Adapters: source code -> Common IR (ADR-0004).
- metrics
- Metric Engine. Calculators read only the Common IR (design principles P2, P4).
- model
- Experimental readability model: ridge regression fitted on user-provided labels (ADR-0019). There are no built-in weights: a model exists only when trained on labels (design principle P5).
- report
- Self-contained HTML report (ADR-0020): no scripts, no external resources.
- result
- Analysis result: the serialized output format (ADR-0010, design principle P10).
- stats
- Statistical analysis of analysis results (ADR-0018).