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 line: 1,
70 col: 0,
71 cyclomatic,
72 cognitive: 0,
73 line_count: 10,
74 param_count: 0,
75 react_hook_count: 0,
76 react_jsx_max_depth: 0,
77 react_prop_count: 0,
78 source_hash: None,
79 contributions: std::iter::repeat_n(
80 ComplexityContribution {
81 line: 1,
82 col: 0,
83 metric: ComplexityMetric::Cyclomatic,
84 kind: ComplexityContributionKind::If,
85 weight: 1,
86 nesting: 0,
87 },
88 usize::from(branches),
89 )
90 .collect(),
91 }
92 }
93
94 fn module(file_id: u32, units: Vec<FunctionComplexity>) -> ModuleInfo {
95 ModuleInfo {
96 complexity: units,
97 ..ModuleInfo::empty(FileId(file_id))
98 }
99 }
100
101 #[test]
102 fn aggregates_each_module_under_its_path() {
103 let files = vec![file(0, "/p/a.ts"), file(1, "/p/b.ts")];
104 let modules = vec![
105 module(0, vec![unit("one", 3, 2), unit("two", 2, 1)]),
106 module(1, vec![unit("three", 1, 0)]),
107 ];
108
109 let by_file = branching_by_file(&files, &modules);
110
111 let a = by_file[&PathBuf::from("/p/a.ts")];
112 assert_eq!(a.branch_points, 3);
113 assert_eq!(a.functions, 2);
114 assert_eq!(a.peak_cyclomatic, 3);
115 assert_eq!(a.implied_cyclomatic(), 5, "2 functions carrying 3 branches");
116
117 let b = by_file[&PathBuf::from("/p/b.ts")];
118 assert_eq!(b.branch_points, 0);
119 assert_eq!(b.functions, 1);
120 }
121
122 #[test]
123 fn omits_modules_with_no_units() {
124 let files = vec![file(0, "/p/a.ts")];
125 let modules = vec![module(0, Vec::new())];
126 assert!(branching_by_file(&files, &modules).is_empty());
127 }
128
129 #[test]
130 fn omits_modules_whose_only_units_are_synthetic() {
131 let files = vec![file(0, "/p/a.vue")];
132 let modules = vec![module(0, vec![unit("<template>", 4, 3)])];
133 assert!(
134 branching_by_file(&files, &modules).is_empty(),
135 "a synthetic template unit is excluded, so the file has nothing to report"
136 );
137 }
138
139 #[test]
140 fn reports_a_module_whose_only_unit_is_module_scope() {
141 let files = vec![file(0, "/p/config.ts")];
142 let modules = vec![module(0, vec![unit("<module>", 3, 2)])];
143
144 let totals = branching_by_file(&files, &modules)[&PathBuf::from("/p/config.ts")];
145
146 assert_eq!(totals.branch_points, 2, "module-scope branching is counted");
147 assert_eq!(totals.functions, 1);
148 assert!(
149 !totals.has_synthetic_units,
150 "the module unit is not a template unit and excludes nothing"
151 );
152 }
153
154 #[test]
155 fn skips_a_module_whose_file_id_is_out_of_range() {
156 let files = vec![file(0, "/p/a.ts")];
157 let modules = vec![module(7, vec![unit("one", 2, 1)])];
158 assert!(branching_by_file(&files, &modules).is_empty());
159 }
160}