fallow_engine/health/
branching.rs1use fallow_types::discover::DiscoveredFile;
10use fallow_types::extract::{FileBranching, ModuleInfo};
11use rustc_hash::FxHashMap;
12use std::path::PathBuf;
13
14pub type BranchingByFile = FxHashMap<PathBuf, FileBranching>;
20
21#[must_use]
31pub fn branching_by_file(files: &[DiscoveredFile], modules: &[ModuleInfo]) -> BranchingByFile {
32 let mut by_file = BranchingByFile::default();
33 for module in modules {
34 if module.complexity.is_empty() {
35 continue;
36 }
37 let Some(file) = files.get(module.file_id.0 as usize) else {
38 continue;
39 };
40 let totals = FileBranching::from_units(&module.complexity);
41 if totals.functions == 0 {
42 continue;
43 }
44 by_file.insert(file.path.clone(), totals);
45 }
46 by_file
47}
48
49#[cfg(test)]
50mod tests {
51 use super::*;
52 use fallow_types::discover::FileId;
53 use fallow_types::extract::{
54 ComplexityContribution, ComplexityContributionKind, ComplexityMetric, FunctionComplexity,
55 };
56
57 fn file(id: u32, path: &str) -> DiscoveredFile {
58 DiscoveredFile {
59 id: FileId(id),
60 path: PathBuf::from(path),
61 size_bytes: 0,
62 }
63 }
64
65 fn unit(name: &str, cyclomatic: u16, branches: u16) -> FunctionComplexity {
66 FunctionComplexity {
67 name: name.to_string(),
68 is_private_member: false,
69 context_named: None,
70 line: 1,
71 col: 0,
72 cyclomatic,
73 cognitive: 0,
74 line_count: 10,
75 param_count: 0,
76 react_hook_count: 0,
77 react_jsx_max_depth: 0,
78 react_prop_count: 0,
79 source_hash: None,
80 contributions: std::iter::repeat_n(
81 ComplexityContribution {
82 line: 1,
83 col: 0,
84 metric: ComplexityMetric::Cyclomatic,
85 kind: ComplexityContributionKind::If,
86 weight: 1,
87 nesting: 0,
88 },
89 usize::from(branches),
90 )
91 .collect(),
92 }
93 }
94
95 fn module(file_id: u32, units: Vec<FunctionComplexity>) -> ModuleInfo {
96 ModuleInfo {
97 complexity: units,
98 ..ModuleInfo::empty(FileId(file_id))
99 }
100 }
101
102 #[test]
103 fn aggregates_each_module_under_its_path() {
104 let files = vec![file(0, "/p/a.ts"), file(1, "/p/b.ts")];
105 let modules = vec![
106 module(0, vec![unit("one", 3, 2), unit("two", 2, 1)]),
107 module(1, vec![unit("three", 1, 0)]),
108 ];
109
110 let by_file = branching_by_file(&files, &modules);
111
112 let a = by_file[&PathBuf::from("/p/a.ts")];
113 assert_eq!(a.branch_points, 3);
114 assert_eq!(a.functions, 2);
115 assert_eq!(a.peak_cyclomatic, 3);
116 assert_eq!(a.implied_cyclomatic(), 5, "2 functions carrying 3 branches");
117
118 let b = by_file[&PathBuf::from("/p/b.ts")];
119 assert_eq!(b.branch_points, 0);
120 assert_eq!(b.functions, 1);
121 }
122
123 #[test]
124 fn omits_modules_with_no_units() {
125 let files = vec![file(0, "/p/a.ts")];
126 let modules = vec![module(0, Vec::new())];
127 assert!(branching_by_file(&files, &modules).is_empty());
128 }
129
130 #[test]
131 fn omits_modules_whose_only_units_are_synthetic() {
132 let files = vec![file(0, "/p/a.vue")];
133 let modules = vec![module(0, vec![unit("<template>", 4, 3)])];
134 assert!(
135 branching_by_file(&files, &modules).is_empty(),
136 "a synthetic template unit is excluded, so the file has nothing to report"
137 );
138 }
139
140 #[test]
141 fn reports_a_module_whose_only_unit_is_module_scope() {
142 let files = vec![file(0, "/p/config.ts")];
143 let modules = vec![module(0, vec![unit("<module>", 3, 2)])];
144
145 let totals = branching_by_file(&files, &modules)[&PathBuf::from("/p/config.ts")];
146
147 assert_eq!(totals.branch_points, 2, "module-scope branching is counted");
148 assert_eq!(totals.functions, 1);
149 assert!(
150 !totals.has_synthetic_units,
151 "the module unit is not a template unit and excludes nothing"
152 );
153 }
154
155 #[test]
156 fn skips_a_module_whose_file_id_is_out_of_range() {
157 let files = vec![file(0, "/p/a.ts")];
158 let modules = vec![module(7, vec![unit("one", 2, 1)])];
159 assert!(branching_by_file(&files, &modules).is_empty());
160 }
161}