1use fallow_output::{DirectCallerEvidence, DirectCallerSymbolEvidence, FileHealthScore};
2
3use super::coverage_gaps::compute_coverage_gaps;
4pub(super) use super::coverage_gaps::{CoverageGapData, build_coverage_summary};
5
6pub struct FileScoreOutput {
8 pub scores: Vec<FileHealthScore>,
9 pub coverage: CoverageGapData,
11 pub circular_files: rustc_hash::FxHashSet<std::path::PathBuf>,
13 pub top_complex_fns: rustc_hash::FxHashMap<std::path::PathBuf, Vec<(String, u32, u16)>>,
15 pub entry_points: rustc_hash::FxHashSet<std::path::PathBuf>,
17 pub value_export_counts: rustc_hash::FxHashMap<std::path::PathBuf, usize>,
19 pub unused_export_names: rustc_hash::FxHashMap<std::path::PathBuf, Vec<String>>,
21 pub cycle_members: rustc_hash::FxHashMap<std::path::PathBuf, Vec<std::path::PathBuf>>,
23 pub direct_callers: rustc_hash::FxHashMap<std::path::PathBuf, Vec<DirectCallerEvidence>>,
25 pub analysis_counts: crate::vital_signs::AnalysisCounts,
27 pub prop_drilling_chains: Vec<fallow_types::output_dead_code::PropDrillingChainFinding>,
32 pub render_fan_in: Option<fallow_types::results::RenderFanInMetric>,
38 pub analysis_snapshot: AnalysisCountsSnapshot,
42 pub istanbul_matched: usize,
44 pub istanbul_total: usize,
45 pub per_function_crap: rustc_hash::FxHashMap<std::path::PathBuf, Vec<PerFunctionCrap>>,
49 pub template_inherit_provenance: rustc_hash::FxHashMap<std::path::PathBuf, std::path::PathBuf>,
56}
57
58struct FileScoreOutputParts<'a> {
59 graph: &'a fallow_graph::graph::ModuleGraph,
60 file_paths: &'a rustc_hash::FxHashMap<crate::discover::FileId, &'a std::path::PathBuf>,
61 results: &'a crate::results::AnalysisResults,
62 scores: Vec<FileHealthScore>,
63 coverage: CoverageGapData,
64 circular_files: rustc_hash::FxHashSet<std::path::PathBuf>,
65 top_complex_fns: rustc_hash::FxHashMap<std::path::PathBuf, Vec<(String, u32, u16)>>,
66 entry_points: rustc_hash::FxHashSet<std::path::PathBuf>,
67 value_export_counts: rustc_hash::FxHashMap<std::path::PathBuf, usize>,
68 unused_export_names: rustc_hash::FxHashMap<std::path::PathBuf, Vec<String>>,
69 cycle_members: rustc_hash::FxHashMap<std::path::PathBuf, Vec<std::path::PathBuf>>,
70 direct_callers: rustc_hash::FxHashMap<std::path::PathBuf, Vec<DirectCallerEvidence>>,
71 istanbul_matched: usize,
72 istanbul_total: usize,
73 per_function_crap: rustc_hash::FxHashMap<std::path::PathBuf, Vec<PerFunctionCrap>>,
74 template_inherit: rustc_hash::FxHashMap<crate::discover::FileId, TemplateInheritContext>,
75}
76
77#[derive(Clone, Default)]
83pub struct AnalysisCountsSnapshot {
84 pub unused_file_paths: Vec<std::path::PathBuf>,
86 pub unused_export_paths: Vec<std::path::PathBuf>,
89 pub unused_dep_package_paths: Vec<std::path::PathBuf>,
93 pub circular_dep_groups: Vec<Vec<std::path::PathBuf>>,
96 pub module_export_counts: rustc_hash::FxHashMap<std::path::PathBuf, usize>,
99}
100
101impl AnalysisCountsSnapshot {
102 pub fn counts_for(
118 &self,
119 subset: &crate::health::SubsetFilter<'_>,
120 defaults: &crate::vital_signs::AnalysisCounts,
121 ) -> crate::vital_signs::AnalysisCounts {
122 if subset.is_full() {
123 return *defaults;
124 }
125 let dead_files = self
126 .unused_file_paths
127 .iter()
128 .filter(|p| subset.matches(p))
129 .count();
130 let dead_exports = self
131 .unused_export_paths
132 .iter()
133 .filter(|p| subset.matches(p))
134 .count();
135 let unused_deps = self
136 .unused_dep_package_paths
137 .iter()
138 .filter(|dep_path| dep_in_subset(subset, dep_path))
139 .count();
140 let circular_deps = self
141 .circular_dep_groups
142 .iter()
143 .filter(|cycle| cycle.iter().any(|p| subset.matches(p)))
144 .count();
145 let total_exports = self
146 .module_export_counts
147 .iter()
148 .filter(|(p, _)| subset.matches(p))
149 .map(|(_, n)| *n)
150 .sum();
151 crate::vital_signs::AnalysisCounts {
152 total_exports,
153 dead_files,
154 dead_exports,
155 unused_deps,
156 circular_deps,
157 total_deps: defaults.total_deps,
158 }
159 }
160}
161
162fn dep_in_subset(subset: &crate::health::SubsetFilter<'_>, dep_path: &std::path::Path) -> bool {
169 match subset {
170 crate::health::SubsetFilter::Full => true,
171 crate::health::SubsetFilter::Paths(set) => {
172 let Some(workspace_root) = dep_path.parent() else {
173 return false;
174 };
175 set.iter().any(|p| p.starts_with(workspace_root))
176 }
177 }
178}
179
180#[expect(
184 clippy::cast_possible_truncation,
185 reason = "line count is bounded by source file size"
186)]
187pub(super) fn aggregate_complexity(module: &crate::source::ModuleInfo) -> (u32, u32, usize, u32) {
188 let cyc: u32 = module
189 .complexity
190 .iter()
191 .map(|f| u32::from(f.cyclomatic))
192 .sum();
193 let cog: u32 = module
194 .complexity
195 .iter()
196 .map(|f| u32::from(f.cognitive))
197 .sum();
198 let funcs = module.complexity.len();
199 let lines = module.line_offsets.len() as u32;
200 (cyc, cog, funcs, lines)
201}
202
203pub(super) fn compute_dead_code_ratio(
211 path: &std::path::Path,
212 exports: &[fallow_graph::graph::ExportSymbol],
213 unused_files: &rustc_hash::FxHashSet<&std::path::Path>,
214 unused_exports_by_path: &rustc_hash::FxHashMap<&std::path::Path, usize>,
215) -> f64 {
216 if unused_files.contains(path) {
217 return 1.0;
218 }
219 let value_exports = exports.iter().filter(|e| !e.is_type_only).count();
220 if value_exports == 0 {
221 return 0.0;
222 }
223 let unused = unused_exports_by_path.get(path).copied().unwrap_or(0);
224 (unused as f64 / value_exports as f64).min(1.0)
225}
226
227pub(super) fn compute_complexity_density(total_cyclomatic: u32, lines: u32) -> f64 {
231 if lines > 0 {
232 f64::from(total_cyclomatic) / f64::from(lines)
233 } else {
234 0.0
235 }
236}
237
238pub(super) const CRAP_THRESHOLD: f64 = 30.0;
241
242#[cfg(test)]
250#[expect(
251 clippy::suboptimal_flops,
252 reason = "cc * cc + cc matches the CRAP formula specification"
253)]
254fn compute_crap_scores_binary(
255 complexity: &[fallow_types::extract::FunctionComplexity],
256 is_test_reachable: bool,
257) -> (f64, usize) {
258 if complexity.is_empty() {
259 return (0.0, 0);
260 }
261 let mut max = 0.0_f64;
262 let mut above = 0usize;
263 for f in complexity {
264 let cc = f64::from(f.cyclomatic);
265 let crap = if is_test_reachable { cc } else { cc * cc + cc };
266 max = max.max(crap);
267 if crap >= CRAP_THRESHOLD {
268 above += 1;
269 }
270 }
271 ((max * 10.0).round() / 10.0, above)
272}
273
274#[derive(Debug, Clone, Copy)]
276pub struct PerFunctionCrap {
277 pub line: u32,
279 pub col: u32,
285 pub crap: f64,
287 pub coverage_pct: Option<f64>,
290 pub coverage_tier: fallow_output::CoverageTier,
294 pub coverage_source: fallow_output::CoverageSource,
301}
302
303pub(super) struct IstanbulCrapResult {
305 pub max_crap: f64,
306 pub above_threshold: usize,
307 pub matched: usize,
309 pub total: usize,
311 pub per_function: Vec<PerFunctionCrap>,
313}
314
315fn compute_crap_scores_istanbul(
326 complexity: &[fallow_types::extract::FunctionComplexity],
327 file_coverage: Option<&IstanbulFileCoverage>,
328 is_test_reachable: bool,
329) -> IstanbulCrapResult {
330 if complexity.is_empty() {
331 return IstanbulCrapResult {
332 max_crap: 0.0,
333 above_threshold: 0,
334 matched: 0,
335 total: 0,
336 per_function: Vec::new(),
337 };
338 }
339 let mut max = 0.0_f64;
340 let mut above = 0usize;
341 let mut matched = 0usize;
342 let mut per_function = Vec::with_capacity(complexity.len());
343 for f in complexity {
344 let (crap, coverage_pct, tier, source) =
345 crap_for_function(f, file_coverage, is_test_reachable, &mut matched);
346 let crap_rounded = (crap * 10.0).round() / 10.0;
347 max = max.max(crap);
348 if crap >= CRAP_THRESHOLD {
349 above += 1;
350 }
351 per_function.push(PerFunctionCrap {
352 line: f.line,
353 col: f.col,
354 crap: crap_rounded,
355 coverage_pct,
356 coverage_tier: tier,
357 coverage_source: source,
358 });
359 }
360 IstanbulCrapResult {
361 max_crap: (max * 10.0).round() / 10.0,
362 above_threshold: above,
363 matched,
364 total: complexity.len(),
365 per_function,
366 }
367}
368
369#[expect(
373 clippy::suboptimal_flops,
374 reason = "cc * cc + cc matches the CRAP formula specification"
375)]
376fn crap_for_function(
377 f: &fallow_types::extract::FunctionComplexity,
378 file_coverage: Option<&IstanbulFileCoverage>,
379 is_test_reachable: bool,
380 matched: &mut usize,
381) -> (
382 f64,
383 Option<f64>,
384 fallow_output::CoverageTier,
385 fallow_output::CoverageSource,
386) {
387 let cc = f64::from(f.cyclomatic);
388 let lookup = file_coverage.and_then(|fc| fc.lookup(f.name.as_str(), f.line, f.col));
389 if let Some(cov_pct) = lookup {
390 *matched += 1;
391 return (
392 crap_formula(cc, cov_pct),
393 Some(cov_pct),
394 fallow_output::CoverageTier::from_pct(cov_pct),
395 fallow_output::CoverageSource::Istanbul,
396 );
397 }
398 if is_test_reachable {
399 return (
400 cc,
401 None,
402 fallow_output::CoverageTier::from_pct(INDIRECT_TEST_COVERAGE_ESTIMATE),
403 fallow_output::CoverageSource::Estimated,
404 );
405 }
406 (
407 cc * cc + cc,
408 None,
409 fallow_output::CoverageTier::None,
410 fallow_output::CoverageSource::Estimated,
411 )
412}
413
414const DIRECT_TEST_COVERAGE_ESTIMATE: f64 = 85.0;
417
418const INDIRECT_TEST_COVERAGE_ESTIMATE: f64 = 40.0;
422const MAX_DIRECT_CALLER_EVIDENCE: usize = 5;
423
424pub(super) struct EstimatedCrapResult {
435 pub max_crap: f64,
436 pub above_threshold: usize,
437 pub per_function: Vec<PerFunctionCrap>,
438}
439
440fn compute_crap_scores_estimated(
441 complexity: &[fallow_types::extract::FunctionComplexity],
442 test_referenced_exports: &rustc_hash::FxHashSet<String>,
443 is_test_reachable: bool,
444 coverage_source: fallow_output::CoverageSource,
445) -> EstimatedCrapResult {
446 if complexity.is_empty() {
447 return EstimatedCrapResult {
448 max_crap: 0.0,
449 above_threshold: 0,
450 per_function: Vec::new(),
451 };
452 }
453 let mut max = 0.0_f64;
454 let mut above = 0usize;
455 let mut per_function = Vec::with_capacity(complexity.len());
456 for f in complexity {
457 let cc = f64::from(f.cyclomatic);
458 let estimated_coverage = if test_referenced_exports.contains(f.name.as_str()) {
459 DIRECT_TEST_COVERAGE_ESTIMATE
460 } else if is_test_reachable {
461 INDIRECT_TEST_COVERAGE_ESTIMATE
462 } else {
463 0.0
464 };
465 let crap = crap_formula(cc, estimated_coverage);
466 let crap_rounded = (crap * 10.0).round() / 10.0;
467 max = max.max(crap);
468 if crap >= CRAP_THRESHOLD {
469 above += 1;
470 }
471 per_function.push(PerFunctionCrap {
472 line: f.line,
473 col: f.col,
474 crap: crap_rounded,
475 coverage_pct: None,
476 coverage_tier: fallow_output::CoverageTier::from_pct(estimated_coverage),
477 coverage_source,
478 });
479 }
480 EstimatedCrapResult {
481 max_crap: (max * 10.0).round() / 10.0,
482 above_threshold: above,
483 per_function,
484 }
485}
486
487#[derive(Debug, Clone)]
501pub(super) struct TemplateInheritContext {
502 pub is_test_reachable: bool,
503 pub test_referenced_exports: rustc_hash::FxHashSet<String>,
504 pub provenance_owner: std::path::PathBuf,
509}
510
511fn build_template_inherit_contexts(
533 graph: &fallow_graph::graph::ModuleGraph,
534 module_by_id: &rustc_hash::FxHashMap<crate::discover::FileId, &crate::source::ModuleInfo>,
535 file_paths: &rustc_hash::FxHashMap<crate::discover::FileId, &std::path::PathBuf>,
536) -> rustc_hash::FxHashMap<crate::discover::FileId, TemplateInheritContext> {
537 let mut out = rustc_hash::FxHashMap::default();
538 for node in &graph.modules {
539 if let Some(context) =
540 template_inherit_context_for_node(node, graph, module_by_id, file_paths)
541 {
542 out.insert(node.file_id, context);
543 }
544 }
545 out
546}
547
548fn template_inherit_context_for_node(
549 node: &fallow_graph::graph::ModuleNode,
550 graph: &fallow_graph::graph::ModuleGraph,
551 module_by_id: &rustc_hash::FxHashMap<crate::discover::FileId, &crate::source::ModuleInfo>,
552 file_paths: &rustc_hash::FxHashMap<crate::discover::FileId, &std::path::PathBuf>,
553) -> Option<TemplateInheritContext> {
554 if !is_template_inherit_candidate(node, module_by_id, file_paths) {
555 return None;
556 }
557 let importers = graph.reverse_deps.get(node.file_id.0 as usize)?;
558 template_inherit_context_from_importers(importers, graph, module_by_id, file_paths)
559}
560
561fn is_template_inherit_candidate(
562 node: &fallow_graph::graph::ModuleNode,
563 module_by_id: &rustc_hash::FxHashMap<crate::discover::FileId, &crate::source::ModuleInfo>,
564 file_paths: &rustc_hash::FxHashMap<crate::discover::FileId, &std::path::PathBuf>,
565) -> bool {
566 let Some(path) = file_paths.get(&node.file_id) else {
567 return false;
568 };
569 if !path
570 .extension()
571 .and_then(|ext| ext.to_str())
572 .is_some_and(|ext| ext.eq_ignore_ascii_case("html"))
573 {
574 return false;
575 }
576 module_by_id.get(&node.file_id).is_some_and(|module| {
577 module
578 .complexity
579 .iter()
580 .any(|finding| finding.name.as_str() == "<template>")
581 })
582}
583
584fn template_inherit_context_from_importers(
585 importers: &[crate::discover::FileId],
586 graph: &fallow_graph::graph::ModuleGraph,
587 module_by_id: &rustc_hash::FxHashMap<crate::discover::FileId, &crate::source::ModuleInfo>,
588 file_paths: &rustc_hash::FxHashMap<crate::discover::FileId, &std::path::PathBuf>,
589) -> Option<TemplateInheritContext> {
590 let mut any_reachable = false;
591 let mut combined_refs = rustc_hash::FxHashSet::default();
592 let mut provenance: Option<std::path::PathBuf> = None;
593 let mut first_owner: Option<std::path::PathBuf> = None;
594
595 for &importer_id in importers {
596 let Some((owner_node, owner_path)) =
597 template_owner(importer_id, graph, module_by_id, file_paths)
598 else {
599 continue;
600 };
601 if first_owner.is_none() {
602 first_owner = Some((*owner_path).clone());
603 }
604 if owner_node.is_test_reachable() {
605 any_reachable = true;
606 provenance.get_or_insert_with(|| (*owner_path).clone());
607 let refs = build_test_referenced_exports(&owner_node.exports, &graph.modules);
608 combined_refs.extend(refs);
609 }
610 }
611
612 let provenance_owner = provenance.or(first_owner)?;
613 Some(TemplateInheritContext {
614 is_test_reachable: any_reachable,
615 test_referenced_exports: combined_refs,
616 provenance_owner,
617 })
618}
619
620fn template_owner<'a>(
621 importer_id: crate::discover::FileId,
622 graph: &'a fallow_graph::graph::ModuleGraph,
623 module_by_id: &rustc_hash::FxHashMap<crate::discover::FileId, &crate::source::ModuleInfo>,
624 file_paths: &'a rustc_hash::FxHashMap<crate::discover::FileId, &std::path::PathBuf>,
625) -> Option<(&'a fallow_graph::graph::ModuleNode, &'a std::path::PathBuf)> {
626 let owner_node = graph.modules.get(importer_id.0 as usize)?;
627 let owner_path = *file_paths.get(&importer_id)?;
628 if !is_template_owner_path(owner_path) || graph.test_entry_points.contains(&importer_id) {
629 return None;
630 }
631 let owner_has_component = module_by_id
632 .get(&importer_id)
633 .is_some_and(|module| module.has_angular_component_template_url);
634 owner_has_component.then_some((owner_node, owner_path))
635}
636
637fn is_template_owner_path(path: &std::path::Path) -> bool {
638 path.extension()
639 .and_then(|ext| ext.to_str())
640 .is_some_and(|ext| {
641 matches!(
642 ext.to_ascii_lowercase().as_str(),
643 "ts" | "tsx" | "mts" | "cts"
644 )
645 })
646}
647
648fn build_test_referenced_exports(
653 exports: &[fallow_graph::graph::ExportSymbol],
654 graph_modules: &[fallow_graph::graph::ModuleNode],
655) -> rustc_hash::FxHashSet<String> {
656 let mut set = rustc_hash::FxHashSet::default();
657 for export in exports {
658 if export.is_type_only {
659 continue;
660 }
661 let has_test_ref = export.references.iter().any(|reference| {
662 graph_modules
663 .get(reference.from_file.0 as usize)
664 .is_some_and(fallow_graph::graph::ModuleNode::is_test_reachable)
665 });
666 if has_test_ref {
667 set.insert(export.name.to_string());
668 }
669 }
670 set
671}
672
673fn collect_direct_callers(
674 graph: &fallow_graph::graph::ModuleGraph,
675 file_paths: &rustc_hash::FxHashMap<crate::discover::FileId, &std::path::PathBuf>,
676) -> rustc_hash::FxHashMap<std::path::PathBuf, Vec<DirectCallerEvidence>> {
677 let mut callers_by_target = rustc_hash::FxHashMap::default();
678 for node in &graph.modules {
679 let Some(target_path) = file_paths.get(&node.file_id) else {
680 continue;
681 };
682 let mut callers = graph
683 .direct_importer_summaries(node.file_id)
684 .into_iter()
685 .filter_map(|summary| {
686 file_paths
687 .get(&summary.source)
688 .map(|caller_path| DirectCallerEvidence {
689 path: (*caller_path).clone(),
690 symbols: summary
691 .symbols
692 .into_iter()
693 .map(|symbol| DirectCallerSymbolEvidence {
694 imported: symbol.imported,
695 local: symbol.local,
696 type_only: symbol.type_only,
697 })
698 .collect(),
699 })
700 })
701 .collect::<Vec<_>>();
702 callers.sort_by(|a, b| a.path.cmp(&b.path));
703 callers.truncate(MAX_DIRECT_CALLER_EVIDENCE);
704 if !callers.is_empty() {
705 callers_by_target.insert((*target_path).clone(), callers);
706 }
707 }
708 callers_by_target
709}
710
711#[expect(
714 clippy::suboptimal_flops,
715 reason = "explicit multiplication matches the CRAP formula specification"
716)]
717fn crap_formula(cc: f64, coverage_pct: f64) -> f64 {
718 let uncovered = 1.0 - coverage_pct / 100.0;
719 cc * cc * uncovered * uncovered * uncovered + cc
720}
721
722const ANONYMOUS_FALLBACK_MAX_COLUMN_DRIFT: u32 = 16;
728
729pub struct IstanbulFileCoverage {
732 functions: rustc_hash::FxHashMap<(String, u32, u32), f64>,
742}
743
744impl IstanbulFileCoverage {
745 pub fn lookup(&self, name: &str, line: u32, col: u32) -> Option<f64> {
762 if let Some(&pct) = self.functions.get(&(name.to_string(), line, col)) {
763 return Some(pct);
764 }
765 if let Some(pct) = self
766 .functions
767 .iter()
768 .filter(|((n, l, _), _)| n == name && l.abs_diff(line) <= 2)
769 .min_by_key(|((_, l, c), _)| (l.abs_diff(line), c.abs_diff(col)))
770 .map(|(_, &pct)| pct)
771 {
772 return Some(pct);
773 }
774 let mut nearest_distance: Option<(u32, u32)> = None;
775 let mut nearest_pct: Option<f64> = None;
776 let mut tied = false;
777 for ((n, l, c), &pct) in &self.functions {
778 if !n.starts_with("(anonymous_") {
779 continue;
780 }
781 if l.abs_diff(line) > 2 {
782 continue;
783 }
784 let dist = (l.abs_diff(line), c.abs_diff(col));
785 if dist.0 > 0 && dist.1 > ANONYMOUS_FALLBACK_MAX_COLUMN_DRIFT {
786 continue;
787 }
788 match nearest_distance {
789 None => {
790 nearest_distance = Some(dist);
791 nearest_pct = Some(pct);
792 tied = false;
793 }
794 Some(prev) if dist < prev => {
795 nearest_distance = Some(dist);
796 nearest_pct = Some(pct);
797 tied = false;
798 }
799 Some(prev) if dist == prev => {
800 tied = true;
801 }
802 Some(_) => {}
803 }
804 }
805 if tied { None } else { nearest_pct }
806 }
807}
808
809pub struct IstanbulCoverage {
811 files: rustc_hash::FxHashMap<std::path::PathBuf, IstanbulFileCoverage>,
812}
813
814impl IstanbulCoverage {
815 pub fn get(&self, path: &std::path::Path) -> Option<&IstanbulFileCoverage> {
817 self.files.get(path)
818 }
819}
820
821enum CrapCoverageResolution<'a> {
829 TemplateInherited(&'a TemplateInheritContext),
830 Istanbul {
831 file_coverage: Option<&'a IstanbulFileCoverage>,
832 },
833 StaticEstimated,
834}
835
836fn resolve_crap_coverage<'a>(
837 template_inherit: Option<&'a TemplateInheritContext>,
838 istanbul_coverage: Option<&'a IstanbulCoverage>,
839 path: &std::path::Path,
840) -> CrapCoverageResolution<'a> {
841 if let Some(inherit_ctx) = template_inherit {
842 CrapCoverageResolution::TemplateInherited(inherit_ctx)
843 } else if let Some(istanbul) = istanbul_coverage {
844 let canonical = dunce::canonicalize(path).unwrap_or_else(|_| path.to_path_buf());
845 CrapCoverageResolution::Istanbul {
846 file_coverage: istanbul.get(&canonical),
847 }
848 } else {
849 CrapCoverageResolution::StaticEstimated
850 }
851}
852
853pub(super) fn auto_detect_coverage(root: &std::path::Path) -> Option<std::path::PathBuf> {
860 let candidates = [
861 root.join("coverage/coverage-final.json"),
862 root.join(".nyc_output/coverage-final.json"),
863 ];
864 candidates.into_iter().find(|p| p.is_file())
865}
866
867pub fn resolve_relative_to_root(
873 path: &std::path::Path,
874 project_root: Option<&std::path::Path>,
875) -> std::path::PathBuf {
876 if fallow_types::path_util::is_absolute_path_any_platform(path) {
877 return path.to_path_buf();
878 }
879 match project_root {
880 Some(root) => root.join(path),
881 None => path.to_path_buf(),
882 }
883}
884
885pub(super) fn load_istanbul_coverage(
897 path: &std::path::Path,
898 coverage_root: Option<&std::path::Path>,
899 project_root: Option<&std::path::Path>,
900) -> Result<IstanbulCoverage, String> {
901 super::validate_coverage_root_absolute(coverage_root)?;
902 let resolved = resolve_relative_to_root(path, project_root);
903 let file_path = if resolved.is_dir() {
904 let candidate = resolved.join("coverage-final.json");
905 if candidate.is_file() {
906 candidate
907 } else {
908 return Err(format!(
909 "no coverage-final.json found in {}",
910 resolved.display()
911 ));
912 }
913 } else {
914 resolved
915 };
916
917 let json = std::fs::read_to_string(&file_path)
918 .map_err(|e| format!("failed to read coverage file {}: {e}", file_path.display()))?;
919
920 let raw: std::collections::BTreeMap<String, oxc_coverage_instrument::FileCoverage> =
921 oxc_coverage_instrument::parse_coverage_map(&json).map_err(|e| {
922 format!(
923 "failed to parse coverage data from {}: {e}",
924 file_path.display()
925 )
926 })?;
927
928 let mut files = rustc_hash::FxHashMap::default();
929 for file_cov in raw.values() {
930 let raw_path = std::path::PathBuf::from(&file_cov.path);
931 let file_path = if let (Some(cov_root), Some(proj_root)) = (coverage_root, project_root) {
932 raw_path
933 .strip_prefix(cov_root)
934 .map(|rel| proj_root.join(rel))
935 .unwrap_or(raw_path)
936 } else {
937 raw_path
938 };
939 let canonical = dunce::canonicalize(&file_path).unwrap_or(file_path);
940
941 let mut functions = rustc_hash::FxHashMap::default();
942 for (fn_id, fn_entry) in &file_cov.fn_map {
943 let coverage_pct = compute_function_statement_coverage(file_cov, fn_id, fn_entry);
944 insert_istanbul_function_coverage(&mut functions, fn_entry, coverage_pct);
945 }
946
947 files.insert(canonical, IstanbulFileCoverage { functions });
948 }
949
950 Ok(IstanbulCoverage { files })
951}
952
953fn insert_istanbul_function_coverage(
954 functions: &mut rustc_hash::FxHashMap<(String, u32, u32), f64>,
955 fn_entry: &oxc_coverage_instrument::FnEntry,
956 coverage_pct: f64,
957) {
958 let name = fn_entry.name.clone();
959 let primary = (
960 name.clone(),
961 effective_istanbul_fn_line(fn_entry),
962 effective_istanbul_fn_col(fn_entry),
963 );
964 functions.insert(primary.clone(), coverage_pct);
965
966 let declaration = (name, fn_entry.decl.start.line, fn_entry.decl.start.column);
967 if declaration != primary {
968 functions.entry(declaration).or_insert(coverage_pct);
969 }
970}
971
972fn effective_istanbul_fn_line(fn_entry: &oxc_coverage_instrument::FnEntry) -> u32 {
973 if fn_entry.line > 0 {
974 fn_entry.line
975 } else {
976 fn_entry.decl.start.line
977 }
978}
979
980fn effective_istanbul_fn_col(fn_entry: &oxc_coverage_instrument::FnEntry) -> u32 {
985 fn_entry.decl.start.column
986}
987
988fn compute_function_statement_coverage(
995 file_cov: &oxc_coverage_instrument::FileCoverage,
996 fn_id: &str,
997 fn_entry: &oxc_coverage_instrument::FnEntry,
998) -> f64 {
999 let fn_start_line = fn_entry.loc.start.line;
1000 let fn_start_col = fn_entry.loc.start.column;
1001 let fn_end_line = fn_entry.loc.end.line;
1002 let fn_end_col = fn_entry.loc.end.column;
1003
1004 let mut total = 0u32;
1005 let mut covered = 0u32;
1006
1007 for (stmt_id, stmt_loc) in &file_cov.statement_map {
1008 let after_start = stmt_loc.start.line > fn_start_line
1009 || (stmt_loc.start.line == fn_start_line && stmt_loc.start.column >= fn_start_col);
1010 let before_end = stmt_loc.end.line < fn_end_line
1011 || (stmt_loc.end.line == fn_end_line && stmt_loc.end.column <= fn_end_col);
1012
1013 if after_start && before_end {
1014 total += 1;
1015 if file_cov.s.get(stmt_id).copied().unwrap_or(0) > 0 {
1016 covered += 1;
1017 }
1018 }
1019 }
1020
1021 if total == 0 {
1022 let hit = file_cov.f.get(fn_id).copied().unwrap_or(0);
1023 if hit > 0 { 100.0 } else { 0.0 }
1024 } else {
1025 f64::from(covered) / f64::from(total) * 100.0
1026 }
1027}
1028
1029pub(super) fn count_unused_exports_by_path(
1034 unused_exports: &[crate::results::UnusedExportFinding],
1035) -> rustc_hash::FxHashMap<&std::path::Path, usize> {
1036 let mut map: rustc_hash::FxHashMap<&std::path::Path, usize> = rustc_hash::FxHashMap::default();
1037 for exp in unused_exports {
1038 *map.entry(exp.export.path.as_path()).or_default() += 1;
1039 }
1040 map
1041}
1042
1043pub(super) fn compute_maintainability_index(
1063 complexity_density: f64,
1064 dead_code_ratio: f64,
1065 fan_out: usize,
1066 lines: u32,
1067) -> f64 {
1068 let dampening = (f64::from(lines) / fallow_output::MI_DENSITY_MIN_LINES).min(1.0);
1069 let fan_out_penalty = ((fan_out as f64).ln_1p() * 4.0).min(15.0);
1070 #[expect(
1071 clippy::suboptimal_flops,
1072 reason = "formula matches documented specification"
1073 )]
1074 let score = 100.0
1075 - (complexity_density * 30.0 * dampening)
1076 - (dead_code_ratio * 20.0)
1077 - fan_out_penalty;
1078 score.clamp(0.0, 100.0)
1079}
1080
1081fn file_score_structural_concern(score: &FileHealthScore) -> f64 {
1082 (100.0 - score.maintainability_index).clamp(0.0, 100.0)
1083}
1084
1085fn file_score_crap_concern(crap_max: f64) -> f64 {
1086 if crap_max <= 0.0 {
1087 0.0
1088 } else if crap_max < 15.0 {
1089 (crap_max / 15.0) * 45.0
1090 } else if crap_max < CRAP_THRESHOLD {
1091 ((crap_max - 15.0) / 15.0).mul_add(30.0, 45.0)
1092 } else if crap_max < 100.0 {
1093 ((crap_max - CRAP_THRESHOLD) / (100.0 - CRAP_THRESHOLD)).mul_add(25.0, 75.0)
1094 } else {
1095 100.0
1096 }
1097}
1098
1099fn file_score_triage_concern(score: &FileHealthScore) -> f64 {
1100 file_score_structural_concern(score).max(file_score_crap_concern(score.crap_max))
1101}
1102
1103#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1109pub enum FileScoreConcern {
1110 Structural,
1111 Risk,
1112}
1113
1114impl FileScoreConcern {
1115 pub const fn label(self) -> &'static str {
1117 match self {
1118 Self::Structural => "structure",
1119 Self::Risk => "risk",
1120 }
1121 }
1122}
1123
1124pub fn file_score_concern_axis(score: &FileHealthScore) -> FileScoreConcern {
1129 if score.crap_max <= 0.0 {
1130 FileScoreConcern::Structural
1131 } else if file_score_crap_concern(score.crap_max) >= file_score_structural_concern(score) {
1132 FileScoreConcern::Risk
1133 } else {
1134 FileScoreConcern::Structural
1135 }
1136}
1137
1138fn compare_file_score_triage(a: &FileHealthScore, b: &FileHealthScore) -> std::cmp::Ordering {
1139 file_score_triage_concern(b)
1140 .total_cmp(&file_score_triage_concern(a))
1141 .then_with(|| b.crap_max.total_cmp(&a.crap_max))
1142 .then_with(|| a.maintainability_index.total_cmp(&b.maintainability_index))
1143 .then_with(|| a.path.cmp(&b.path))
1144}
1145
1146pub(super) fn compute_file_scores(
1152 modules: &[crate::source::ModuleInfo],
1153 file_paths: &rustc_hash::FxHashMap<crate::discover::FileId, &std::path::PathBuf>,
1154 changed_files: Option<&rustc_hash::FxHashSet<std::path::PathBuf>>,
1155 analysis_output: crate::results::DeadCodeAnalysisArtifacts,
1156 istanbul_coverage: Option<&IstanbulCoverage>,
1157 root: &std::path::Path,
1158) -> Result<FileScoreOutput, String> {
1159 let retained_graph = analysis_output.graph.ok_or("graph not available")?;
1160 let graph = retained_graph.as_graph();
1161 let results = &analysis_output.results;
1162
1163 let circular_files = collect_circular_files(results);
1164 let top_complex_fns = collect_top_complex_fns(modules, file_paths);
1165 let cycle_members = collect_cycle_members(results);
1166 let direct_callers = collect_direct_callers(graph, file_paths);
1167 let unused_export_names = collect_unused_export_names(results);
1168
1169 let unused_files: rustc_hash::FxHashSet<&std::path::Path> = results
1170 .unused_files
1171 .iter()
1172 .map(|f| f.file.path.as_path())
1173 .collect();
1174
1175 let unused_exports_by_path = count_unused_exports_by_path(&results.unused_exports);
1176
1177 let FileScoreCoverageSetup {
1178 module_by_id,
1179 coverage,
1180 } = prepare_file_score_coverage_setup(modules, file_paths, results, graph, root);
1181
1182 let template_inherit = build_template_inherit_contexts(graph, &module_by_id, file_paths);
1183
1184 let mut acc = accumulate_file_scores(
1185 unused_export_names,
1186 &FileScoreLoopCtx {
1187 graph,
1188 file_paths,
1189 module_by_id: &module_by_id,
1190 unused_files: &unused_files,
1191 unused_exports_by_path: &unused_exports_by_path,
1192 template_inherit: &template_inherit,
1193 istanbul_coverage,
1194 },
1195 );
1196 acc.scores = finalize_file_score_list(acc.scores, changed_files);
1197
1198 Ok(build_file_score_output(FileScoreOutputParts {
1199 graph,
1200 file_paths,
1201 results,
1202 scores: acc.scores,
1203 coverage,
1204 circular_files,
1205 top_complex_fns,
1206 entry_points: acc.entry_points,
1207 value_export_counts: acc.value_export_counts,
1208 unused_export_names: acc.unused_export_names,
1209 cycle_members,
1210 direct_callers,
1211 istanbul_matched: acc.istanbul_matched,
1212 istanbul_total: acc.istanbul_total,
1213 per_function_crap: acc.per_function_crap,
1214 template_inherit,
1215 }))
1216}
1217
1218struct FileScoreLoopCtx<'a> {
1220 graph: &'a fallow_graph::graph::ModuleGraph,
1221 file_paths: &'a rustc_hash::FxHashMap<crate::discover::FileId, &'a std::path::PathBuf>,
1222 module_by_id: &'a rustc_hash::FxHashMap<crate::discover::FileId, &'a crate::source::ModuleInfo>,
1223 unused_files: &'a rustc_hash::FxHashSet<&'a std::path::Path>,
1224 unused_exports_by_path: &'a rustc_hash::FxHashMap<&'a std::path::Path, usize>,
1225 template_inherit: &'a rustc_hash::FxHashMap<crate::discover::FileId, TemplateInheritContext>,
1226 istanbul_coverage: Option<&'a IstanbulCoverage>,
1227}
1228
1229struct FileScoreAccumulator {
1231 scores: Vec<FileHealthScore>,
1232 entry_points: rustc_hash::FxHashSet<std::path::PathBuf>,
1233 value_export_counts: rustc_hash::FxHashMap<std::path::PathBuf, usize>,
1234 unused_export_names: rustc_hash::FxHashMap<std::path::PathBuf, Vec<String>>,
1235 per_function_crap: rustc_hash::FxHashMap<std::path::PathBuf, Vec<PerFunctionCrap>>,
1236 istanbul_matched: usize,
1237 istanbul_total: usize,
1238}
1239
1240impl FileScoreAccumulator {
1241 fn with_capacity(modules: usize) -> Self {
1243 FileScoreAccumulator {
1244 scores: Vec::with_capacity(modules),
1245 entry_points: rustc_hash::FxHashSet::default(),
1246 value_export_counts: rustc_hash::FxHashMap::default(),
1247 unused_export_names: rustc_hash::FxHashMap::default(),
1248 per_function_crap: rustc_hash::FxHashMap::default(),
1249 istanbul_matched: 0,
1250 istanbul_total: 0,
1251 }
1252 }
1253}
1254
1255fn accumulate_file_scores(
1258 unused_export_names: rustc_hash::FxHashMap<std::path::PathBuf, Vec<String>>,
1259 ctx: &FileScoreLoopCtx<'_>,
1260) -> FileScoreAccumulator {
1261 let mut acc = FileScoreAccumulator {
1262 unused_export_names,
1263 ..FileScoreAccumulator::with_capacity(ctx.graph.modules.len())
1264 };
1265 for node in &ctx.graph.modules {
1266 let Some(path) = ctx.file_paths.get(&node.file_id) else {
1267 continue;
1268 };
1269 record_entry_point(&mut acc.entry_points, node, path);
1270 let score = compute_one_file_score(&mut acc, ctx, node, path);
1271 acc.scores.push(score);
1272 }
1273 acc
1274}
1275
1276fn finalize_file_score_list(
1279 mut scores: Vec<FileHealthScore>,
1280 changed_files: Option<&rustc_hash::FxHashSet<std::path::PathBuf>>,
1281) -> Vec<FileHealthScore> {
1282 if let Some(changed) = changed_files {
1283 scores.retain(|s| changed.contains(&s.path));
1284 }
1285 scores.retain(|s| s.function_count > 0);
1286 scores.sort_by(compare_file_score_triage);
1287 scores
1288}
1289
1290fn compute_one_file_score(
1292 acc: &mut FileScoreAccumulator,
1293 ctx: &FileScoreLoopCtx<'_>,
1294 node: &fallow_graph::graph::ModuleNode,
1295 path: &std::path::Path,
1296) -> FileHealthScore {
1297 let fan_in = ctx
1298 .graph
1299 .reverse_deps
1300 .get(node.file_id.0 as usize)
1301 .map_or(0, Vec::len);
1302 let fan_out = node.edge_range.len();
1303
1304 let (total_cyclomatic, total_cognitive, function_count, lines) = ctx
1305 .module_by_id
1306 .get(&node.file_id)
1307 .map_or((0, 0, 0, 0), |module| aggregate_complexity(module));
1308
1309 let value_exports = node.exports.iter().filter(|e| !e.is_type_only).count();
1310 let path_owned = path.to_path_buf();
1311 acc.value_export_counts
1312 .insert(path_owned.clone(), value_exports);
1313 record_unused_file_export_names(
1314 path_owned.as_path(),
1315 &node.exports,
1316 ctx.unused_files,
1317 &mut acc.unused_export_names,
1318 );
1319
1320 let (dead_code_ratio_rounded, complexity_density_rounded, maintainability_index_rounded) =
1321 compute_file_score_metrics(node, &path_owned, ctx, total_cyclomatic, lines, fan_out);
1322
1323 let crap = compute_file_score_crap(
1324 node,
1325 ctx.module_by_id.get(&node.file_id).copied(),
1326 ctx.graph,
1327 ctx.template_inherit.get(&node.file_id),
1328 ctx.istanbul_coverage,
1329 &path_owned,
1330 );
1331 acc.istanbul_matched += crap.istanbul_matched;
1332 acc.istanbul_total += crap.istanbul_total;
1333 record_per_function_crap(&mut acc.per_function_crap, &path_owned, crap.per_function);
1334
1335 FileHealthScore {
1336 path: path_owned,
1337 fan_in,
1338 fan_out,
1339 dead_code_ratio: dead_code_ratio_rounded,
1340 complexity_density: complexity_density_rounded,
1341 maintainability_index: maintainability_index_rounded,
1342 total_cyclomatic,
1343 total_cognitive,
1344 function_count,
1345 lines,
1346 crap_max: crap.max,
1347 crap_above_threshold: crap.above_threshold,
1348 }
1349}
1350
1351fn compute_file_score_metrics(
1354 node: &fallow_graph::graph::ModuleNode,
1355 path: &std::path::Path,
1356 ctx: &FileScoreLoopCtx<'_>,
1357 total_cyclomatic: u32,
1358 lines: u32,
1359 fan_out: usize,
1360) -> (f64, f64, f64) {
1361 let dead_code_ratio = compute_dead_code_ratio(
1362 path,
1363 &node.exports,
1364 ctx.unused_files,
1365 ctx.unused_exports_by_path,
1366 );
1367 let complexity_density = compute_complexity_density(total_cyclomatic, lines);
1368
1369 let dead_code_ratio_rounded = (dead_code_ratio * 100.0).round() / 100.0;
1370 let complexity_density_rounded = (complexity_density * 100.0).round() / 100.0;
1371
1372 let maintainability_index = compute_maintainability_index(
1373 complexity_density_rounded,
1374 dead_code_ratio_rounded,
1375 fan_out,
1376 lines,
1377 );
1378 (
1379 dead_code_ratio_rounded,
1380 complexity_density_rounded,
1381 (maintainability_index * 10.0).round() / 10.0,
1382 )
1383}
1384
1385fn build_file_score_output(parts: FileScoreOutputParts<'_>) -> FileScoreOutput {
1386 let total_exports: usize = parts.graph.modules.iter().map(|m| m.exports.len()).sum();
1387 let unused_deps = parts.results.unused_dependencies.len()
1388 + parts.results.unused_dev_dependencies.len()
1389 + parts.results.unused_optional_dependencies.len();
1390 let analysis_snapshot =
1391 build_analysis_counts_snapshot(parts.graph, parts.file_paths, parts.results, unused_deps);
1392 let analysis_counts =
1393 build_file_score_analysis_counts(parts.results, total_exports, unused_deps);
1394 let template_inherit_provenance =
1395 build_template_inherit_provenance(parts.template_inherit, parts.file_paths);
1396
1397 FileScoreOutput {
1398 scores: parts.scores,
1399 coverage: parts.coverage,
1400 circular_files: parts.circular_files,
1401 top_complex_fns: parts.top_complex_fns,
1402 entry_points: parts.entry_points,
1403 value_export_counts: parts.value_export_counts,
1404 unused_export_names: parts.unused_export_names,
1405 cycle_members: parts.cycle_members,
1406 direct_callers: parts.direct_callers,
1407 analysis_counts,
1408 prop_drilling_chains: parts.results.prop_drilling_chains.clone(),
1409 render_fan_in: parts.results.render_fan_in.clone(),
1410 analysis_snapshot,
1411 istanbul_matched: parts.istanbul_matched,
1412 istanbul_total: parts.istanbul_total,
1413 per_function_crap: parts.per_function_crap,
1414 template_inherit_provenance,
1415 }
1416}
1417
1418fn build_file_score_analysis_counts(
1419 results: &crate::results::AnalysisResults,
1420 total_exports: usize,
1421 unused_deps: usize,
1422) -> crate::vital_signs::AnalysisCounts {
1423 crate::vital_signs::AnalysisCounts {
1424 total_exports,
1425 dead_files: results.unused_files.len(),
1426 dead_exports: results.unused_exports.len() + results.unused_types.len(),
1427 unused_deps,
1428 circular_deps: results.circular_dependencies.len(),
1429 total_deps: 0usize,
1430 }
1431}
1432
1433fn build_template_inherit_provenance(
1434 template_inherit: rustc_hash::FxHashMap<crate::discover::FileId, TemplateInheritContext>,
1435 file_paths: &rustc_hash::FxHashMap<crate::discover::FileId, &std::path::PathBuf>,
1436) -> rustc_hash::FxHashMap<std::path::PathBuf, std::path::PathBuf> {
1437 template_inherit
1438 .into_iter()
1439 .filter_map(|(file_id, ctx)| {
1440 file_paths
1441 .get(&file_id)
1442 .map(|path| ((**path).clone(), ctx.provenance_owner))
1443 })
1444 .collect()
1445}
1446
1447fn record_entry_point(
1448 entry_points: &mut rustc_hash::FxHashSet<std::path::PathBuf>,
1449 node: &fallow_graph::graph::ModuleNode,
1450 path: &std::path::Path,
1451) {
1452 if node.is_entry_point() {
1453 entry_points.insert(path.to_path_buf());
1454 }
1455}
1456
1457fn record_unused_file_export_names(
1458 path: &std::path::Path,
1459 exports: &[fallow_graph::graph::ExportSymbol],
1460 unused_files: &rustc_hash::FxHashSet<&std::path::Path>,
1461 unused_export_names: &mut rustc_hash::FxHashMap<std::path::PathBuf, Vec<String>>,
1462) {
1463 if !unused_files.contains(path) || unused_export_names.contains_key(path) {
1464 return;
1465 }
1466
1467 let names: Vec<String> = exports
1468 .iter()
1469 .filter(|export| !export.is_type_only)
1470 .map(|export| export.name.to_string())
1471 .collect();
1472 if !names.is_empty() {
1473 unused_export_names.insert(path.to_path_buf(), names);
1474 }
1475}
1476
1477struct FileScoreCrap {
1478 max: f64,
1479 above_threshold: usize,
1480 per_function: Vec<PerFunctionCrap>,
1481 istanbul_matched: usize,
1482 istanbul_total: usize,
1483}
1484
1485impl FileScoreCrap {
1486 fn empty() -> Self {
1487 Self {
1488 max: 0.0,
1489 above_threshold: 0,
1490 per_function: Vec::new(),
1491 istanbul_matched: 0,
1492 istanbul_total: 0,
1493 }
1494 }
1495
1496 fn estimated(result: EstimatedCrapResult) -> Self {
1497 Self {
1498 max: result.max_crap,
1499 above_threshold: result.above_threshold,
1500 per_function: result.per_function,
1501 istanbul_matched: 0,
1502 istanbul_total: 0,
1503 }
1504 }
1505
1506 fn istanbul(result: IstanbulCrapResult) -> Self {
1507 Self {
1508 max: result.max_crap,
1509 above_threshold: result.above_threshold,
1510 per_function: result.per_function,
1511 istanbul_matched: result.matched,
1512 istanbul_total: result.total,
1513 }
1514 }
1515}
1516
1517fn compute_file_score_crap(
1518 node: &fallow_graph::graph::ModuleNode,
1519 module: Option<&crate::source::ModuleInfo>,
1520 graph: &fallow_graph::graph::ModuleGraph,
1521 template_inherit: Option<&TemplateInheritContext>,
1522 istanbul_coverage: Option<&IstanbulCoverage>,
1523 path: &std::path::Path,
1524) -> FileScoreCrap {
1525 let Some(module) = module else {
1526 return FileScoreCrap::empty();
1527 };
1528
1529 let is_coverage_suppressed = crate::suppress::is_file_suppressed(
1530 &module.suppressions,
1531 fallow_types::suppress::IssueKind::CoverageGaps,
1532 );
1533 let is_test_reachable = node.is_test_reachable() || is_coverage_suppressed;
1534 let resolution = resolve_crap_coverage(template_inherit, istanbul_coverage, path);
1535 match resolution {
1536 CrapCoverageResolution::TemplateInherited(inherit_ctx) => {
1537 compute_template_inherited_crap(module, inherit_ctx)
1538 }
1539 CrapCoverageResolution::Istanbul { file_coverage } => {
1540 compute_istanbul_file_crap(module, file_coverage, is_test_reachable)
1541 }
1542 CrapCoverageResolution::StaticEstimated => {
1543 compute_static_file_crap(module, &node.exports, &graph.modules, is_test_reachable)
1544 }
1545 }
1546}
1547
1548fn compute_template_inherited_crap(
1549 module: &crate::source::ModuleInfo,
1550 inherit_ctx: &TemplateInheritContext,
1551) -> FileScoreCrap {
1552 FileScoreCrap::estimated(compute_crap_scores_estimated(
1553 &module.complexity,
1554 &inherit_ctx.test_referenced_exports,
1555 inherit_ctx.is_test_reachable,
1556 fallow_output::CoverageSource::EstimatedComponentInherited,
1557 ))
1558}
1559
1560fn compute_istanbul_file_crap(
1561 module: &crate::source::ModuleInfo,
1562 file_coverage: Option<&IstanbulFileCoverage>,
1563 is_test_reachable: bool,
1564) -> FileScoreCrap {
1565 FileScoreCrap::istanbul(compute_crap_scores_istanbul(
1566 &module.complexity,
1567 file_coverage,
1568 is_test_reachable,
1569 ))
1570}
1571
1572fn compute_static_file_crap(
1573 module: &crate::source::ModuleInfo,
1574 exports: &[fallow_graph::graph::ExportSymbol],
1575 graph_modules: &[fallow_graph::graph::ModuleNode],
1576 is_test_reachable: bool,
1577) -> FileScoreCrap {
1578 let test_refs = build_test_referenced_exports(exports, graph_modules);
1579 FileScoreCrap::estimated(compute_crap_scores_estimated(
1580 &module.complexity,
1581 &test_refs,
1582 is_test_reachable,
1583 fallow_output::CoverageSource::Estimated,
1584 ))
1585}
1586
1587fn record_per_function_crap(
1588 per_function_crap: &mut rustc_hash::FxHashMap<std::path::PathBuf, Vec<PerFunctionCrap>>,
1589 path: &std::path::Path,
1590 per_function: Vec<PerFunctionCrap>,
1591) {
1592 if !per_function.is_empty() {
1593 per_function_crap.insert(path.to_path_buf(), per_function);
1594 }
1595}
1596
1597struct FileScoreCoverageSetup<'a> {
1598 module_by_id: rustc_hash::FxHashMap<crate::discover::FileId, &'a crate::source::ModuleInfo>,
1599 coverage: CoverageGapData,
1600}
1601
1602fn prepare_file_score_coverage_setup<'a>(
1603 modules: &'a [crate::source::ModuleInfo],
1604 file_paths: &rustc_hash::FxHashMap<crate::discover::FileId, &std::path::PathBuf>,
1605 results: &crate::results::AnalysisResults,
1606 graph: &fallow_graph::graph::ModuleGraph,
1607 root: &std::path::Path,
1608) -> FileScoreCoverageSetup<'a> {
1609 let module_by_id: rustc_hash::FxHashMap<_, _> =
1610 modules.iter().map(|m| (m.file_id, m)).collect();
1611 let unused_exports: rustc_hash::FxHashSet<(&std::path::Path, String)> = results
1612 .unused_exports
1613 .iter()
1614 .map(|export| {
1615 (
1616 export.export.path.as_path(),
1617 export.export.export_name.clone(),
1618 )
1619 })
1620 .collect();
1621 let coverage = compute_coverage_gaps(graph, file_paths, &module_by_id, &unused_exports, root);
1622 FileScoreCoverageSetup {
1623 module_by_id,
1624 coverage,
1625 }
1626}
1627
1628fn collect_circular_files(
1629 results: &crate::results::AnalysisResults,
1630) -> rustc_hash::FxHashSet<std::path::PathBuf> {
1631 results
1632 .circular_dependencies
1633 .iter()
1634 .flat_map(|c| c.cycle.files.iter().cloned())
1635 .collect()
1636}
1637
1638fn collect_top_complex_fns(
1639 modules: &[crate::source::ModuleInfo],
1640 file_paths: &rustc_hash::FxHashMap<crate::discover::FileId, &std::path::PathBuf>,
1641) -> rustc_hash::FxHashMap<std::path::PathBuf, Vec<(String, u32, u16)>> {
1642 let mut top_complex_fns = rustc_hash::FxHashMap::default();
1643 for module in modules {
1644 if module.complexity.is_empty() {
1645 continue;
1646 }
1647 let Some(path) = file_paths.get(&module.file_id) else {
1648 continue;
1649 };
1650 let mut funcs: Vec<(String, u32, u16)> = module
1651 .complexity
1652 .iter()
1653 .map(|f| (f.name.clone(), f.line, f.cognitive))
1654 .collect();
1655 funcs.sort_by_key(|f| std::cmp::Reverse(f.2));
1656 funcs.truncate(3);
1657 if funcs[0].2 > 0 {
1658 top_complex_fns.insert((*path).clone(), funcs);
1659 }
1660 }
1661 top_complex_fns
1662}
1663
1664fn collect_cycle_members(
1665 results: &crate::results::AnalysisResults,
1666) -> rustc_hash::FxHashMap<std::path::PathBuf, Vec<std::path::PathBuf>> {
1667 let mut cycle_members: rustc_hash::FxHashMap<std::path::PathBuf, Vec<std::path::PathBuf>> =
1668 rustc_hash::FxHashMap::default();
1669 for cycle in &results.circular_dependencies {
1670 for file in &cycle.cycle.files {
1671 let others: Vec<std::path::PathBuf> = cycle
1672 .cycle
1673 .files
1674 .iter()
1675 .filter(|f| *f != file)
1676 .cloned()
1677 .collect();
1678 cycle_members
1679 .entry(file.clone())
1680 .or_default()
1681 .extend(others);
1682 }
1683 }
1684 for members in cycle_members.values_mut() {
1685 members.sort();
1686 members.dedup();
1687 }
1688 cycle_members
1689}
1690
1691fn collect_unused_export_names(
1692 results: &crate::results::AnalysisResults,
1693) -> rustc_hash::FxHashMap<std::path::PathBuf, Vec<String>> {
1694 let mut unused_export_names: rustc_hash::FxHashMap<std::path::PathBuf, Vec<String>> =
1695 rustc_hash::FxHashMap::default();
1696 for exp in &results.unused_exports {
1697 unused_export_names
1698 .entry(exp.export.path.clone())
1699 .or_default()
1700 .push(exp.export.export_name.clone());
1701 }
1702 unused_export_names
1703}
1704
1705fn build_analysis_counts_snapshot(
1706 graph: &fallow_graph::graph::ModuleGraph,
1707 file_paths: &rustc_hash::FxHashMap<crate::discover::FileId, &std::path::PathBuf>,
1708 results: &crate::results::AnalysisResults,
1709 unused_deps: usize,
1710) -> AnalysisCountsSnapshot {
1711 let mut module_export_counts = rustc_hash::FxHashMap::with_capacity_and_hasher(
1712 graph.modules.len(),
1713 rustc_hash::FxBuildHasher,
1714 );
1715 for module in &graph.modules {
1716 if let Some(path) = file_paths.get(&module.file_id) {
1717 module_export_counts.insert((*path).clone(), module.exports.len());
1718 }
1719 }
1720
1721 let mut unused_export_paths =
1722 Vec::with_capacity(results.unused_exports.len() + results.unused_types.len());
1723 unused_export_paths.extend(results.unused_exports.iter().map(|e| e.export.path.clone()));
1724 unused_export_paths.extend(results.unused_types.iter().map(|e| e.export.path.clone()));
1725
1726 let mut unused_dep_package_paths = Vec::with_capacity(unused_deps);
1727 unused_dep_package_paths.extend(
1728 results
1729 .unused_dependencies
1730 .iter()
1731 .map(|d| d.dep.path.clone()),
1732 );
1733 unused_dep_package_paths.extend(
1734 results
1735 .unused_dev_dependencies
1736 .iter()
1737 .map(|d| d.dep.path.clone()),
1738 );
1739 unused_dep_package_paths.extend(
1740 results
1741 .unused_optional_dependencies
1742 .iter()
1743 .map(|d| d.dep.path.clone()),
1744 );
1745
1746 AnalysisCountsSnapshot {
1747 unused_file_paths: results
1748 .unused_files
1749 .iter()
1750 .map(|f| f.file.path.clone())
1751 .collect(),
1752 unused_export_paths,
1753 unused_dep_package_paths,
1754 circular_dep_groups: results
1755 .circular_dependencies
1756 .iter()
1757 .map(|c| c.cycle.files.clone())
1758 .collect(),
1759 module_export_counts,
1760 }
1761}
1762
1763#[cfg(test)]
1764mod tests {
1765 use super::*;
1766
1767 #[test]
1768 fn maintainability_perfect_score() {
1769 assert!((compute_maintainability_index(0.0, 0.0, 0, 100) - 100.0).abs() < f64::EPSILON);
1770 }
1771
1772 #[test]
1773 fn crap_resolution_prefers_template_inheritance_over_istanbul() {
1774 let inherit_ctx = TemplateInheritContext {
1775 is_test_reachable: true,
1776 test_referenced_exports: rustc_hash::FxHashSet::default(),
1777 provenance_owner: std::path::PathBuf::from("/project/src/app.component.ts"),
1778 };
1779 let istanbul = IstanbulCoverage {
1780 files: rustc_hash::FxHashMap::default(),
1781 };
1782
1783 let resolution = resolve_crap_coverage(
1784 Some(&inherit_ctx),
1785 Some(&istanbul),
1786 std::path::Path::new("/project/src/app.component.html"),
1787 );
1788
1789 assert!(matches!(
1790 resolution,
1791 CrapCoverageResolution::TemplateInherited(_)
1792 ));
1793 }
1794
1795 #[test]
1796 fn crap_resolution_keeps_istanbul_when_file_is_missing() {
1797 let istanbul = IstanbulCoverage {
1798 files: rustc_hash::FxHashMap::default(),
1799 };
1800
1801 let resolution = resolve_crap_coverage(
1802 None,
1803 Some(&istanbul),
1804 std::path::Path::new("/project/src/missing.ts"),
1805 );
1806
1807 assert!(matches!(
1808 resolution,
1809 CrapCoverageResolution::Istanbul {
1810 file_coverage: None
1811 }
1812 ));
1813 }
1814
1815 #[test]
1816 fn maintainability_clamped_at_zero() {
1817 assert!((compute_maintainability_index(10.0, 1.0, 100, 200) - 0.0).abs() < f64::EPSILON);
1818 }
1819
1820 #[test]
1821 fn maintainability_formula_correct() {
1822 let result = compute_maintainability_index(0.5, 0.3, 10, 100);
1823 let expected = 11.0_f64.ln().mul_add(-4.0, 100.0 - 15.0 - 6.0);
1824 assert!((result - expected).abs() < 0.01);
1825 }
1826
1827 #[test]
1828 fn maintainability_dead_file_penalty() {
1829 let result = compute_maintainability_index(0.0, 1.0, 0, 100);
1830 assert!((result - 80.0).abs() < f64::EPSILON);
1831 }
1832
1833 #[test]
1834 fn maintainability_fan_out_is_logarithmic() {
1835 let result_10 = compute_maintainability_index(0.0, 0.0, 10, 100);
1836 let result_100 = compute_maintainability_index(0.0, 0.0, 100, 100);
1837 let result_200 = compute_maintainability_index(0.0, 0.0, 200, 100);
1838
1839 assert!(result_10 > 90.0); assert!(result_100 > 84.0); assert!((result_100 - result_200).abs() < f64::EPSILON);
1842 }
1843
1844 #[test]
1845 fn maintainability_fan_out_capped_at_15() {
1846 let result = compute_maintainability_index(0.0, 1.0, 1000, 100);
1847 assert!((result - 65.0).abs() < f64::EPSILON);
1848 }
1849
1850 #[test]
1851 fn maintainability_small_file_dampened() {
1852 let small = compute_maintainability_index(0.40, 0.0, 0, 5);
1853 assert!((small - 98.8).abs() < 0.01);
1854 }
1855
1856 #[test]
1857 fn maintainability_large_file_undampened() {
1858 let large = compute_maintainability_index(0.30, 0.0, 0, 192);
1859 assert!((large - 91.0).abs() < 0.01);
1860 }
1861
1862 #[test]
1863 fn maintainability_small_file_ranks_better_than_complex_large_file() {
1864 let trivial = compute_maintainability_index(0.40, 0.0, 0, 5);
1865 let nightmare = compute_maintainability_index(0.30, 0.0, 0, 192);
1866 assert!(
1867 trivial > nightmare,
1868 "trivial file ({trivial}) should rank better than nightmare ({nightmare})"
1869 );
1870 }
1871
1872 #[test]
1873 fn maintainability_at_dampening_boundary() {
1874 let at_boundary = compute_maintainability_index(0.5, 0.0, 0, 50);
1875 let above_boundary = compute_maintainability_index(0.5, 0.0, 0, 51);
1876 assert!((at_boundary - above_boundary).abs() < 0.01);
1877 }
1878
1879 #[test]
1880 fn maintainability_zero_lines_zero_density_penalty() {
1881 let result = compute_maintainability_index(5.0, 0.0, 0, 0);
1882 assert!((result - 100.0).abs() < f64::EPSILON);
1883 }
1884
1885 #[test]
1886 fn complexity_density_zero_lines() {
1887 assert!((compute_complexity_density(10, 0)).abs() < f64::EPSILON);
1888 }
1889
1890 #[test]
1891 fn complexity_density_normal() {
1892 let result = compute_complexity_density(10, 100);
1893 assert!((result - 0.1).abs() < f64::EPSILON);
1894 }
1895
1896 #[test]
1897 fn complexity_density_high() {
1898 let result = compute_complexity_density(50, 10);
1899 assert!((result - 5.0).abs() < f64::EPSILON);
1900 }
1901
1902 #[test]
1903 fn dead_code_ratio_no_exports() {
1904 let unused_files = rustc_hash::FxHashSet::default();
1905 let unused_map = rustc_hash::FxHashMap::default();
1906 let path = std::path::Path::new("/src/foo.ts");
1907 let exports: Vec<fallow_graph::graph::ExportSymbol> = vec![];
1908
1909 let ratio = compute_dead_code_ratio(path, &exports, &unused_files, &unused_map);
1910 assert!((ratio).abs() < f64::EPSILON);
1911 }
1912
1913 #[test]
1914 fn dead_code_ratio_all_unused_file() {
1915 let mut unused_files: rustc_hash::FxHashSet<&std::path::Path> =
1916 rustc_hash::FxHashSet::default();
1917 let path = std::path::Path::new("/src/foo.ts");
1918 unused_files.insert(path);
1919 let unused_map = rustc_hash::FxHashMap::default();
1920 let exports: Vec<fallow_graph::graph::ExportSymbol> = vec![];
1921
1922 let ratio = compute_dead_code_ratio(path, &exports, &unused_files, &unused_map);
1923 assert!((ratio - 1.0).abs() < f64::EPSILON);
1924 }
1925
1926 #[test]
1927 fn dead_code_ratio_mix() {
1928 let unused_files = rustc_hash::FxHashSet::default();
1929 let path = std::path::Path::new("/src/foo.ts");
1930
1931 let exports = vec![
1932 fallow_graph::graph::ExportSymbol {
1933 name: crate::source::ExportName::Named("a".into()),
1934 is_type_only: false,
1935 is_side_effect_used: false,
1936 visibility: crate::source::VisibilityTag::None,
1937 expected_unused_reason: None,
1938 span: oxc_span::Span::empty(0),
1939 references: vec![],
1940 members: vec![],
1941 },
1942 fallow_graph::graph::ExportSymbol {
1943 name: crate::source::ExportName::Named("b".into()),
1944 is_type_only: false,
1945 is_side_effect_used: false,
1946 visibility: crate::source::VisibilityTag::None,
1947 expected_unused_reason: None,
1948 span: oxc_span::Span::empty(0),
1949 references: vec![],
1950 members: vec![],
1951 },
1952 fallow_graph::graph::ExportSymbol {
1953 name: crate::source::ExportName::Named("c".into()),
1954 is_type_only: false,
1955 is_side_effect_used: false,
1956 visibility: crate::source::VisibilityTag::None,
1957 expected_unused_reason: None,
1958 span: oxc_span::Span::empty(0),
1959 references: vec![],
1960 members: vec![],
1961 },
1962 fallow_graph::graph::ExportSymbol {
1963 name: crate::source::ExportName::Named("MyType".into()),
1964 is_type_only: true,
1965 is_side_effect_used: false,
1966 visibility: crate::source::VisibilityTag::None,
1967 expected_unused_reason: None,
1968 span: oxc_span::Span::empty(0),
1969 references: vec![],
1970 members: vec![],
1971 },
1972 ];
1973
1974 let mut unused_map: rustc_hash::FxHashMap<&std::path::Path, usize> =
1975 rustc_hash::FxHashMap::default();
1976 unused_map.insert(path, 2);
1977
1978 let ratio = compute_dead_code_ratio(path, &exports, &unused_files, &unused_map);
1979 assert!((ratio - 2.0 / 3.0).abs() < 1e-10);
1980 }
1981
1982 #[test]
1983 fn dead_code_ratio_all_type_only_exports() {
1984 let unused_files = rustc_hash::FxHashSet::default();
1985 let path = std::path::Path::new("/src/types.ts");
1986
1987 let exports = vec![fallow_graph::graph::ExportSymbol {
1988 name: crate::source::ExportName::Named("Foo".into()),
1989 is_type_only: true,
1990 is_side_effect_used: false,
1991 visibility: crate::source::VisibilityTag::None,
1992 expected_unused_reason: None,
1993 span: oxc_span::Span::empty(0),
1994 references: vec![],
1995 members: vec![],
1996 }];
1997 let unused_map = rustc_hash::FxHashMap::default();
1998
1999 let ratio = compute_dead_code_ratio(path, &exports, &unused_files, &unused_map);
2000 assert!((ratio).abs() < f64::EPSILON);
2001 }
2002
2003 #[test]
2004 fn aggregate_complexity_empty_module() {
2005 let module = crate::source::ModuleInfo {
2006 file_id: crate::discover::FileId(0),
2007 exports: vec![],
2008 imports: vec![],
2009 re_exports: vec![],
2010 dynamic_imports: vec![],
2011 dynamic_import_patterns: vec![],
2012 require_calls: vec![],
2013 package_path_references: Box::default(),
2014 member_accesses: vec![],
2015 semantic_facts: Box::default(),
2016 whole_object_uses: Box::default(),
2017 has_cjs_exports: false,
2018 has_angular_component_template_url: false,
2019 content_hash: 0,
2020 suppressions: vec![],
2021 unknown_suppression_kinds: vec![],
2022 unused_import_bindings: vec![],
2023 type_referenced_import_bindings: vec![],
2024 value_referenced_import_bindings: vec![],
2025 line_offsets: vec![],
2026 complexity: vec![],
2027 flag_uses: vec![],
2028 class_heritage: vec![],
2029 exported_factory_returns: Box::default(),
2030 injection_tokens: vec![],
2031 local_type_declarations: Vec::new(),
2032 public_signature_type_references: Vec::new(),
2033 namespace_object_aliases: Vec::new(),
2034 iconify_prefixes: Vec::new(),
2035 iconify_icon_names: Vec::new(),
2036 auto_import_candidates: Vec::new(),
2037 directives: Vec::new(),
2038 client_only_dynamic_import_spans: Vec::new(),
2039 security_sinks: Vec::new(),
2040 security_sinks_skipped: 0,
2041 security_unresolved_callee_sites: Vec::new(),
2042 tainted_bindings: Vec::new(),
2043 sanitized_sink_args: Vec::new(),
2044 security_control_sites: Vec::new(),
2045 callee_uses: Vec::new(),
2046 misplaced_directives: Vec::new(),
2047 inline_server_action_exports: Vec::new(),
2048 di_key_sites: Vec::new(),
2049 has_dynamic_provide: false,
2050 referenced_import_bindings: Vec::new(),
2051 component_props: Vec::new(),
2052 has_props_attrs_fallthrough: false,
2053 has_define_expose: false,
2054 has_define_model: false,
2055 has_unharvestable_props: false,
2056 component_emits: Vec::new(),
2057 angular_inputs: Vec::new(),
2058 angular_outputs: Vec::new(),
2059 has_unharvestable_emits: false,
2060 has_dynamic_emit: false,
2061 has_emit_whole_object_use: false,
2062 load_return_keys: Vec::new(),
2063 has_unharvestable_load: false,
2064 has_load_data_whole_use: false,
2065 has_page_data_store_whole_use: false,
2066 component_functions: Vec::new(),
2067 react_props: Vec::new(),
2068 hook_uses: Vec::new(),
2069 render_edges: Vec::new(),
2070 svelte_dispatched_events: Vec::new(),
2071 svelte_listened_events: Vec::new(),
2072 angular_component_selectors: Vec::new(),
2073 registered_custom_elements: Vec::new(),
2074 used_custom_element_tags: Vec::new(),
2075 angular_used_selectors: Vec::new(),
2076 angular_entry_component_refs: Vec::new(),
2077 has_dynamic_component_render: false,
2078 has_dynamic_dispatch: false,
2079 };
2080
2081 let (cyc, cog, funcs, lines) = aggregate_complexity(&module);
2082 assert_eq!(cyc, 0);
2083 assert_eq!(cog, 0);
2084 assert_eq!(funcs, 0);
2085 assert_eq!(lines, 0);
2086 }
2087
2088 #[test]
2089 fn aggregate_complexity_single_function() {
2090 let module = crate::source::ModuleInfo {
2091 file_id: crate::discover::FileId(0),
2092 exports: vec![],
2093 imports: vec![],
2094 re_exports: vec![],
2095 dynamic_imports: vec![],
2096 dynamic_import_patterns: vec![],
2097 require_calls: vec![],
2098 package_path_references: Box::default(),
2099 member_accesses: vec![],
2100 semantic_facts: Box::default(),
2101 whole_object_uses: Box::default(),
2102 has_cjs_exports: false,
2103 has_angular_component_template_url: false,
2104 content_hash: 0,
2105 suppressions: vec![],
2106 unknown_suppression_kinds: vec![],
2107 unused_import_bindings: vec![],
2108 type_referenced_import_bindings: vec![],
2109 value_referenced_import_bindings: vec![],
2110 flag_uses: vec![],
2111 class_heritage: vec![],
2112 exported_factory_returns: Box::default(),
2113 injection_tokens: vec![],
2114 local_type_declarations: Vec::new(),
2115 public_signature_type_references: Vec::new(),
2116 namespace_object_aliases: Vec::new(),
2117 iconify_prefixes: Vec::new(),
2118 iconify_icon_names: Vec::new(),
2119 auto_import_candidates: Vec::new(),
2120 directives: Vec::new(),
2121 client_only_dynamic_import_spans: Vec::new(),
2122 security_sinks: Vec::new(),
2123 security_sinks_skipped: 0,
2124 security_unresolved_callee_sites: Vec::new(),
2125 tainted_bindings: Vec::new(),
2126 sanitized_sink_args: Vec::new(),
2127 security_control_sites: Vec::new(),
2128 callee_uses: Vec::new(),
2129 misplaced_directives: Vec::new(),
2130 inline_server_action_exports: Vec::new(),
2131 di_key_sites: Vec::new(),
2132 has_dynamic_provide: false,
2133 referenced_import_bindings: Vec::new(),
2134 component_props: Vec::new(),
2135 has_props_attrs_fallthrough: false,
2136 has_define_expose: false,
2137 has_define_model: false,
2138 has_unharvestable_props: false,
2139 component_emits: Vec::new(),
2140 angular_inputs: Vec::new(),
2141 angular_outputs: Vec::new(),
2142 has_unharvestable_emits: false,
2143 has_dynamic_emit: false,
2144 has_emit_whole_object_use: false,
2145 load_return_keys: Vec::new(),
2146 has_unharvestable_load: false,
2147 has_load_data_whole_use: false,
2148 has_page_data_store_whole_use: false,
2149 component_functions: Vec::new(),
2150 react_props: Vec::new(),
2151 hook_uses: Vec::new(),
2152 render_edges: Vec::new(),
2153 svelte_dispatched_events: Vec::new(),
2154 svelte_listened_events: Vec::new(),
2155 angular_component_selectors: Vec::new(),
2156 registered_custom_elements: Vec::new(),
2157 used_custom_element_tags: Vec::new(),
2158 angular_used_selectors: Vec::new(),
2159 angular_entry_component_refs: Vec::new(),
2160 has_dynamic_component_render: false,
2161 has_dynamic_dispatch: false,
2162 line_offsets: vec![0, 10, 20, 30, 40], complexity: vec![fallow_types::extract::FunctionComplexity {
2164 name: "doStuff".into(),
2165 line: 1,
2166 col: 0,
2167 cyclomatic: 7,
2168 cognitive: 4,
2169 line_count: 5,
2170 param_count: 0,
2171 react_hook_count: 0,
2172 react_jsx_max_depth: 0,
2173 react_prop_count: 0,
2174 source_hash: None,
2175 contributions: Vec::new(),
2176 }],
2177 };
2178
2179 let (cyc, cog, funcs, lines) = aggregate_complexity(&module);
2180 assert_eq!(cyc, 7);
2181 assert_eq!(cog, 4);
2182 assert_eq!(funcs, 1);
2183 assert_eq!(lines, 5);
2184 }
2185
2186 #[test]
2187 #[expect(
2188 clippy::too_many_lines,
2189 reason = "test fixture; linear setup/assert, length is not a maintainability concern"
2190 )]
2191 fn aggregate_complexity_multiple_functions() {
2192 let module = crate::source::ModuleInfo {
2193 file_id: crate::discover::FileId(0),
2194 exports: vec![],
2195 imports: vec![],
2196 re_exports: vec![],
2197 dynamic_imports: vec![],
2198 dynamic_import_patterns: vec![],
2199 require_calls: vec![],
2200 package_path_references: Box::default(),
2201 member_accesses: vec![],
2202 semantic_facts: Box::default(),
2203 whole_object_uses: Box::default(),
2204 has_cjs_exports: false,
2205 has_angular_component_template_url: false,
2206 content_hash: 0,
2207 suppressions: vec![],
2208 unknown_suppression_kinds: vec![],
2209 unused_import_bindings: vec![],
2210 type_referenced_import_bindings: vec![],
2211 value_referenced_import_bindings: vec![],
2212 flag_uses: vec![],
2213 class_heritage: vec![],
2214 exported_factory_returns: Box::default(),
2215 injection_tokens: vec![],
2216 local_type_declarations: Vec::new(),
2217 public_signature_type_references: Vec::new(),
2218 namespace_object_aliases: Vec::new(),
2219 iconify_prefixes: Vec::new(),
2220 iconify_icon_names: Vec::new(),
2221 auto_import_candidates: Vec::new(),
2222 directives: Vec::new(),
2223 client_only_dynamic_import_spans: Vec::new(),
2224 security_sinks: Vec::new(),
2225 security_sinks_skipped: 0,
2226 security_unresolved_callee_sites: Vec::new(),
2227 tainted_bindings: Vec::new(),
2228 sanitized_sink_args: Vec::new(),
2229 security_control_sites: Vec::new(),
2230 callee_uses: Vec::new(),
2231 misplaced_directives: Vec::new(),
2232 inline_server_action_exports: Vec::new(),
2233 di_key_sites: Vec::new(),
2234 has_dynamic_provide: false,
2235 referenced_import_bindings: Vec::new(),
2236 component_props: Vec::new(),
2237 has_props_attrs_fallthrough: false,
2238 has_define_expose: false,
2239 has_define_model: false,
2240 has_unharvestable_props: false,
2241 component_emits: Vec::new(),
2242 angular_inputs: Vec::new(),
2243 angular_outputs: Vec::new(),
2244 has_unharvestable_emits: false,
2245 has_dynamic_emit: false,
2246 has_emit_whole_object_use: false,
2247 load_return_keys: Vec::new(),
2248 has_unharvestable_load: false,
2249 has_load_data_whole_use: false,
2250 has_page_data_store_whole_use: false,
2251 component_functions: Vec::new(),
2252 react_props: Vec::new(),
2253 hook_uses: Vec::new(),
2254 render_edges: Vec::new(),
2255 svelte_dispatched_events: Vec::new(),
2256 svelte_listened_events: Vec::new(),
2257 angular_component_selectors: Vec::new(),
2258 registered_custom_elements: Vec::new(),
2259 used_custom_element_tags: Vec::new(),
2260 angular_used_selectors: Vec::new(),
2261 angular_entry_component_refs: Vec::new(),
2262 has_dynamic_component_render: false,
2263 has_dynamic_dispatch: false,
2264 line_offsets: vec![0, 10, 20], complexity: vec![
2266 fallow_types::extract::FunctionComplexity {
2267 name: "a".into(),
2268 line: 1,
2269 col: 0,
2270 cyclomatic: 3,
2271 cognitive: 2,
2272 line_count: 1,
2273 param_count: 0,
2274 react_hook_count: 0,
2275 react_jsx_max_depth: 0,
2276 react_prop_count: 0,
2277 source_hash: None,
2278 contributions: Vec::new(),
2279 },
2280 fallow_types::extract::FunctionComplexity {
2281 name: "b".into(),
2282 line: 2,
2283 col: 0,
2284 cyclomatic: 5,
2285 cognitive: 8,
2286 line_count: 2,
2287 param_count: 0,
2288 react_hook_count: 0,
2289 react_jsx_max_depth: 0,
2290 react_prop_count: 0,
2291 source_hash: None,
2292 contributions: Vec::new(),
2293 },
2294 ],
2295 };
2296
2297 let (cyc, cog, funcs, lines) = aggregate_complexity(&module);
2298 assert_eq!(cyc, 8);
2299 assert_eq!(cog, 10);
2300 assert_eq!(funcs, 2);
2301 assert_eq!(lines, 3);
2302 }
2303
2304 #[test]
2305 fn count_unused_exports_empty() {
2306 let exports: Vec<crate::results::UnusedExportFinding> = vec![];
2307 let map = count_unused_exports_by_path(&exports);
2308 assert!(map.is_empty());
2309 }
2310
2311 #[test]
2312 fn count_unused_exports_groups_by_path() {
2313 let exports = vec![
2314 crate::results::UnusedExportFinding::with_actions(crate::results::UnusedExport {
2315 path: std::path::PathBuf::from("/src/a.ts"),
2316 export_name: "foo".into(),
2317 is_type_only: false,
2318 line: 1,
2319 col: 0,
2320 span_start: 0,
2321 is_re_export: false,
2322 }),
2323 crate::results::UnusedExportFinding::with_actions(crate::results::UnusedExport {
2324 path: std::path::PathBuf::from("/src/a.ts"),
2325 export_name: "bar".into(),
2326 is_type_only: false,
2327 line: 5,
2328 col: 0,
2329 span_start: 40,
2330 is_re_export: false,
2331 }),
2332 crate::results::UnusedExportFinding::with_actions(crate::results::UnusedExport {
2333 path: std::path::PathBuf::from("/src/b.ts"),
2334 export_name: "baz".into(),
2335 is_type_only: false,
2336 line: 1,
2337 col: 0,
2338 span_start: 0,
2339 is_re_export: false,
2340 }),
2341 ];
2342 let map = count_unused_exports_by_path(&exports);
2343 assert_eq!(map.get(std::path::Path::new("/src/a.ts")).copied(), Some(2));
2344 assert_eq!(map.get(std::path::Path::new("/src/b.ts")).copied(), Some(1));
2345 }
2346
2347 #[test]
2348 fn dead_code_ratio_all_value_exports_unused() {
2349 let unused_files = rustc_hash::FxHashSet::default();
2350 let path = std::path::Path::new("/src/foo.ts");
2351
2352 let exports = vec![
2353 fallow_graph::graph::ExportSymbol {
2354 name: crate::source::ExportName::Named("a".into()),
2355 is_type_only: false,
2356 is_side_effect_used: false,
2357 visibility: crate::source::VisibilityTag::None,
2358 expected_unused_reason: None,
2359 span: oxc_span::Span::empty(0),
2360 references: vec![],
2361 members: vec![],
2362 },
2363 fallow_graph::graph::ExportSymbol {
2364 name: crate::source::ExportName::Named("b".into()),
2365 is_type_only: false,
2366 is_side_effect_used: false,
2367 visibility: crate::source::VisibilityTag::None,
2368 expected_unused_reason: None,
2369 span: oxc_span::Span::empty(0),
2370 references: vec![],
2371 members: vec![],
2372 },
2373 ];
2374
2375 let mut unused_map: rustc_hash::FxHashMap<&std::path::Path, usize> =
2376 rustc_hash::FxHashMap::default();
2377 unused_map.insert(path, 2);
2378
2379 let ratio = compute_dead_code_ratio(path, &exports, &unused_files, &unused_map);
2380 assert!((ratio - 1.0).abs() < f64::EPSILON);
2381 }
2382
2383 #[test]
2384 fn dead_code_ratio_clamped_when_unused_exceeds_value_exports() {
2385 let unused_files = rustc_hash::FxHashSet::default();
2386 let path = std::path::Path::new("/src/foo.ts");
2387
2388 let exports = vec![fallow_graph::graph::ExportSymbol {
2389 name: crate::source::ExportName::Named("a".into()),
2390 is_type_only: false,
2391 is_side_effect_used: false,
2392 visibility: crate::source::VisibilityTag::None,
2393 expected_unused_reason: None,
2394 span: oxc_span::Span::empty(0),
2395 references: vec![],
2396 members: vec![],
2397 }];
2398
2399 let mut unused_map: rustc_hash::FxHashMap<&std::path::Path, usize> =
2400 rustc_hash::FxHashMap::default();
2401 unused_map.insert(path, 5);
2402
2403 let ratio = compute_dead_code_ratio(path, &exports, &unused_files, &unused_map);
2404 assert!((ratio - 1.0).abs() < f64::EPSILON);
2405 }
2406
2407 #[test]
2408 fn dead_code_ratio_no_unused_exports_for_path() {
2409 let unused_files = rustc_hash::FxHashSet::default();
2410 let path = std::path::Path::new("/src/clean.ts");
2411
2412 let exports = vec![fallow_graph::graph::ExportSymbol {
2413 name: crate::source::ExportName::Named("used".into()),
2414 is_type_only: false,
2415 is_side_effect_used: false,
2416 visibility: crate::source::VisibilityTag::None,
2417 expected_unused_reason: None,
2418 span: oxc_span::Span::empty(0),
2419 references: vec![],
2420 members: vec![],
2421 }];
2422
2423 let unused_map = rustc_hash::FxHashMap::default();
2424 let ratio = compute_dead_code_ratio(path, &exports, &unused_files, &unused_map);
2425 assert!(ratio.abs() < f64::EPSILON);
2426 }
2427
2428 #[test]
2429 fn complexity_density_zero_cyclomatic_with_lines() {
2430 let result = compute_complexity_density(0, 100);
2431 assert!(result.abs() < f64::EPSILON);
2432 }
2433
2434 #[test]
2435 fn complexity_density_single_line() {
2436 let result = compute_complexity_density(1, 1);
2437 assert!((result - 1.0).abs() < f64::EPSILON);
2438 }
2439
2440 #[test]
2441 fn maintainability_only_complexity_penalty() {
2442 let result = compute_maintainability_index(3.0, 0.0, 0, 100);
2443 assert!((result - 10.0).abs() < f64::EPSILON);
2444 }
2445
2446 #[test]
2447 fn maintainability_only_dead_code_penalty() {
2448 let result = compute_maintainability_index(0.0, 0.5, 0, 100);
2449 assert!((result - 90.0).abs() < f64::EPSILON);
2450 }
2451
2452 #[test]
2453 fn maintainability_fan_out_one() {
2454 let result = compute_maintainability_index(0.0, 0.0, 1, 100);
2455 let expected = 2.0_f64.ln().mul_add(-4.0, 100.0);
2456 assert!((result - expected).abs() < 0.01);
2457 }
2458
2459 #[test]
2460 fn maintainability_all_penalties_maxed() {
2461 let result = compute_maintainability_index(10.0, 1.0, 1000, 200);
2462 assert!(result.abs() < f64::EPSILON);
2463 }
2464
2465 #[test]
2466 fn count_unused_exports_single_file_single_export() {
2467 let exports = vec![crate::results::UnusedExportFinding::with_actions(
2468 crate::results::UnusedExport {
2469 path: std::path::PathBuf::from("/src/only.ts"),
2470 export_name: "lonely".into(),
2471 is_type_only: false,
2472 line: 1,
2473 col: 0,
2474 span_start: 0,
2475 is_re_export: false,
2476 },
2477 )];
2478 let map = count_unused_exports_by_path(&exports);
2479 assert_eq!(map.len(), 1);
2480 assert_eq!(
2481 map.get(std::path::Path::new("/src/only.ts")).copied(),
2482 Some(1)
2483 );
2484 }
2485
2486 fn build_test_graph(
2488 files: &[crate::discover::DiscoveredFile],
2489 entry_point_paths: &[std::path::PathBuf],
2490 resolved_modules: &[fallow_graph::resolve::ResolvedModule],
2491 ) -> fallow_graph::graph::ModuleGraph {
2492 let entry_points: Vec<crate::discover::EntryPoint> = entry_point_paths
2493 .iter()
2494 .map(|p| crate::discover::EntryPoint {
2495 path: p.clone(),
2496 source: crate::discover::EntryPointSource::PackageJsonMain,
2497 })
2498 .collect();
2499 fallow_graph::graph::ModuleGraph::build(resolved_modules, &entry_points, files)
2500 }
2501
2502 fn make_module_info(
2504 file_id: u32,
2505 line_count: usize,
2506 functions: Vec<fallow_types::extract::FunctionComplexity>,
2507 ) -> crate::source::ModuleInfo {
2508 crate::source::ModuleInfo {
2509 file_id: crate::discover::FileId(file_id),
2510 exports: vec![],
2511 imports: vec![],
2512 re_exports: vec![],
2513 dynamic_imports: vec![],
2514 dynamic_import_patterns: vec![],
2515 require_calls: vec![],
2516 package_path_references: Box::default(),
2517 member_accesses: vec![],
2518 semantic_facts: Box::default(),
2519 whole_object_uses: Box::default(),
2520 has_cjs_exports: false,
2521 has_angular_component_template_url: false,
2522 content_hash: 0,
2523 suppressions: vec![],
2524 unknown_suppression_kinds: vec![],
2525 unused_import_bindings: vec![],
2526 type_referenced_import_bindings: vec![],
2527 value_referenced_import_bindings: vec![],
2528 line_offsets: (0..line_count).map(|i| (i * 10) as u32).collect(),
2529 complexity: functions,
2530 flag_uses: vec![],
2531 class_heritage: vec![],
2532 exported_factory_returns: Box::default(),
2533 injection_tokens: vec![],
2534 local_type_declarations: Vec::new(),
2535 public_signature_type_references: Vec::new(),
2536 namespace_object_aliases: Vec::new(),
2537 iconify_prefixes: Vec::new(),
2538 iconify_icon_names: Vec::new(),
2539 auto_import_candidates: Vec::new(),
2540 directives: Vec::new(),
2541 client_only_dynamic_import_spans: Vec::new(),
2542 security_sinks: Vec::new(),
2543 security_sinks_skipped: 0,
2544 security_unresolved_callee_sites: Vec::new(),
2545 tainted_bindings: Vec::new(),
2546 sanitized_sink_args: Vec::new(),
2547 security_control_sites: Vec::new(),
2548 callee_uses: Vec::new(),
2549 misplaced_directives: Vec::new(),
2550 inline_server_action_exports: Vec::new(),
2551 di_key_sites: Vec::new(),
2552 has_dynamic_provide: false,
2553 referenced_import_bindings: Vec::new(),
2554 component_props: Vec::new(),
2555 has_props_attrs_fallthrough: false,
2556 has_define_expose: false,
2557 has_define_model: false,
2558 has_unharvestable_props: false,
2559 component_emits: Vec::new(),
2560 angular_inputs: Vec::new(),
2561 angular_outputs: Vec::new(),
2562 has_unharvestable_emits: false,
2563 has_dynamic_emit: false,
2564 has_emit_whole_object_use: false,
2565 load_return_keys: Vec::new(),
2566 has_unharvestable_load: false,
2567 has_load_data_whole_use: false,
2568 has_page_data_store_whole_use: false,
2569 component_functions: Vec::new(),
2570 react_props: Vec::new(),
2571 hook_uses: Vec::new(),
2572 render_edges: Vec::new(),
2573 svelte_dispatched_events: Vec::new(),
2574 svelte_listened_events: Vec::new(),
2575 angular_component_selectors: Vec::new(),
2576 registered_custom_elements: Vec::new(),
2577 used_custom_element_tags: Vec::new(),
2578 angular_used_selectors: Vec::new(),
2579 angular_entry_component_refs: Vec::new(),
2580 has_dynamic_component_render: false,
2581 has_dynamic_dispatch: false,
2582 }
2583 }
2584
2585 fn make_file_score(path: &str, maintainability_index: f64, crap_max: f64) -> FileHealthScore {
2586 FileHealthScore {
2587 path: std::path::PathBuf::from(path),
2588 fan_in: 0,
2589 fan_out: 0,
2590 dead_code_ratio: 0.0,
2591 complexity_density: 0.0,
2592 maintainability_index,
2593 total_cyclomatic: 0,
2594 total_cognitive: 0,
2595 function_count: 1,
2596 lines: 1,
2597 crap_max,
2598 crap_above_threshold: usize::from(crap_max >= CRAP_THRESHOLD),
2599 }
2600 }
2601
2602 #[test]
2603 fn file_score_crap_concern_tracks_crap_risk_bands() {
2604 assert!((file_score_crap_concern(0.0) - 0.0).abs() < f64::EPSILON);
2605 assert!((file_score_crap_concern(15.0) - 45.0).abs() < f64::EPSILON);
2606 assert!((file_score_crap_concern(CRAP_THRESHOLD) - 75.0).abs() < f64::EPSILON);
2607 assert!((file_score_crap_concern(100.0) - 100.0).abs() < f64::EPSILON);
2608 assert!((file_score_crap_concern(552.0) - 100.0).abs() < f64::EPSILON);
2609 }
2610
2611 #[test]
2612 fn file_score_concern_axis_labels_dominant_signal() {
2613 let risk_driven = make_file_score("/src/risk.ts", 84.8, 552.0);
2614 assert_eq!(
2615 file_score_concern_axis(&risk_driven),
2616 FileScoreConcern::Risk
2617 );
2618 assert_eq!(file_score_concern_axis(&risk_driven).label(), "risk");
2619
2620 let structure_driven = make_file_score("/src/structure.ts", 30.0, 8.0);
2621 assert_eq!(
2622 file_score_concern_axis(&structure_driven),
2623 FileScoreConcern::Structural
2624 );
2625 assert_eq!(
2626 file_score_concern_axis(&structure_driven).label(),
2627 "structure"
2628 );
2629
2630 let no_risk = make_file_score("/src/clean.ts", 100.0, 0.0);
2631 assert_eq!(
2632 file_score_concern_axis(&no_risk),
2633 FileScoreConcern::Structural
2634 );
2635 }
2636
2637 #[test]
2638 fn file_score_triage_sort_prioritizes_high_crap_over_slightly_lower_mi() {
2639 let low_mi_low_risk = make_file_score("/src/low-mi-low-risk.ts", 81.7, 2.0);
2640 let higher_mi_high_risk = make_file_score("/src/higher-mi-high-risk.ts", 84.8, 552.0);
2641
2642 let mut scores = [low_mi_low_risk, higher_mi_high_risk];
2643 scores.sort_by(compare_file_score_triage);
2644
2645 assert_eq!(
2646 scores[0].path,
2647 std::path::Path::new("/src/higher-mi-high-risk.ts")
2648 );
2649 assert_eq!(
2650 scores[1].path,
2651 std::path::Path::new("/src/low-mi-low-risk.ts")
2652 );
2653 }
2654
2655 #[test]
2656 fn file_score_triage_sort_orders_saturated_crap_by_raw_crap_descending() {
2657 let lower_crap_worse_mi = make_file_score("/src/a.ts", 84.8, 106.0);
2658 let higher_crap_better_mi = make_file_score("/src/b.ts", 96.7, 552.0);
2659
2660 let mut scores = [lower_crap_worse_mi, higher_crap_better_mi];
2661 scores.sort_by(compare_file_score_triage);
2662
2663 assert_eq!(scores[0].path, std::path::Path::new("/src/b.ts"));
2664 assert_eq!(scores[1].path, std::path::Path::new("/src/a.ts"));
2665 }
2666
2667 #[test]
2668 fn file_score_triage_sort_uses_mi_crap_and_path_tie_breakers() {
2669 let mut scores = [
2670 make_file_score("/src/b.ts", 70.0, 1.0),
2671 make_file_score("/src/a.ts", 70.0, 1.0),
2672 make_file_score("/src/higher-crap.ts", 70.0, 2.0),
2673 make_file_score("/src/lower-concern.ts", 80.0, 1.0),
2674 ];
2675
2676 scores.sort_by(compare_file_score_triage);
2677
2678 let paths: Vec<_> = scores.iter().map(|score| score.path.as_path()).collect();
2679 assert_eq!(
2680 paths,
2681 vec![
2682 std::path::Path::new("/src/higher-crap.ts"),
2683 std::path::Path::new("/src/a.ts"),
2684 std::path::Path::new("/src/b.ts"),
2685 std::path::Path::new("/src/lower-concern.ts"),
2686 ]
2687 );
2688 }
2689
2690 #[test]
2691 fn compute_file_scores_empty_graph() {
2692 let files: Vec<crate::discover::DiscoveredFile> = vec![];
2693 let graph = build_test_graph(&files, &[], &[]);
2694 let modules: Vec<crate::source::ModuleInfo> = vec![];
2695 let file_paths = rustc_hash::FxHashMap::default();
2696
2697 let output = crate::results::DeadCodeAnalysisArtifacts {
2698 results: fallow_types::results::AnalysisResults::default(),
2699 timings: None,
2700 graph: Some(crate::module_graph::RetainedModuleGraph::from(graph)),
2701 modules: None,
2702 files: None,
2703 script_used_packages: rustc_hash::FxHashSet::default(),
2704 file_hashes: rustc_hash::FxHashMap::default(),
2705 };
2706
2707 let result = compute_file_scores(
2708 &modules,
2709 &file_paths,
2710 None,
2711 output,
2712 None,
2713 std::path::Path::new("/project"),
2714 )
2715 .unwrap();
2716 assert!(result.scores.is_empty());
2717 assert!(result.circular_files.is_empty());
2718 assert!(result.top_complex_fns.is_empty());
2719 assert!(result.entry_points.is_empty());
2720 assert_eq!(result.analysis_counts.total_exports, 0);
2721 assert_eq!(result.analysis_counts.dead_files, 0);
2722 }
2723
2724 #[test]
2725 fn compute_file_scores_no_graph_returns_error() {
2726 let modules: Vec<crate::source::ModuleInfo> = vec![];
2727 let file_paths = rustc_hash::FxHashMap::default();
2728
2729 let output = crate::results::DeadCodeAnalysisArtifacts {
2730 results: fallow_types::results::AnalysisResults::default(),
2731 timings: None,
2732 graph: None,
2733 modules: None,
2734 files: None,
2735 script_used_packages: rustc_hash::FxHashSet::default(),
2736 file_hashes: rustc_hash::FxHashMap::default(),
2737 };
2738
2739 let result = compute_file_scores(
2740 &modules,
2741 &file_paths,
2742 None,
2743 output,
2744 None,
2745 std::path::Path::new("/project"),
2746 );
2747 assert!(result.is_err());
2748 match result {
2749 Err(msg) => assert_eq!(msg, "graph not available"),
2750 Ok(_) => panic!("expected error"),
2751 }
2752 }
2753
2754 #[test]
2755 fn compute_file_scores_single_file_with_function() {
2756 let path_a = std::path::PathBuf::from("/src/a.ts");
2757 let files = vec![crate::discover::DiscoveredFile {
2758 id: crate::discover::FileId(0),
2759 path: path_a.clone(),
2760 size_bytes: 100,
2761 }];
2762
2763 let resolved_modules = vec![fallow_graph::resolve::ResolvedModule {
2764 file_id: crate::discover::FileId(0),
2765 path: path_a.clone(),
2766 exports: vec![fallow_types::extract::ExportInfo {
2767 name: crate::source::ExportName::Named("foo".into()),
2768 local_name: None,
2769 is_type_only: false,
2770 visibility: crate::source::VisibilityTag::None,
2771 expected_unused_reason: None,
2772 span: oxc_span::Span::empty(0),
2773 members: vec![],
2774 is_side_effect_used: false,
2775 super_class: None,
2776 }],
2777 ..Default::default()
2778 }];
2779
2780 let graph = build_test_graph(&files, std::slice::from_ref(&path_a), &resolved_modules);
2781
2782 let modules = vec![make_module_info(
2783 0,
2784 10,
2785 vec![fallow_types::extract::FunctionComplexity {
2786 name: "foo".into(),
2787 line: 1,
2788 col: 0,
2789 cyclomatic: 5,
2790 cognitive: 3,
2791 line_count: 10,
2792 param_count: 0,
2793 react_hook_count: 0,
2794 react_jsx_max_depth: 0,
2795 react_prop_count: 0,
2796 source_hash: None,
2797 contributions: Vec::new(),
2798 }],
2799 )];
2800
2801 let mut file_paths: rustc_hash::FxHashMap<crate::discover::FileId, &std::path::PathBuf> =
2802 rustc_hash::FxHashMap::default();
2803 file_paths.insert(crate::discover::FileId(0), &files[0].path);
2804
2805 let output = crate::results::DeadCodeAnalysisArtifacts {
2806 results: fallow_types::results::AnalysisResults::default(),
2807 timings: None,
2808 graph: Some(crate::module_graph::RetainedModuleGraph::from(graph)),
2809 modules: None,
2810 files: None,
2811 script_used_packages: rustc_hash::FxHashSet::default(),
2812 file_hashes: rustc_hash::FxHashMap::default(),
2813 };
2814
2815 let result = compute_file_scores(
2816 &modules,
2817 &file_paths,
2818 None,
2819 output,
2820 None,
2821 std::path::Path::new("/project"),
2822 )
2823 .unwrap();
2824 assert_eq!(result.scores.len(), 1);
2825
2826 let score = &result.scores[0];
2827 assert_eq!(score.path, path_a);
2828 assert_eq!(score.total_cyclomatic, 5);
2829 assert_eq!(score.total_cognitive, 3);
2830 assert_eq!(score.function_count, 1);
2831 assert_eq!(score.lines, 10);
2832 assert!((score.complexity_density - 0.5).abs() < f64::EPSILON);
2833 assert!(score.dead_code_ratio.abs() < f64::EPSILON);
2834 assert!(result.entry_points.contains(&path_a));
2835 }
2836
2837 #[test]
2838 fn compute_file_scores_excludes_barrel_files() {
2839 let path_a = std::path::PathBuf::from("/src/index.ts");
2840 let files = vec![crate::discover::DiscoveredFile {
2841 id: crate::discover::FileId(0),
2842 path: path_a.clone(),
2843 size_bytes: 50,
2844 }];
2845
2846 let resolved_modules = vec![fallow_graph::resolve::ResolvedModule {
2847 file_id: crate::discover::FileId(0),
2848 path: path_a.clone(),
2849 ..Default::default()
2850 }];
2851
2852 let graph = build_test_graph(&files, std::slice::from_ref(&path_a), &resolved_modules);
2853
2854 let modules = vec![make_module_info(0, 5, vec![])];
2855
2856 let mut file_paths: rustc_hash::FxHashMap<crate::discover::FileId, &std::path::PathBuf> =
2857 rustc_hash::FxHashMap::default();
2858 file_paths.insert(crate::discover::FileId(0), &files[0].path);
2859
2860 let output = crate::results::DeadCodeAnalysisArtifacts {
2861 results: fallow_types::results::AnalysisResults::default(),
2862 timings: None,
2863 graph: Some(crate::module_graph::RetainedModuleGraph::from(graph)),
2864 modules: None,
2865 files: None,
2866 script_used_packages: rustc_hash::FxHashSet::default(),
2867 file_hashes: rustc_hash::FxHashMap::default(),
2868 };
2869
2870 let result = compute_file_scores(
2871 &modules,
2872 &file_paths,
2873 None,
2874 output,
2875 None,
2876 std::path::Path::new("/project"),
2877 )
2878 .unwrap();
2879 assert!(result.scores.is_empty());
2880 }
2881
2882 #[test]
2883 fn compute_file_scores_changed_since_filter() {
2884 let path_a = std::path::PathBuf::from("/src/a.ts");
2885 let path_b = std::path::PathBuf::from("/src/b.ts");
2886 let files = vec![
2887 crate::discover::DiscoveredFile {
2888 id: crate::discover::FileId(0),
2889 path: path_a.clone(),
2890 size_bytes: 100,
2891 },
2892 crate::discover::DiscoveredFile {
2893 id: crate::discover::FileId(1),
2894 path: path_b.clone(),
2895 size_bytes: 100,
2896 },
2897 ];
2898
2899 let resolved_modules = vec![
2900 fallow_graph::resolve::ResolvedModule {
2901 file_id: crate::discover::FileId(0),
2902 path: path_a,
2903 ..Default::default()
2904 },
2905 fallow_graph::resolve::ResolvedModule {
2906 file_id: crate::discover::FileId(1),
2907 path: path_b.clone(),
2908 ..Default::default()
2909 },
2910 ];
2911
2912 let graph = build_test_graph(&files, &[], &resolved_modules);
2913
2914 let modules = vec![
2915 make_module_info(
2916 0,
2917 10,
2918 vec![fallow_types::extract::FunctionComplexity {
2919 name: "fn_a".into(),
2920 line: 1,
2921 col: 0,
2922 cyclomatic: 2,
2923 cognitive: 1,
2924 line_count: 10,
2925 param_count: 0,
2926 react_hook_count: 0,
2927 react_jsx_max_depth: 0,
2928 react_prop_count: 0,
2929 source_hash: None,
2930 contributions: Vec::new(),
2931 }],
2932 ),
2933 make_module_info(
2934 1,
2935 10,
2936 vec![fallow_types::extract::FunctionComplexity {
2937 name: "fn_b".into(),
2938 line: 1,
2939 col: 0,
2940 cyclomatic: 3,
2941 cognitive: 2,
2942 line_count: 10,
2943 param_count: 0,
2944 react_hook_count: 0,
2945 react_jsx_max_depth: 0,
2946 react_prop_count: 0,
2947 source_hash: None,
2948 contributions: Vec::new(),
2949 }],
2950 ),
2951 ];
2952
2953 let mut file_paths: rustc_hash::FxHashMap<crate::discover::FileId, &std::path::PathBuf> =
2954 rustc_hash::FxHashMap::default();
2955 file_paths.insert(crate::discover::FileId(0), &files[0].path);
2956 file_paths.insert(crate::discover::FileId(1), &files[1].path);
2957
2958 let path_b_check = std::path::PathBuf::from("/src/b.ts");
2959 let mut changed = rustc_hash::FxHashSet::default();
2960 changed.insert(path_b);
2961
2962 let output = crate::results::DeadCodeAnalysisArtifacts {
2963 results: fallow_types::results::AnalysisResults::default(),
2964 timings: None,
2965 graph: Some(crate::module_graph::RetainedModuleGraph::from(graph)),
2966 modules: None,
2967 files: None,
2968 script_used_packages: rustc_hash::FxHashSet::default(),
2969 file_hashes: rustc_hash::FxHashMap::default(),
2970 };
2971
2972 let result = compute_file_scores(
2973 &modules,
2974 &file_paths,
2975 Some(&changed),
2976 output,
2977 None,
2978 std::path::Path::new("/project"),
2979 )
2980 .unwrap();
2981 assert_eq!(result.scores.len(), 1);
2982 assert_eq!(result.scores[0].path, path_b_check);
2983 }
2984
2985 #[test]
2986 fn compute_file_scores_sorted_by_triage_concern() {
2987 let path_a = std::path::PathBuf::from("/src/a.ts");
2988 let path_b = std::path::PathBuf::from("/src/b.ts");
2989 let files = vec![
2990 crate::discover::DiscoveredFile {
2991 id: crate::discover::FileId(0),
2992 path: path_a.clone(),
2993 size_bytes: 100,
2994 },
2995 crate::discover::DiscoveredFile {
2996 id: crate::discover::FileId(1),
2997 path: path_b.clone(),
2998 size_bytes: 100,
2999 },
3000 ];
3001
3002 let resolved_modules = vec![
3003 fallow_graph::resolve::ResolvedModule {
3004 file_id: crate::discover::FileId(0),
3005 path: path_a.clone(),
3006 ..Default::default()
3007 },
3008 fallow_graph::resolve::ResolvedModule {
3009 file_id: crate::discover::FileId(1),
3010 path: path_b,
3011 ..Default::default()
3012 },
3013 ];
3014
3015 let graph = build_test_graph(&files, &[], &resolved_modules);
3016
3017 let modules = vec![
3018 make_module_info(
3019 0,
3020 10,
3021 vec![fallow_types::extract::FunctionComplexity {
3022 name: "complex_fn".into(),
3023 line: 1,
3024 col: 0,
3025 cyclomatic: 30,
3026 cognitive: 20,
3027 line_count: 10,
3028 param_count: 0,
3029 react_hook_count: 0,
3030 react_jsx_max_depth: 0,
3031 react_prop_count: 0,
3032 source_hash: None,
3033 contributions: Vec::new(),
3034 }],
3035 ),
3036 make_module_info(
3037 1,
3038 100,
3039 vec![fallow_types::extract::FunctionComplexity {
3040 name: "simple_fn".into(),
3041 line: 1,
3042 col: 0,
3043 cyclomatic: 1,
3044 cognitive: 0,
3045 line_count: 100,
3046 param_count: 0,
3047 react_hook_count: 0,
3048 react_jsx_max_depth: 0,
3049 react_prop_count: 0,
3050 source_hash: None,
3051 contributions: Vec::new(),
3052 }],
3053 ),
3054 ];
3055
3056 let mut file_paths: rustc_hash::FxHashMap<crate::discover::FileId, &std::path::PathBuf> =
3057 rustc_hash::FxHashMap::default();
3058 file_paths.insert(crate::discover::FileId(0), &files[0].path);
3059 file_paths.insert(crate::discover::FileId(1), &files[1].path);
3060
3061 let output = crate::results::DeadCodeAnalysisArtifacts {
3062 results: fallow_types::results::AnalysisResults::default(),
3063 timings: None,
3064 graph: Some(crate::module_graph::RetainedModuleGraph::from(graph)),
3065 modules: None,
3066 files: None,
3067 script_used_packages: rustc_hash::FxHashSet::default(),
3068 file_hashes: rustc_hash::FxHashMap::default(),
3069 };
3070
3071 let result = compute_file_scores(
3072 &modules,
3073 &file_paths,
3074 None,
3075 output,
3076 None,
3077 std::path::Path::new("/project"),
3078 )
3079 .unwrap();
3080 assert_eq!(result.scores.len(), 2);
3081 assert!(result.scores[0].maintainability_index <= result.scores[1].maintainability_index);
3082 assert_eq!(result.scores[0].path, path_a);
3083 }
3084
3085 #[test]
3086 fn compute_file_scores_with_unused_file_populates_evidence() {
3087 let path_a = std::path::PathBuf::from("/src/unused.ts");
3088 let files = vec![crate::discover::DiscoveredFile {
3089 id: crate::discover::FileId(0),
3090 path: path_a.clone(),
3091 size_bytes: 100,
3092 }];
3093
3094 let resolved_modules = vec![fallow_graph::resolve::ResolvedModule {
3095 file_id: crate::discover::FileId(0),
3096 path: path_a.clone(),
3097 exports: vec![fallow_types::extract::ExportInfo {
3098 name: crate::source::ExportName::Named("orphan".into()),
3099 local_name: None,
3100 is_type_only: false,
3101 visibility: crate::source::VisibilityTag::None,
3102 expected_unused_reason: None,
3103 span: oxc_span::Span::empty(0),
3104 members: vec![],
3105 is_side_effect_used: false,
3106 super_class: None,
3107 }],
3108 ..Default::default()
3109 }];
3110
3111 let graph = build_test_graph(&files, &[], &resolved_modules);
3112
3113 let modules = vec![make_module_info(
3114 0,
3115 10,
3116 vec![fallow_types::extract::FunctionComplexity {
3117 name: "orphan".into(),
3118 line: 1,
3119 col: 0,
3120 cyclomatic: 1,
3121 cognitive: 0,
3122 line_count: 10,
3123 param_count: 0,
3124 react_hook_count: 0,
3125 react_jsx_max_depth: 0,
3126 react_prop_count: 0,
3127 source_hash: None,
3128 contributions: Vec::new(),
3129 }],
3130 )];
3131
3132 let mut file_paths: rustc_hash::FxHashMap<crate::discover::FileId, &std::path::PathBuf> =
3133 rustc_hash::FxHashMap::default();
3134 file_paths.insert(crate::discover::FileId(0), &files[0].path);
3135
3136 let mut results = fallow_types::results::AnalysisResults::default();
3137 results.unused_files.push(
3138 fallow_types::output_dead_code::UnusedFileFinding::with_actions(
3139 fallow_types::results::UnusedFile {
3140 path: path_a.clone(),
3141 },
3142 ),
3143 );
3144
3145 let output = crate::results::DeadCodeAnalysisArtifacts {
3146 results,
3147 timings: None,
3148 graph: Some(crate::module_graph::RetainedModuleGraph::from(graph)),
3149 modules: None,
3150 files: None,
3151 script_used_packages: rustc_hash::FxHashSet::default(),
3152 file_hashes: rustc_hash::FxHashMap::default(),
3153 };
3154
3155 let result = compute_file_scores(
3156 &modules,
3157 &file_paths,
3158 None,
3159 output,
3160 None,
3161 std::path::Path::new("/project"),
3162 )
3163 .unwrap();
3164 assert_eq!(result.scores.len(), 1);
3165 assert!((result.scores[0].dead_code_ratio - 1.0).abs() < f64::EPSILON);
3166 assert!(result.unused_export_names.contains_key(&path_a));
3167 let names = &result.unused_export_names[&path_a];
3168 assert_eq!(names, &["orphan"]);
3169 assert_eq!(result.analysis_counts.dead_files, 1);
3170 }
3171
3172 #[test]
3173 #[expect(
3174 clippy::too_many_lines,
3175 reason = "test fixture; linear setup/assert, length is not a maintainability concern"
3176 )]
3177 fn compute_file_scores_tracks_top_complex_functions() {
3178 let path_a = std::path::PathBuf::from("/src/complex.ts");
3179 let files = vec![crate::discover::DiscoveredFile {
3180 id: crate::discover::FileId(0),
3181 path: path_a.clone(),
3182 size_bytes: 500,
3183 }];
3184
3185 let resolved_modules = vec![fallow_graph::resolve::ResolvedModule {
3186 file_id: crate::discover::FileId(0),
3187 path: path_a.clone(),
3188 ..Default::default()
3189 }];
3190
3191 let graph = build_test_graph(&files, &[], &resolved_modules);
3192
3193 let modules = vec![make_module_info(
3194 0,
3195 50,
3196 vec![
3197 fallow_types::extract::FunctionComplexity {
3198 name: "high".into(),
3199 line: 1,
3200 col: 0,
3201 cyclomatic: 10,
3202 cognitive: 20,
3203 line_count: 10,
3204 param_count: 0,
3205 react_hook_count: 0,
3206 react_jsx_max_depth: 0,
3207 react_prop_count: 0,
3208 source_hash: None,
3209 contributions: Vec::new(),
3210 },
3211 fallow_types::extract::FunctionComplexity {
3212 name: "medium".into(),
3213 line: 11,
3214 col: 0,
3215 cyclomatic: 5,
3216 cognitive: 10,
3217 line_count: 10,
3218 param_count: 0,
3219 react_hook_count: 0,
3220 react_jsx_max_depth: 0,
3221 react_prop_count: 0,
3222 source_hash: None,
3223 contributions: Vec::new(),
3224 },
3225 fallow_types::extract::FunctionComplexity {
3226 name: "low".into(),
3227 line: 21,
3228 col: 0,
3229 cyclomatic: 2,
3230 cognitive: 5,
3231 line_count: 10,
3232 param_count: 0,
3233 react_hook_count: 0,
3234 react_jsx_max_depth: 0,
3235 react_prop_count: 0,
3236 source_hash: None,
3237 contributions: Vec::new(),
3238 },
3239 fallow_types::extract::FunctionComplexity {
3240 name: "trivial".into(),
3241 line: 31,
3242 col: 0,
3243 cyclomatic: 1,
3244 cognitive: 1,
3245 line_count: 10,
3246 param_count: 0,
3247 react_hook_count: 0,
3248 react_jsx_max_depth: 0,
3249 react_prop_count: 0,
3250 source_hash: None,
3251 contributions: Vec::new(),
3252 },
3253 ],
3254 )];
3255
3256 let mut file_paths: rustc_hash::FxHashMap<crate::discover::FileId, &std::path::PathBuf> =
3257 rustc_hash::FxHashMap::default();
3258 file_paths.insert(crate::discover::FileId(0), &files[0].path);
3259
3260 let output = crate::results::DeadCodeAnalysisArtifacts {
3261 results: fallow_types::results::AnalysisResults::default(),
3262 timings: None,
3263 graph: Some(crate::module_graph::RetainedModuleGraph::from(graph)),
3264 modules: None,
3265 files: None,
3266 script_used_packages: rustc_hash::FxHashSet::default(),
3267 file_hashes: rustc_hash::FxHashMap::default(),
3268 };
3269
3270 let result = compute_file_scores(
3271 &modules,
3272 &file_paths,
3273 None,
3274 output,
3275 None,
3276 std::path::Path::new("/project"),
3277 )
3278 .unwrap();
3279 assert!(result.top_complex_fns.contains_key(&path_a));
3280 let top = &result.top_complex_fns[&path_a];
3281 assert_eq!(top.len(), 3);
3282 assert_eq!(top[0].0, "high");
3283 assert_eq!(top[0].2, 20);
3284 assert_eq!(top[1].0, "medium");
3285 assert_eq!(top[1].2, 10);
3286 assert_eq!(top[2].0, "low");
3287 assert_eq!(top[2].2, 5);
3288 }
3289
3290 #[test]
3291 #[expect(
3292 clippy::too_many_lines,
3293 reason = "test fixture; linear setup/assert, length is not a maintainability concern"
3294 )]
3295 fn compute_file_scores_with_circular_deps() {
3296 let path_a = std::path::PathBuf::from("/src/a.ts");
3297 let path_b = std::path::PathBuf::from("/src/b.ts");
3298 let files = vec![
3299 crate::discover::DiscoveredFile {
3300 id: crate::discover::FileId(0),
3301 path: path_a.clone(),
3302 size_bytes: 100,
3303 },
3304 crate::discover::DiscoveredFile {
3305 id: crate::discover::FileId(1),
3306 path: path_b.clone(),
3307 size_bytes: 100,
3308 },
3309 ];
3310
3311 let resolved_modules = vec![
3312 fallow_graph::resolve::ResolvedModule {
3313 file_id: crate::discover::FileId(0),
3314 path: path_a.clone(),
3315 ..Default::default()
3316 },
3317 fallow_graph::resolve::ResolvedModule {
3318 file_id: crate::discover::FileId(1),
3319 path: path_b.clone(),
3320 ..Default::default()
3321 },
3322 ];
3323
3324 let graph = build_test_graph(&files, &[], &resolved_modules);
3325
3326 let modules = vec![
3327 make_module_info(
3328 0,
3329 10,
3330 vec![fallow_types::extract::FunctionComplexity {
3331 name: "fn_a".into(),
3332 line: 1,
3333 col: 0,
3334 cyclomatic: 2,
3335 cognitive: 1,
3336 line_count: 10,
3337 param_count: 0,
3338 react_hook_count: 0,
3339 react_jsx_max_depth: 0,
3340 react_prop_count: 0,
3341 source_hash: None,
3342 contributions: Vec::new(),
3343 }],
3344 ),
3345 make_module_info(
3346 1,
3347 10,
3348 vec![fallow_types::extract::FunctionComplexity {
3349 name: "fn_b".into(),
3350 line: 1,
3351 col: 0,
3352 cyclomatic: 3,
3353 cognitive: 2,
3354 line_count: 10,
3355 param_count: 0,
3356 react_hook_count: 0,
3357 react_jsx_max_depth: 0,
3358 react_prop_count: 0,
3359 source_hash: None,
3360 contributions: Vec::new(),
3361 }],
3362 ),
3363 ];
3364
3365 let mut file_paths: rustc_hash::FxHashMap<crate::discover::FileId, &std::path::PathBuf> =
3366 rustc_hash::FxHashMap::default();
3367 file_paths.insert(crate::discover::FileId(0), &files[0].path);
3368 file_paths.insert(crate::discover::FileId(1), &files[1].path);
3369
3370 let mut results = fallow_types::results::AnalysisResults::default();
3371 results.circular_dependencies.push(
3372 fallow_types::output_dead_code::CircularDependencyFinding::with_actions(
3373 fallow_types::results::CircularDependency {
3374 files: vec![path_a.clone(), path_b.clone()],
3375 length: 2,
3376 line: 1,
3377 col: 0,
3378 edges: Vec::new(),
3379 is_cross_package: false,
3380 },
3381 ),
3382 );
3383
3384 let output = crate::results::DeadCodeAnalysisArtifacts {
3385 results,
3386 timings: None,
3387 graph: Some(crate::module_graph::RetainedModuleGraph::from(graph)),
3388 modules: None,
3389 files: None,
3390 script_used_packages: rustc_hash::FxHashSet::default(),
3391 file_hashes: rustc_hash::FxHashMap::default(),
3392 };
3393
3394 let result = compute_file_scores(
3395 &modules,
3396 &file_paths,
3397 None,
3398 output,
3399 None,
3400 std::path::Path::new("/project"),
3401 )
3402 .unwrap();
3403 assert!(result.circular_files.contains(&path_a));
3404 assert!(result.circular_files.contains(&path_b));
3405 assert!(result.cycle_members.contains_key(&path_a));
3406 assert_eq!(result.cycle_members[&path_a], vec![path_b.clone()]);
3407 assert!(result.cycle_members.contains_key(&path_b));
3408 assert_eq!(result.cycle_members[&path_b], vec![path_a]);
3409 assert_eq!(result.analysis_counts.circular_deps, 1);
3410 }
3411
3412 #[test]
3413 #[expect(
3414 clippy::too_many_lines,
3415 reason = "test fixture; linear setup/assert, length is not a maintainability concern"
3416 )]
3417 fn compute_file_scores_analysis_counts_unused_exports_and_types() {
3418 let path_a = std::path::PathBuf::from("/src/a.ts");
3419 let files = vec![crate::discover::DiscoveredFile {
3420 id: crate::discover::FileId(0),
3421 path: path_a.clone(),
3422 size_bytes: 100,
3423 }];
3424
3425 let resolved_modules = vec![fallow_graph::resolve::ResolvedModule {
3426 file_id: crate::discover::FileId(0),
3427 path: path_a.clone(),
3428 exports: vec![
3429 fallow_types::extract::ExportInfo {
3430 name: crate::source::ExportName::Named("foo".into()),
3431 local_name: None,
3432 is_type_only: false,
3433 visibility: crate::source::VisibilityTag::None,
3434 expected_unused_reason: None,
3435 span: oxc_span::Span::empty(0),
3436 members: vec![],
3437 is_side_effect_used: false,
3438 super_class: None,
3439 },
3440 fallow_types::extract::ExportInfo {
3441 name: crate::source::ExportName::Named("bar".into()),
3442 local_name: None,
3443 is_type_only: false,
3444 visibility: crate::source::VisibilityTag::None,
3445 expected_unused_reason: None,
3446 span: oxc_span::Span::empty(0),
3447 members: vec![],
3448 is_side_effect_used: false,
3449 super_class: None,
3450 },
3451 ],
3452 ..Default::default()
3453 }];
3454
3455 let graph = build_test_graph(&files, &[], &resolved_modules);
3456
3457 let mut module = make_module_info(
3458 0,
3459 10,
3460 vec![fallow_types::extract::FunctionComplexity {
3461 name: "fn_a".into(),
3462 line: 1,
3463 col: 0,
3464 cyclomatic: 1,
3465 cognitive: 0,
3466 line_count: 10,
3467 param_count: 0,
3468 react_hook_count: 0,
3469 react_jsx_max_depth: 0,
3470 react_prop_count: 0,
3471 source_hash: None,
3472 contributions: Vec::new(),
3473 }],
3474 );
3475 module.exports = vec![
3476 fallow_types::extract::ExportInfo {
3477 name: crate::source::ExportName::Named("foo".into()),
3478 local_name: None,
3479 is_type_only: false,
3480 visibility: crate::source::VisibilityTag::None,
3481 expected_unused_reason: None,
3482 span: oxc_span::Span::empty(0),
3483 members: vec![],
3484 is_side_effect_used: false,
3485 super_class: None,
3486 },
3487 fallow_types::extract::ExportInfo {
3488 name: crate::source::ExportName::Named("bar".into()),
3489 local_name: None,
3490 is_type_only: false,
3491 visibility: crate::source::VisibilityTag::None,
3492 expected_unused_reason: None,
3493 span: oxc_span::Span::empty(0),
3494 members: vec![],
3495 is_side_effect_used: false,
3496 super_class: None,
3497 },
3498 ];
3499 let modules = vec![module];
3500
3501 let mut file_paths: rustc_hash::FxHashMap<crate::discover::FileId, &std::path::PathBuf> =
3502 rustc_hash::FxHashMap::default();
3503 file_paths.insert(crate::discover::FileId(0), &files[0].path);
3504
3505 let mut results = fallow_types::results::AnalysisResults::default();
3506 results.unused_exports.push(
3507 fallow_types::output_dead_code::UnusedExportFinding::with_actions(
3508 fallow_types::results::UnusedExport {
3509 path: path_a.clone(),
3510 export_name: "foo".into(),
3511 is_type_only: false,
3512 line: 1,
3513 col: 0,
3514 span_start: 0,
3515 is_re_export: false,
3516 },
3517 ),
3518 );
3519 results.unused_types.push(
3520 fallow_types::output_dead_code::UnusedTypeFinding::with_actions(
3521 fallow_types::results::UnusedExport {
3522 path: path_a,
3523 export_name: "MyType".into(),
3524 is_type_only: true,
3525 line: 5,
3526 col: 0,
3527 span_start: 40,
3528 is_re_export: false,
3529 },
3530 ),
3531 );
3532 results.unused_dependencies.push(
3533 fallow_types::output_dead_code::UnusedDependencyFinding::with_actions(
3534 fallow_types::results::UnusedDependency {
3535 package_name: "lodash".into(),
3536 location: fallow_types::results::DependencyLocation::Dependencies,
3537 path: std::path::PathBuf::from("/package.json"),
3538 line: 1,
3539 used_in_workspaces: Vec::new(),
3540 },
3541 ),
3542 );
3543
3544 let output = crate::results::DeadCodeAnalysisArtifacts {
3545 results,
3546 timings: None,
3547 graph: Some(crate::module_graph::RetainedModuleGraph::from(graph)),
3548 modules: None,
3549 files: None,
3550 script_used_packages: rustc_hash::FxHashSet::default(),
3551 file_hashes: rustc_hash::FxHashMap::default(),
3552 };
3553
3554 let result = compute_file_scores(
3555 &modules,
3556 &file_paths,
3557 None,
3558 output,
3559 None,
3560 std::path::Path::new("/project"),
3561 )
3562 .unwrap();
3563 assert_eq!(result.analysis_counts.total_exports, 2);
3564 assert_eq!(result.analysis_counts.dead_exports, 2);
3565 assert_eq!(result.analysis_counts.unused_deps, 1);
3566 }
3567
3568 #[test]
3570 #[expect(
3571 clippy::too_many_lines,
3572 reason = "test fixture; linear setup/assert, length is not a maintainability concern"
3573 )]
3574 fn total_exports_counts_graph_modules_not_extraction_modules() {
3575 let path_a = std::path::PathBuf::from("/src/a.ts");
3576 let files = vec![crate::discover::DiscoveredFile {
3577 id: crate::discover::FileId(0),
3578 path: path_a.clone(),
3579 size_bytes: 100,
3580 }];
3581
3582 let resolved_modules = vec![fallow_graph::resolve::ResolvedModule {
3583 file_id: crate::discover::FileId(0),
3584 path: path_a.clone(),
3585 exports: vec![
3586 fallow_types::extract::ExportInfo {
3587 name: crate::source::ExportName::Named("foo".into()),
3588 local_name: None,
3589 is_type_only: false,
3590 visibility: crate::source::VisibilityTag::None,
3591 expected_unused_reason: None,
3592 span: oxc_span::Span::empty(0),
3593 members: vec![],
3594 is_side_effect_used: false,
3595 super_class: None,
3596 },
3597 fallow_types::extract::ExportInfo {
3598 name: crate::source::ExportName::Named("bar".into()),
3599 local_name: None,
3600 is_type_only: false,
3601 visibility: crate::source::VisibilityTag::None,
3602 expected_unused_reason: None,
3603 span: oxc_span::Span::empty(0),
3604 members: vec![],
3605 is_side_effect_used: false,
3606 super_class: None,
3607 },
3608 fallow_types::extract::ExportInfo {
3609 name: crate::source::ExportName::Named("baz".into()),
3610 local_name: None,
3611 is_type_only: false,
3612 visibility: crate::source::VisibilityTag::None,
3613 expected_unused_reason: None,
3614 span: oxc_span::Span::new(0, 0),
3615 members: vec![],
3616 is_side_effect_used: false,
3617 super_class: None,
3618 },
3619 ],
3620 ..Default::default()
3621 }];
3622
3623 let graph = build_test_graph(&files, &[], &resolved_modules);
3624
3625 let mut module = make_module_info(
3626 0,
3627 10,
3628 vec![fallow_types::extract::FunctionComplexity {
3629 name: "fn_a".into(),
3630 line: 1,
3631 col: 0,
3632 cyclomatic: 1,
3633 cognitive: 0,
3634 line_count: 10,
3635 param_count: 0,
3636 react_hook_count: 0,
3637 react_jsx_max_depth: 0,
3638 react_prop_count: 0,
3639 source_hash: None,
3640 contributions: Vec::new(),
3641 }],
3642 );
3643 module.exports = vec![
3644 fallow_types::extract::ExportInfo {
3645 name: crate::source::ExportName::Named("foo".into()),
3646 local_name: None,
3647 is_type_only: false,
3648 visibility: crate::source::VisibilityTag::None,
3649 expected_unused_reason: None,
3650 span: oxc_span::Span::empty(0),
3651 members: vec![],
3652 is_side_effect_used: false,
3653 super_class: None,
3654 },
3655 fallow_types::extract::ExportInfo {
3656 name: crate::source::ExportName::Named("bar".into()),
3657 local_name: None,
3658 is_type_only: false,
3659 visibility: crate::source::VisibilityTag::None,
3660 expected_unused_reason: None,
3661 span: oxc_span::Span::empty(0),
3662 members: vec![],
3663 is_side_effect_used: false,
3664 super_class: None,
3665 },
3666 ];
3667 let modules = vec![module];
3668
3669 let mut file_paths: rustc_hash::FxHashMap<crate::discover::FileId, &std::path::PathBuf> =
3670 rustc_hash::FxHashMap::default();
3671 file_paths.insert(crate::discover::FileId(0), &files[0].path);
3672
3673 let mut results = fallow_types::results::AnalysisResults::default();
3674 for name in ["foo", "bar", "baz"] {
3675 results.unused_exports.push(
3676 fallow_types::output_dead_code::UnusedExportFinding::with_actions(
3677 fallow_types::results::UnusedExport {
3678 path: path_a.clone(),
3679 export_name: name.into(),
3680 is_type_only: false,
3681 line: 1,
3682 col: 0,
3683 span_start: 0,
3684 is_re_export: name == "baz",
3685 },
3686 ),
3687 );
3688 }
3689
3690 let output = crate::results::DeadCodeAnalysisArtifacts {
3691 results,
3692 timings: None,
3693 graph: Some(crate::module_graph::RetainedModuleGraph::from(graph)),
3694 modules: None,
3695 files: None,
3696 script_used_packages: rustc_hash::FxHashSet::default(),
3697 file_hashes: rustc_hash::FxHashMap::default(),
3698 };
3699
3700 let result = compute_file_scores(
3701 &modules,
3702 &file_paths,
3703 None,
3704 output,
3705 None,
3706 std::path::Path::new("/project"),
3707 )
3708 .unwrap();
3709 assert_eq!(result.analysis_counts.total_exports, 3);
3710 assert_eq!(result.analysis_counts.dead_exports, 3);
3711 }
3712
3713 #[test]
3714 fn compute_file_scores_module_not_in_file_paths_skipped() {
3715 let path_a = std::path::PathBuf::from("/src/a.ts");
3716 let files = vec![crate::discover::DiscoveredFile {
3717 id: crate::discover::FileId(0),
3718 path: path_a.clone(),
3719 size_bytes: 100,
3720 }];
3721
3722 let resolved_modules = vec![fallow_graph::resolve::ResolvedModule {
3723 file_id: crate::discover::FileId(0),
3724 path: path_a,
3725 ..Default::default()
3726 }];
3727
3728 let graph = build_test_graph(&files, &[], &resolved_modules);
3729
3730 let modules = vec![make_module_info(
3731 0,
3732 10,
3733 vec![fallow_types::extract::FunctionComplexity {
3734 name: "fn_a".into(),
3735 line: 1,
3736 col: 0,
3737 cyclomatic: 2,
3738 cognitive: 1,
3739 line_count: 10,
3740 param_count: 0,
3741 react_hook_count: 0,
3742 react_jsx_max_depth: 0,
3743 react_prop_count: 0,
3744 source_hash: None,
3745 contributions: Vec::new(),
3746 }],
3747 )];
3748
3749 let file_paths: rustc_hash::FxHashMap<crate::discover::FileId, &std::path::PathBuf> =
3750 rustc_hash::FxHashMap::default();
3751
3752 let output = crate::results::DeadCodeAnalysisArtifacts {
3753 results: fallow_types::results::AnalysisResults::default(),
3754 timings: None,
3755 graph: Some(crate::module_graph::RetainedModuleGraph::from(graph)),
3756 modules: None,
3757 files: None,
3758 script_used_packages: rustc_hash::FxHashSet::default(),
3759 file_hashes: rustc_hash::FxHashMap::default(),
3760 };
3761
3762 let result = compute_file_scores(
3763 &modules,
3764 &file_paths,
3765 None,
3766 output,
3767 None,
3768 std::path::Path::new("/project"),
3769 )
3770 .unwrap();
3771 assert!(result.scores.is_empty());
3772 }
3773
3774 #[test]
3775 fn compute_file_scores_mi_rounded_to_one_decimal() {
3776 let path_a = std::path::PathBuf::from("/src/a.ts");
3777 let files = vec![crate::discover::DiscoveredFile {
3778 id: crate::discover::FileId(0),
3779 path: path_a.clone(),
3780 size_bytes: 100,
3781 }];
3782
3783 let resolved_modules = vec![fallow_graph::resolve::ResolvedModule {
3784 file_id: crate::discover::FileId(0),
3785 path: path_a.clone(),
3786 ..Default::default()
3787 }];
3788
3789 let graph = build_test_graph(&files, std::slice::from_ref(&path_a), &resolved_modules);
3790
3791 let modules = vec![make_module_info(
3792 0,
3793 100,
3794 vec![fallow_types::extract::FunctionComplexity {
3795 name: "fn".into(),
3796 line: 1,
3797 col: 0,
3798 cyclomatic: 7,
3799 cognitive: 3,
3800 line_count: 100,
3801 param_count: 0,
3802 react_hook_count: 0,
3803 react_jsx_max_depth: 0,
3804 react_prop_count: 0,
3805 source_hash: None,
3806 contributions: Vec::new(),
3807 }],
3808 )];
3809
3810 let mut file_paths: rustc_hash::FxHashMap<crate::discover::FileId, &std::path::PathBuf> =
3811 rustc_hash::FxHashMap::default();
3812 file_paths.insert(crate::discover::FileId(0), &files[0].path);
3813
3814 let output = crate::results::DeadCodeAnalysisArtifacts {
3815 results: fallow_types::results::AnalysisResults::default(),
3816 timings: None,
3817 graph: Some(crate::module_graph::RetainedModuleGraph::from(graph)),
3818 modules: None,
3819 files: None,
3820 script_used_packages: rustc_hash::FxHashSet::default(),
3821 file_hashes: rustc_hash::FxHashMap::default(),
3822 };
3823
3824 let result = compute_file_scores(
3825 &modules,
3826 &file_paths,
3827 None,
3828 output,
3829 None,
3830 std::path::Path::new("/project"),
3831 )
3832 .unwrap();
3833 let mi = result.scores[0].maintainability_index;
3834 let rounded = (mi * 10.0).round() / 10.0;
3835 assert!((mi - rounded).abs() < f64::EPSILON);
3836 }
3837
3838 #[test]
3839 fn compute_file_scores_value_export_counts_tracked() {
3840 let path_a = std::path::PathBuf::from("/src/a.ts");
3841 let files = vec![crate::discover::DiscoveredFile {
3842 id: crate::discover::FileId(0),
3843 path: path_a.clone(),
3844 size_bytes: 100,
3845 }];
3846
3847 let resolved_modules = vec![fallow_graph::resolve::ResolvedModule {
3848 file_id: crate::discover::FileId(0),
3849 path: path_a.clone(),
3850 exports: vec![
3851 fallow_types::extract::ExportInfo {
3852 name: crate::source::ExportName::Named("a".into()),
3853 local_name: None,
3854 is_type_only: false,
3855 visibility: crate::source::VisibilityTag::None,
3856 expected_unused_reason: None,
3857 span: oxc_span::Span::empty(0),
3858 members: vec![],
3859 is_side_effect_used: false,
3860 super_class: None,
3861 },
3862 fallow_types::extract::ExportInfo {
3863 name: crate::source::ExportName::Named("b".into()),
3864 local_name: None,
3865 is_type_only: false,
3866 visibility: crate::source::VisibilityTag::None,
3867 expected_unused_reason: None,
3868 span: oxc_span::Span::empty(0),
3869 members: vec![],
3870 is_side_effect_used: false,
3871 super_class: None,
3872 },
3873 fallow_types::extract::ExportInfo {
3874 name: crate::source::ExportName::Named("T".into()),
3875 local_name: None,
3876 is_type_only: true,
3877 visibility: crate::source::VisibilityTag::None,
3878 expected_unused_reason: None,
3879 span: oxc_span::Span::empty(0),
3880 members: vec![],
3881 is_side_effect_used: false,
3882 super_class: None,
3883 },
3884 ],
3885 ..Default::default()
3886 }];
3887
3888 let graph = build_test_graph(&files, &[], &resolved_modules);
3889
3890 let modules = vec![make_module_info(
3891 0,
3892 10,
3893 vec![fallow_types::extract::FunctionComplexity {
3894 name: "fn_a".into(),
3895 line: 1,
3896 col: 0,
3897 cyclomatic: 2,
3898 cognitive: 1,
3899 line_count: 10,
3900 param_count: 0,
3901 react_hook_count: 0,
3902 react_jsx_max_depth: 0,
3903 react_prop_count: 0,
3904 source_hash: None,
3905 contributions: Vec::new(),
3906 }],
3907 )];
3908
3909 let mut file_paths: rustc_hash::FxHashMap<crate::discover::FileId, &std::path::PathBuf> =
3910 rustc_hash::FxHashMap::default();
3911 file_paths.insert(crate::discover::FileId(0), &files[0].path);
3912
3913 let output = crate::results::DeadCodeAnalysisArtifacts {
3914 results: fallow_types::results::AnalysisResults::default(),
3915 timings: None,
3916 graph: Some(crate::module_graph::RetainedModuleGraph::from(graph)),
3917 modules: None,
3918 files: None,
3919 script_used_packages: rustc_hash::FxHashSet::default(),
3920 file_hashes: rustc_hash::FxHashMap::default(),
3921 };
3922
3923 let result = compute_file_scores(
3924 &modules,
3925 &file_paths,
3926 None,
3927 output,
3928 None,
3929 std::path::Path::new("/project"),
3930 )
3931 .unwrap();
3932 assert_eq!(result.value_export_counts[&path_a], 2);
3933 }
3934
3935 #[test]
3936 fn compute_file_scores_top_complex_fns_zero_cognitive_excluded() {
3937 let path_a = std::path::PathBuf::from("/src/simple.ts");
3938 let files = vec![crate::discover::DiscoveredFile {
3939 id: crate::discover::FileId(0),
3940 path: path_a.clone(),
3941 size_bytes: 100,
3942 }];
3943
3944 let resolved_modules = vec![fallow_graph::resolve::ResolvedModule {
3945 file_id: crate::discover::FileId(0),
3946 path: path_a.clone(),
3947 ..Default::default()
3948 }];
3949
3950 let graph = build_test_graph(&files, &[], &resolved_modules);
3951
3952 let modules = vec![make_module_info(
3953 0,
3954 10,
3955 vec![fallow_types::extract::FunctionComplexity {
3956 name: "trivial".into(),
3957 line: 1,
3958 col: 0,
3959 cyclomatic: 1,
3960 cognitive: 0,
3961 line_count: 10,
3962 param_count: 0,
3963 react_hook_count: 0,
3964 react_jsx_max_depth: 0,
3965 react_prop_count: 0,
3966 source_hash: None,
3967 contributions: Vec::new(),
3968 }],
3969 )];
3970
3971 let mut file_paths: rustc_hash::FxHashMap<crate::discover::FileId, &std::path::PathBuf> =
3972 rustc_hash::FxHashMap::default();
3973 file_paths.insert(crate::discover::FileId(0), &files[0].path);
3974
3975 let output = crate::results::DeadCodeAnalysisArtifacts {
3976 results: fallow_types::results::AnalysisResults::default(),
3977 timings: None,
3978 graph: Some(crate::module_graph::RetainedModuleGraph::from(graph)),
3979 modules: None,
3980 files: None,
3981 script_used_packages: rustc_hash::FxHashSet::default(),
3982 file_hashes: rustc_hash::FxHashMap::default(),
3983 };
3984
3985 let result = compute_file_scores(
3986 &modules,
3987 &file_paths,
3988 None,
3989 output,
3990 None,
3991 std::path::Path::new("/project"),
3992 )
3993 .unwrap();
3994 assert!(!result.top_complex_fns.contains_key(&path_a));
3995 }
3996
3997 fn make_fn_complexity(cyclomatic: u16) -> fallow_types::extract::FunctionComplexity {
3998 fallow_types::extract::FunctionComplexity {
3999 name: "test_fn".into(),
4000 line: 1,
4001 col: 0,
4002 cyclomatic,
4003 cognitive: 0,
4004 line_count: 10,
4005 param_count: 0,
4006 react_hook_count: 0,
4007 react_jsx_max_depth: 0,
4008 react_prop_count: 0,
4009 source_hash: None,
4010 contributions: Vec::new(),
4011 }
4012 }
4013
4014 #[test]
4015 fn crap_scores_empty_complexity() {
4016 let (max, above) = compute_crap_scores_binary(&[], true);
4017 assert!((max).abs() < f64::EPSILON);
4018 assert_eq!(above, 0);
4019 }
4020
4021 #[test]
4022 fn crap_scores_test_reachable() {
4023 let funcs = vec![make_fn_complexity(5)];
4024 let (max, above) = compute_crap_scores_binary(&funcs, true);
4025 assert!((max - 5.0).abs() < f64::EPSILON);
4026 assert_eq!(above, 0);
4027 }
4028
4029 #[test]
4030 fn crap_scores_untested_at_threshold() {
4031 let funcs = vec![make_fn_complexity(5)];
4032 let (max, above) = compute_crap_scores_binary(&funcs, false);
4033 assert!((max - 30.0).abs() < f64::EPSILON);
4034 assert_eq!(above, 1);
4035 }
4036
4037 #[test]
4038 fn crap_scores_untested_above_threshold() {
4039 let funcs = vec![make_fn_complexity(6)];
4040 let (max, above) = compute_crap_scores_binary(&funcs, false);
4041 assert!((max - 42.0).abs() < f64::EPSILON);
4042 assert_eq!(above, 1);
4043 }
4044
4045 #[test]
4046 fn crap_scores_untested_below_threshold() {
4047 let funcs = vec![make_fn_complexity(4)];
4048 let (max, above) = compute_crap_scores_binary(&funcs, false);
4049 assert!((max - 20.0).abs() < f64::EPSILON);
4050 assert_eq!(above, 0);
4051 }
4052
4053 #[test]
4054 fn crap_scores_mixed_functions_untested() {
4055 let funcs = vec![
4056 make_fn_complexity(2),
4057 make_fn_complexity(5),
4058 make_fn_complexity(8),
4059 ];
4060 let (max, above) = compute_crap_scores_binary(&funcs, false);
4061 assert!((max - 72.0).abs() < f64::EPSILON);
4062 assert_eq!(above, 2);
4063 }
4064
4065 #[test]
4066 fn crap_formula_full_coverage() {
4067 let result = crap_formula(10.0, 100.0);
4068 assert!((result - 10.0).abs() < f64::EPSILON);
4069 }
4070
4071 #[test]
4072 fn crap_formula_zero_coverage() {
4073 let result = crap_formula(5.0, 0.0);
4074 assert!((result - 30.0).abs() < f64::EPSILON);
4075 }
4076
4077 #[test]
4078 fn crap_formula_partial_coverage() {
4079 let result = crap_formula(10.0, 50.0);
4080 assert!((result - 22.5).abs() < f64::EPSILON);
4081 }
4082
4083 #[test]
4084 fn crap_formula_high_coverage_low_complexity() {
4085 let result = crap_formula(2.0, 90.0);
4086 assert!((result - 2.004).abs() < 0.001);
4087 }
4088
4089 #[test]
4090 fn istanbul_crap_with_coverage_data() {
4091 let funcs = vec![make_fn_complexity(10)];
4092 let mut functions = rustc_hash::FxHashMap::default();
4093 functions.insert(("test_fn".to_string(), 1, 0), 80.0);
4094 let file_cov = IstanbulFileCoverage { functions };
4095 let result = compute_crap_scores_istanbul(&funcs, Some(&file_cov), false);
4096 assert!((result.max_crap - 10.8).abs() < 0.1);
4097 assert_eq!(result.above_threshold, 0);
4098 }
4099
4100 #[test]
4101 fn istanbul_crap_falls_back_to_binary_when_no_match() {
4102 let funcs = vec![make_fn_complexity(6)];
4103 let file_cov = IstanbulFileCoverage {
4104 functions: rustc_hash::FxHashMap::default(),
4105 };
4106 let result = compute_crap_scores_istanbul(&funcs, Some(&file_cov), false);
4107 assert!((result.max_crap - 42.0).abs() < f64::EPSILON);
4108 assert_eq!(result.above_threshold, 1);
4109 }
4110
4111 #[test]
4112 fn istanbul_crap_falls_back_to_binary_when_no_file_coverage() {
4113 let funcs = vec![make_fn_complexity(5)];
4114 let result = compute_crap_scores_istanbul(&funcs, None, true);
4115 assert!((result.max_crap - 5.0).abs() < f64::EPSILON);
4116 assert_eq!(result.above_threshold, 0);
4117 }
4118
4119 #[test]
4120 fn istanbul_crap_zero_coverage_matches_binary_untested() {
4121 let funcs = vec![make_fn_complexity(5)];
4122 let mut functions = rustc_hash::FxHashMap::default();
4123 functions.insert(("test_fn".to_string(), 1, 0), 0.0);
4124 let file_cov = IstanbulFileCoverage { functions };
4125 let result = compute_crap_scores_istanbul(&funcs, Some(&file_cov), false);
4126 assert!((result.max_crap - 30.0).abs() < f64::EPSILON);
4127 assert_eq!(result.above_threshold, 1);
4128 }
4129
4130 #[test]
4131 fn estimated_crap_direct_test_reference() {
4132 let funcs = vec![make_fn_complexity(10)];
4133 let mut refs = rustc_hash::FxHashSet::default();
4134 refs.insert("test_fn".to_string());
4135 let result = compute_crap_scores_estimated(
4136 &funcs,
4137 &refs,
4138 true,
4139 fallow_output::CoverageSource::Estimated,
4140 );
4141 let (max, above) = (result.max_crap, result.above_threshold);
4142 assert!((max - 10.3).abs() < 0.1);
4143 assert_eq!(above, 0);
4144 }
4145
4146 #[test]
4147 fn estimated_crap_indirect_test_reachable() {
4148 let funcs = vec![make_fn_complexity(10)];
4149 let refs = rustc_hash::FxHashSet::default();
4150 let result = compute_crap_scores_estimated(
4151 &funcs,
4152 &refs,
4153 true,
4154 fallow_output::CoverageSource::Estimated,
4155 );
4156 let (max, above) = (result.max_crap, result.above_threshold);
4157 assert!((max - 31.6).abs() < 0.1);
4158 assert_eq!(above, 1);
4159 }
4160
4161 #[test]
4162 fn estimated_crap_untested_file() {
4163 let funcs = vec![make_fn_complexity(5)];
4164 let refs = rustc_hash::FxHashSet::default();
4165 let result = compute_crap_scores_estimated(
4166 &funcs,
4167 &refs,
4168 false,
4169 fallow_output::CoverageSource::Estimated,
4170 );
4171 let (max, above) = (result.max_crap, result.above_threshold);
4172 assert!((max - 30.0).abs() < f64::EPSILON);
4173 assert_eq!(above, 1);
4174 }
4175
4176 #[test]
4177 fn estimated_crap_low_complexity_direct_ref() {
4178 let funcs = vec![make_fn_complexity(2)];
4179 let mut refs = rustc_hash::FxHashSet::default();
4180 refs.insert("test_fn".to_string());
4181 let result = compute_crap_scores_estimated(
4182 &funcs,
4183 &refs,
4184 true,
4185 fallow_output::CoverageSource::Estimated,
4186 );
4187 let (max, above) = (result.max_crap, result.above_threshold);
4188 assert!(max < 3.0);
4189 assert_eq!(above, 0);
4190 }
4191
4192 #[test]
4193 fn estimated_crap_empty() {
4194 let refs = rustc_hash::FxHashSet::default();
4195 let result = compute_crap_scores_estimated(
4196 &[],
4197 &refs,
4198 true,
4199 fallow_output::CoverageSource::Estimated,
4200 );
4201 let (max, above) = (result.max_crap, result.above_threshold);
4202 assert!((max).abs() < f64::EPSILON);
4203 assert_eq!(above, 0);
4204 }
4205
4206 fn make_export(name: &str, is_type_only: bool) -> fallow_graph::graph::ExportSymbol {
4207 fallow_graph::graph::ExportSymbol {
4208 name: fallow_types::extract::ExportName::Named(name.into()),
4209 is_type_only,
4210 is_side_effect_used: false,
4211 visibility: crate::source::VisibilityTag::None,
4212 expected_unused_reason: None,
4213 span: oxc_span::Span::default(),
4214 references: vec![],
4215 members: vec![],
4216 }
4217 }
4218
4219 #[test]
4220 fn dead_code_ratio_type_only_exports_excluded_from_denominator() {
4221 let path = std::path::Path::new("src/types.ts");
4222 let exports = vec![
4223 make_export("MyInterface", true),
4224 make_export("MyType", true),
4225 make_export("myFunction", false),
4226 ];
4227 let unused_files = rustc_hash::FxHashSet::default();
4228 let mut unused_by_path = rustc_hash::FxHashMap::default();
4229 unused_by_path.insert(path, 1_usize); let ratio = compute_dead_code_ratio(path, &exports, &unused_files, &unused_by_path);
4232 assert!((ratio - 1.0).abs() < f64::EPSILON);
4233 }
4234
4235 #[test]
4236 fn dead_code_ratio_only_type_exports_returns_zero() {
4237 let path = std::path::Path::new("src/types.ts");
4238 let exports = vec![
4239 make_export("MyInterface", true),
4240 make_export("MyType", true),
4241 ];
4242 let unused_files = rustc_hash::FxHashSet::default();
4243 let unused_by_path = rustc_hash::FxHashMap::default();
4244
4245 let ratio = compute_dead_code_ratio(path, &exports, &unused_files, &unused_by_path);
4246 assert!(ratio.abs() < f64::EPSILON);
4247 }
4248
4249 #[test]
4250 fn dead_code_ratio_mixed_exports_counts_only_values() {
4251 let path = std::path::Path::new("src/component.ts");
4252 let exports = vec![
4253 make_export("Props", true),
4254 make_export("State", true),
4255 make_export("Component", false),
4256 make_export("helper", false),
4257 ];
4258 let unused_files = rustc_hash::FxHashSet::default();
4259 let mut unused_by_path = rustc_hash::FxHashMap::default();
4260 unused_by_path.insert(path, 1_usize);
4261
4262 let ratio = compute_dead_code_ratio(path, &exports, &unused_files, &unused_by_path);
4263 assert!((ratio - 0.5).abs() < f64::EPSILON);
4264 }
4265
4266 fn write_single_file_istanbul_fixture(
4267 coverage_path: &std::path::Path,
4268 source_path: &std::path::Path,
4269 fn_map: &serde_json::Value,
4270 function_hits: &serde_json::Value,
4271 ) {
4272 let mut root = serde_json::Map::new();
4273 root.insert(
4274 source_path.to_string_lossy().into_owned(),
4275 serde_json::json!({
4276 "path": source_path.to_string_lossy().into_owned(),
4277 "statementMap": {},
4278 "fnMap": fn_map,
4279 "branchMap": {},
4280 "s": {},
4281 "f": function_hits,
4282 "b": {}
4283 }),
4284 );
4285
4286 std::fs::write(coverage_path, serde_json::to_string(&root).unwrap()).unwrap();
4287 }
4288
4289 #[test]
4290 fn resolve_relative_to_root_joins_relative_with_project_root() {
4291 let resolved = resolve_relative_to_root(
4292 std::path::Path::new("coverage/coverage-final.json"),
4293 Some(std::path::Path::new("/work/my-app")),
4294 );
4295 assert_eq!(
4296 resolved,
4297 std::path::PathBuf::from("/work/my-app/coverage/coverage-final.json")
4298 );
4299 }
4300
4301 #[test]
4302 fn resolve_relative_to_root_returns_absolute_unchanged() {
4303 let resolved = resolve_relative_to_root(
4304 std::path::Path::new("/tmp/coverage-final.json"),
4305 Some(std::path::Path::new("/work/my-app")),
4306 );
4307 assert_eq!(
4308 resolved,
4309 std::path::PathBuf::from("/tmp/coverage-final.json")
4310 );
4311 }
4312
4313 #[test]
4314 fn resolve_relative_to_root_returns_windows_absolute_unchanged_on_any_host() {
4315 let path = std::path::Path::new(r"C:\coverage\coverage-final.json");
4316 let resolved = resolve_relative_to_root(path, Some(std::path::Path::new("/work/my-app")));
4317 assert_eq!(resolved, path);
4318 }
4319
4320 #[cfg(windows)]
4321 #[test]
4322 fn resolve_relative_to_root_returns_posix_rooted_path_unchanged_on_windows() {
4323 let path = std::path::Path::new(r"/ci/workspace/coverage-final.json");
4324 let resolved =
4325 resolve_relative_to_root(path, Some(std::path::Path::new(r"C:\work\my-app")));
4326 assert_eq!(resolved, path);
4327 }
4328
4329 #[test]
4330 fn resolve_relative_to_root_without_project_root_returns_relative_unchanged() {
4331 let resolved =
4332 resolve_relative_to_root(std::path::Path::new("coverage/coverage-final.json"), None);
4333 assert_eq!(
4334 resolved,
4335 std::path::PathBuf::from("coverage/coverage-final.json")
4336 );
4337 }
4338
4339 #[test]
4340 fn load_istanbul_coverage_resolves_relative_path_against_project_root() {
4341 let temp = tempfile::TempDir::new().unwrap();
4342 let source_path = temp.path().join("src/index.ts");
4343 std::fs::create_dir_all(source_path.parent().unwrap()).unwrap();
4344 std::fs::write(&source_path, "export function f(){}").unwrap();
4345
4346 let coverage_path = temp.path().join("coverage/coverage-final.json");
4347 std::fs::create_dir_all(coverage_path.parent().unwrap()).unwrap();
4348 write_single_file_istanbul_fixture(
4349 &coverage_path,
4350 &source_path,
4351 &serde_json::json!({
4352 "0": {
4353 "name": "f",
4354 "decl": { "start": { "line": 1, "column": 0 }, "end": { "line": 1, "column": 21 } },
4355 "loc": { "start": { "line": 1, "column": 0 }, "end": { "line": 1, "column": 21 } }
4356 }
4357 }),
4358 &serde_json::json!({ "0": 1 }),
4359 );
4360
4361 let coverage = load_istanbul_coverage(
4362 std::path::Path::new("coverage/coverage-final.json"),
4363 None,
4364 Some(temp.path()),
4365 )
4366 .expect("relative path must resolve against project_root");
4367 assert!(
4368 !coverage.files.is_empty(),
4369 "expected coverage to load via project_root resolution, got {} files",
4370 coverage.files.len()
4371 );
4372 }
4373
4374 #[test]
4375 fn load_istanbul_coverage_falls_back_to_decl_line_for_missing_fn_line() {
4376 let temp = tempfile::TempDir::new().unwrap();
4377 let source_path = temp.path().join("src/service.ts");
4378 std::fs::create_dir_all(source_path.parent().unwrap()).unwrap();
4379 std::fs::write(&source_path, "export class DataService {}\n").unwrap();
4380
4381 let coverage_path = temp.path().join("coverage-final.json");
4382 write_single_file_istanbul_fixture(
4383 &coverage_path,
4384 &source_path,
4385 &serde_json::json!({
4386 "0": {
4387 "name": "(anonymous_0)",
4388 "decl": {
4389 "start": { "line": 5, "column": 2 },
4390 "end": { "line": 5, "column": 13 }
4391 },
4392 "loc": {
4393 "start": { "line": 5, "column": 14 },
4394 "end": { "line": 11, "column": 3 }
4395 }
4396 },
4397 "1": {
4398 "name": "(anonymous_1)",
4399 "decl": {
4400 "start": { "line": 20, "column": 14 },
4401 "end": { "line": 20, "column": 25 }
4402 },
4403 "loc": {
4404 "start": { "line": 20, "column": 28 },
4405 "end": { "line": 22, "column": 2 }
4406 }
4407 }
4408 }),
4409 &serde_json::json!({
4410 "0": 1,
4411 "1": 0
4412 }),
4413 );
4414
4415 let coverage = load_istanbul_coverage(&coverage_path, None, None).unwrap();
4416 let canonical_source = dunce::canonicalize(&source_path).unwrap();
4417 let file_coverage = coverage.get(&canonical_source).unwrap();
4418
4419 assert_eq!(file_coverage.lookup("processData", 5, 0), Some(100.0));
4420 assert_eq!(file_coverage.lookup("handleSpecial", 20, 0), Some(0.0));
4421 }
4422
4423 #[test]
4424 fn load_istanbul_coverage_indexes_explicit_and_decl_lines() {
4425 let temp = tempfile::TempDir::new().unwrap();
4426 let source_path = temp.path().join("src/handler.ts");
4427 std::fs::create_dir_all(source_path.parent().unwrap()).unwrap();
4428 std::fs::write(&source_path, "export const handleClick = () => {}\n").unwrap();
4429
4430 let coverage_path = temp.path().join("coverage-final.json");
4431 write_single_file_istanbul_fixture(
4432 &coverage_path,
4433 &source_path,
4434 &serde_json::json!({
4435 "0": {
4436 "name": "handleClick",
4437 "line": 40,
4438 "decl": {
4439 "start": { "line": 22, "column": 13 },
4440 "end": { "line": 22, "column": 24 }
4441 },
4442 "loc": {
4443 "start": { "line": 40, "column": 27 },
4444 "end": { "line": 42, "column": 1 }
4445 }
4446 }
4447 }),
4448 &serde_json::json!({
4449 "0": 1
4450 }),
4451 );
4452
4453 let coverage = load_istanbul_coverage(&coverage_path, None, None).unwrap();
4454 let canonical_source = dunce::canonicalize(&source_path).unwrap();
4455 let file_coverage = coverage.get(&canonical_source).unwrap();
4456
4457 assert_eq!(file_coverage.lookup("handleClick", 40, 0), Some(100.0));
4458 assert_eq!(file_coverage.lookup("handleClick", 22, 13), Some(100.0));
4459 }
4460
4461 #[test]
4462 fn load_istanbul_coverage_matches_multiline_async_arrow_decl_alias() {
4463 let temp = tempfile::TempDir::new().unwrap();
4464 let source_path = temp.path().join("src/actor.ts");
4465 std::fs::create_dir_all(source_path.parent().unwrap()).unwrap();
4466 std::fs::write(
4467 &source_path,
4468 "export const elementsFrom = async (\n locator: AnyLocator,\n options?: { missingAsEmpty?: boolean },\n): Promise<HTMLElement[]> => {\n return [];\n};\n",
4469 )
4470 .unwrap();
4471
4472 let coverage_path = temp.path().join("coverage-final.json");
4473 write_single_file_istanbul_fixture(
4474 &coverage_path,
4475 &source_path,
4476 &serde_json::json!({
4477 "0": {
4478 "name": "(anonymous_0)",
4479 "line": 4,
4480 "decl": {
4481 "start": { "line": 1, "column": 28 },
4482 "end": { "line": 4, "column": 26 }
4483 },
4484 "loc": {
4485 "start": { "line": 4, "column": 27 },
4486 "end": { "line": 6, "column": 1 }
4487 }
4488 }
4489 }),
4490 &serde_json::json!({
4491 "0": 642
4492 }),
4493 );
4494
4495 let coverage = load_istanbul_coverage(&coverage_path, None, None).unwrap();
4496 let canonical_source = dunce::canonicalize(&source_path).unwrap();
4497 let file_coverage = coverage.get(&canonical_source).unwrap();
4498
4499 assert_eq!(file_coverage.lookup("elementsFrom", 1, 28), Some(100.0));
4500 }
4501
4502 #[test]
4503 fn istanbul_lookup_exact_match() {
4504 let mut functions = rustc_hash::FxHashMap::default();
4505 functions.insert(("handleClick".to_string(), 10, 0), 85.0);
4506 let fc = IstanbulFileCoverage { functions };
4507 assert!((fc.lookup("handleClick", 10, 0).unwrap() - 85.0).abs() < f64::EPSILON);
4508 }
4509
4510 #[test]
4511 fn istanbul_lookup_fuzzy_match_within_offset() {
4512 let mut functions = rustc_hash::FxHashMap::default();
4513 functions.insert(("handleClick".to_string(), 10, 0), 72.0);
4514 let fc = IstanbulFileCoverage { functions };
4515 assert!((fc.lookup("handleClick", 11, 0).unwrap() - 72.0).abs() < f64::EPSILON);
4516 assert!((fc.lookup("handleClick", 12, 0).unwrap() - 72.0).abs() < f64::EPSILON);
4517 }
4518
4519 #[test]
4520 fn istanbul_lookup_fuzzy_match_outside_offset() {
4521 let mut functions = rustc_hash::FxHashMap::default();
4522 functions.insert(("handleClick".to_string(), 10, 0), 72.0);
4523 let fc = IstanbulFileCoverage { functions };
4524 assert!(fc.lookup("handleClick", 13, 0).is_none());
4525 }
4526
4527 #[test]
4528 fn istanbul_lookup_name_mismatch() {
4529 let mut functions = rustc_hash::FxHashMap::default();
4530 functions.insert(("handleClick".to_string(), 10, 0), 85.0);
4531 let fc = IstanbulFileCoverage { functions };
4532 assert!(fc.lookup("handleSubmit", 10, 0).is_none());
4533 }
4534
4535 #[test]
4536 fn istanbul_lookup_empty() {
4537 let fc = IstanbulFileCoverage {
4538 functions: rustc_hash::FxHashMap::default(),
4539 };
4540 assert!(fc.lookup("anything", 1, 0).is_none());
4541 }
4542
4543 #[test]
4544 fn istanbul_lookup_fuzzy_picks_closest() {
4545 let mut functions = rustc_hash::FxHashMap::default();
4546 functions.insert(("render".to_string(), 8, 0), 60.0);
4547 functions.insert(("render".to_string(), 12, 0), 90.0);
4548 let fc = IstanbulFileCoverage { functions };
4549 let result = fc.lookup("render", 10, 0);
4550 assert!(result.is_some());
4551 let pct = result.unwrap();
4552 assert!((pct - 60.0).abs() < f64::EPSILON || (pct - 90.0).abs() < f64::EPSILON);
4553 }
4554
4555 #[test]
4556 fn istanbul_lookup_anonymous_fallback_single_candidate() {
4557 let mut functions = rustc_hash::FxHashMap::default();
4558 functions.insert(("(anonymous_0)".to_string(), 28, 0), 75.0);
4559 let fc = IstanbulFileCoverage { functions };
4560 assert!((fc.lookup("myHandler", 28, 0).unwrap() - 75.0).abs() < f64::EPSILON);
4561 assert!((fc.lookup("myHandler", 30, 0).unwrap() - 75.0).abs() < f64::EPSILON);
4562 }
4563
4564 #[test]
4565 fn istanbul_lookup_anonymous_fallback_rejects_nearby_far_column() {
4566 let mut functions = rustc_hash::FxHashMap::default();
4567 functions.insert(("(anonymous_0)".to_string(), 4, 28), 75.0);
4568 let fc = IstanbulFileCoverage { functions };
4569
4570 assert!(fc.lookup("declaredHelper", 3, 0).is_none());
4571 }
4572
4573 #[test]
4574 fn istanbul_lookup_anonymous_fallback_picks_closest_when_lines_differ() {
4575 let mut functions = rustc_hash::FxHashMap::default();
4576 functions.insert(("(anonymous_0)".to_string(), 28, 0), 75.0);
4577 functions.insert(("(anonymous_1)".to_string(), 29, 0), 50.0);
4578 let fc = IstanbulFileCoverage { functions };
4579 assert!((fc.lookup("myHandler", 28, 0).unwrap() - 75.0).abs() < f64::EPSILON);
4580 }
4581
4582 #[test]
4583 fn istanbul_lookup_anonymous_fallback_picks_closest_by_col_on_same_line() {
4584 let mut functions = rustc_hash::FxHashMap::default();
4585 functions.insert(("(anonymous_0)".to_string(), 1, 23), 90.0); functions.insert(("(anonymous_1)".to_string(), 1, 43), 10.0); let fc = IstanbulFileCoverage { functions };
4588 assert!((fc.lookup("<arrow>", 1, 43).unwrap() - 10.0).abs() < f64::EPSILON);
4589 assert!((fc.lookup("<arrow>", 1, 23).unwrap() - 90.0).abs() < f64::EPSILON);
4590 }
4591
4592 #[test]
4593 fn istanbul_lookup_anonymous_fallback_bails_only_on_true_tie() {
4594 let mut functions = rustc_hash::FxHashMap::default();
4595 functions.insert(("(anonymous_0)".to_string(), 27, 0), 75.0);
4596 functions.insert(("(anonymous_1)".to_string(), 29, 0), 50.0);
4597 let fc = IstanbulFileCoverage { functions };
4598 assert!(fc.lookup("myHandler", 28, 0).is_none());
4599 }
4600
4601 #[test]
4602 fn istanbul_lookup_anonymous_fallback_outside_offset() {
4603 let mut functions = rustc_hash::FxHashMap::default();
4604 functions.insert(("(anonymous_0)".to_string(), 28, 0), 75.0);
4605 let fc = IstanbulFileCoverage { functions };
4606 assert!(fc.lookup("myHandler", 31, 0).is_none());
4607 }
4608
4609 #[test]
4610 fn istanbul_lookup_named_match_beats_nearby_anonymous() {
4611 let mut functions = rustc_hash::FxHashMap::default();
4612 functions.insert(("handleClick".to_string(), 10, 0), 90.0);
4613 functions.insert(("(anonymous_7)".to_string(), 11, 0), 10.0);
4614 let fc = IstanbulFileCoverage { functions };
4615 assert!((fc.lookup("handleClick", 10, 0).unwrap() - 90.0).abs() < f64::EPSILON);
4616 }
4617
4618 #[test]
4619 fn build_test_refs_empty() {
4620 let exports: Vec<fallow_graph::graph::ExportSymbol> = vec![];
4621 let modules: Vec<fallow_graph::graph::ModuleNode> = vec![];
4622 let refs = build_test_referenced_exports(&exports, &modules);
4623 assert!(refs.is_empty());
4624 }
4625
4626 #[test]
4627 fn build_test_refs_empty_inputs() {
4628 let exports: Vec<fallow_graph::graph::ExportSymbol> = vec![];
4629 let modules: Vec<fallow_graph::graph::ModuleNode> = vec![];
4630 let refs = build_test_referenced_exports(&exports, &modules);
4631 assert!(refs.is_empty());
4632 }
4633
4634 #[test]
4635 fn istanbul_crap_empty_complexity() {
4636 let result = compute_crap_scores_istanbul(&[], None, false);
4637 assert!((result.max_crap).abs() < f64::EPSILON);
4638 assert_eq!(result.above_threshold, 0);
4639 assert_eq!(result.matched, 0);
4640 assert_eq!(result.total, 0);
4641 }
4642
4643 #[test]
4644 fn istanbul_crap_match_statistics() {
4645 let funcs = vec![make_fn_complexity(5), {
4646 let mut f = make_fn_complexity(3);
4647 f.name = "other_fn".into();
4648 f.line = 10;
4649 f
4650 }];
4651 let mut functions = rustc_hash::FxHashMap::default();
4652 functions.insert(("test_fn".to_string(), 1, 0), 80.0);
4653 let file_cov = IstanbulFileCoverage { functions };
4654 let result = compute_crap_scores_istanbul(&funcs, Some(&file_cov), true);
4655 assert_eq!(result.matched, 1);
4656 assert_eq!(result.total, 2);
4657 }
4658
4659 #[test]
4660 fn estimated_crap_multiple_functions_mixed_coverage() {
4661 let funcs = vec![
4662 make_fn_complexity(10), {
4664 let mut f = make_fn_complexity(3);
4665 f.name = "helper".into();
4666 f.line = 20;
4667 f
4668 },
4669 ];
4670 let mut refs = rustc_hash::FxHashSet::default();
4671 refs.insert("test_fn".to_string());
4672 let result = compute_crap_scores_estimated(
4673 &funcs,
4674 &refs,
4675 true,
4676 fallow_output::CoverageSource::Estimated,
4677 );
4678 let (max, above) = (result.max_crap, result.above_threshold);
4679 assert!(max > 10.0);
4680 assert_eq!(above, 0);
4681 }
4682
4683 #[test]
4684 fn binary_crap_test_reachable() {
4685 let funcs = vec![make_fn_complexity(10)];
4686 let (max, above) = compute_crap_scores_binary(&funcs, true);
4687 assert!((max - 10.0).abs() < f64::EPSILON);
4688 assert_eq!(above, 0);
4689 }
4690
4691 #[test]
4692 fn binary_crap_not_reachable() {
4693 let funcs = vec![make_fn_complexity(6)];
4694 let (max, above) = compute_crap_scores_binary(&funcs, false);
4695 assert!((max - 42.0).abs() < f64::EPSILON);
4696 assert_eq!(above, 1);
4697 }
4698
4699 #[test]
4700 fn binary_crap_threshold_boundary() {
4701 let funcs = vec![make_fn_complexity(5)];
4702 let (max, above) = compute_crap_scores_binary(&funcs, false);
4703 assert!((max - 30.0).abs() < f64::EPSILON);
4704 assert_eq!(above, 1);
4705 }
4706
4707 #[test]
4708 fn binary_crap_empty() {
4709 let (max, above) = compute_crap_scores_binary(&[], true);
4710 assert!((max).abs() < f64::EPSILON);
4711 assert_eq!(above, 0);
4712 }
4713
4714 #[test]
4715 fn binary_crap_multiple_functions() {
4716 let funcs = vec![make_fn_complexity(3), make_fn_complexity(8)];
4717 let (max, above) = compute_crap_scores_binary(&funcs, false);
4718 assert!((max - 72.0).abs() < f64::EPSILON);
4719 assert_eq!(above, 1);
4720 }
4721}