use fallow_output::{DirectCallerEvidence, DirectCallerSymbolEvidence, FileHealthScore};
use crate::module_graph::StaticTestCoverage;
use super::coverage_gaps::compute_coverage_gaps;
pub(super) use super::coverage_gaps::{CoverageGapData, build_coverage_summary};
use super::coverage_input::{CoverageInput, read_coverage_input};
use super::test_reach::build_test_referenced_exports;
use super::threshold_overrides::ThresholdOverrideResolver;
const MAX_FUNCTION_EVIDENCE: usize = 3;
pub struct FileScoreOutput {
pub(crate) scores: Vec<FileHealthScore>,
pub(crate) coverage: CoverageGapData,
pub(crate) circular_files: rustc_hash::FxHashSet<std::path::PathBuf>,
pub(crate) top_complex_fns: rustc_hash::FxHashMap<std::path::PathBuf, Vec<(String, u32, u16)>>,
pub(crate) crap_target_fns: rustc_hash::FxHashMap<std::path::PathBuf, Vec<(String, u32, u16)>>,
pub(crate) entry_points: rustc_hash::FxHashSet<std::path::PathBuf>,
pub(crate) value_export_counts: rustc_hash::FxHashMap<std::path::PathBuf, usize>,
pub(crate) unused_export_names: rustc_hash::FxHashMap<std::path::PathBuf, Vec<String>>,
pub(crate) cycle_members: rustc_hash::FxHashMap<std::path::PathBuf, Vec<std::path::PathBuf>>,
pub(crate) direct_callers: rustc_hash::FxHashMap<std::path::PathBuf, Vec<DirectCallerEvidence>>,
pub(crate) analysis_counts: crate::vital_signs::AnalysisCounts,
pub(crate) prop_drilling_chains: Vec<fallow_types::output_dead_code::PropDrillingChainFinding>,
pub(crate) render_fan_in: Option<fallow_types::results::RenderFanInMetric>,
pub(crate) analysis_snapshot: AnalysisCountsSnapshot,
pub(crate) istanbul_matched: usize,
pub(crate) istanbul_total: usize,
pub(crate) istanbul_files_joined: usize,
pub(crate) istanbul_files_total: usize,
pub(crate) coverage_input_format: Option<fallow_output::CoverageInputFormat>,
pub(crate) per_function_crap: rustc_hash::FxHashMap<std::path::PathBuf, Vec<PerFunctionCrap>>,
pub(crate) template_inherit_provenance:
rustc_hash::FxHashMap<std::path::PathBuf, std::path::PathBuf>,
pub(crate) static_links: Option<crate::change_coupling::StaticLinkIndex>,
}
struct FileScoreOutputParts<'a> {
graph: &'a fallow_graph::graph::ModuleGraph,
with_static_links: bool,
file_paths: &'a rustc_hash::FxHashMap<crate::discover::FileId, &'a std::path::PathBuf>,
results: &'a crate::results::AnalysisResults,
scores: Vec<FileHealthScore>,
coverage: CoverageGapData,
circular_files: rustc_hash::FxHashSet<std::path::PathBuf>,
top_complex_fns: rustc_hash::FxHashMap<std::path::PathBuf, Vec<(String, u32, u16)>>,
crap_target_fns: rustc_hash::FxHashMap<std::path::PathBuf, Vec<(String, u32, u16)>>,
entry_points: rustc_hash::FxHashSet<std::path::PathBuf>,
value_export_counts: rustc_hash::FxHashMap<std::path::PathBuf, usize>,
unused_export_names: rustc_hash::FxHashMap<std::path::PathBuf, Vec<String>>,
cycle_members: rustc_hash::FxHashMap<std::path::PathBuf, Vec<std::path::PathBuf>>,
direct_callers: rustc_hash::FxHashMap<std::path::PathBuf, Vec<DirectCallerEvidence>>,
istanbul_matched: usize,
istanbul_total: usize,
istanbul_files_joined: usize,
istanbul_files_total: usize,
coverage_input_format: Option<fallow_output::CoverageInputFormat>,
per_function_crap: rustc_hash::FxHashMap<std::path::PathBuf, Vec<PerFunctionCrap>>,
template_inherit: rustc_hash::FxHashMap<crate::discover::FileId, TemplateInheritContext>,
}
#[derive(Clone, Default)]
pub struct AnalysisCountsSnapshot {
unused_file_paths: Vec<std::path::PathBuf>,
unused_export_paths: Vec<std::path::PathBuf>,
unused_dep_package_paths: Vec<std::path::PathBuf>,
circular_dep_groups: Vec<Vec<std::path::PathBuf>>,
module_export_counts: rustc_hash::FxHashMap<std::path::PathBuf, usize>,
}
impl AnalysisCountsSnapshot {
pub(crate) fn counts_for(
&self,
subset: &crate::health::SubsetFilter<'_>,
defaults: &crate::vital_signs::AnalysisCounts,
) -> crate::vital_signs::AnalysisCounts {
if subset.is_full() {
return *defaults;
}
let dead_files = self
.unused_file_paths
.iter()
.filter(|p| subset.matches(p))
.count();
let dead_exports = self
.unused_export_paths
.iter()
.filter(|p| subset.matches(p))
.count();
let unused_deps = self
.unused_dep_package_paths
.iter()
.filter(|dep_path| dep_in_subset(subset, dep_path))
.count();
let circular_deps = self
.circular_dep_groups
.iter()
.filter(|cycle| cycle.iter().any(|p| subset.matches(p)))
.count();
let total_exports = self
.module_export_counts
.iter()
.filter(|(p, _)| subset.matches(p))
.map(|(_, n)| *n)
.sum();
crate::vital_signs::AnalysisCounts {
total_exports,
dead_files,
dead_exports,
unused_deps,
circular_deps,
total_deps: defaults.total_deps,
}
}
}
fn dep_in_subset(subset: &crate::health::SubsetFilter<'_>, dep_path: &std::path::Path) -> bool {
match subset {
crate::health::SubsetFilter::Full => true,
crate::health::SubsetFilter::Paths(set) => {
let Some(workspace_root) = dep_path.parent() else {
return false;
};
set.iter().any(|p| p.starts_with(workspace_root))
}
}
}
#[expect(
clippy::cast_possible_truncation,
reason = "line count is bounded by source file size"
)]
fn aggregate_complexity(module: &crate::source::ModuleInfo) -> (u32, u32, usize, u32) {
let cyc: u32 = module
.complexity
.iter()
.map(|f| u32::from(f.cyclomatic))
.sum();
let cog: u32 = module
.complexity
.iter()
.map(|f| u32::from(f.cognitive))
.sum();
let funcs = module.complexity.len();
let lines = module.line_offsets.len() as u32;
(cyc, cog, funcs, lines)
}
fn compute_dead_code_ratio(
path: &std::path::Path,
exports: &[fallow_graph::graph::ExportSymbol],
unused_files: &rustc_hash::FxHashSet<&std::path::Path>,
unused_exports_by_path: &rustc_hash::FxHashMap<&std::path::Path, usize>,
) -> f64 {
if unused_files.contains(path) {
return 1.0;
}
let value_exports = exports.iter().filter(|e| !e.is_type_only).count();
if value_exports == 0 {
return 0.0;
}
let unused = unused_exports_by_path.get(path).copied().unwrap_or(0);
(unused as f64 / value_exports as f64).min(1.0)
}
fn compute_complexity_density(total_cyclomatic: u32, lines: u32) -> f64 {
if lines > 0 {
f64::from(total_cyclomatic) / f64::from(lines)
} else {
0.0
}
}
const CRAP_THRESHOLD: f64 = 30.0;
#[derive(Clone, Copy)]
pub(super) struct CrapScoreThresholds<'a> {
pub(super) resolver: &'a ThresholdOverrideResolver,
pub(super) enforce_crap: bool,
}
pub(super) struct CrapCeilingLookup<'a> {
resolver: &'a ThresholdOverrideResolver,
relative: &'a std::path::Path,
enforce_crap: bool,
suppressions: &'a [crate::suppress::Suppression],
}
#[derive(Debug, Default)]
struct CrapThresholdSignals {
above: usize,
exempted: usize,
min_ceiling: Option<f64>,
suppressed: usize,
unsuppressed_max: f64,
unsuppressed_min_ceiling: Option<f64>,
eligible_functions: Vec<(String, u32, u16)>,
}
impl<'a> CrapCeilingLookup<'a> {
pub(super) fn new(
thresholds: CrapScoreThresholds<'a>,
relative: &'a std::path::Path,
suppressions: &'a [crate::suppress::Suppression],
) -> Self {
Self {
resolver: thresholds.resolver,
relative,
enforce_crap: thresholds.enforce_crap,
suppressions,
}
}
fn observe(
&self,
function: &fallow_types::extract::FunctionComplexity,
crap_rounded: f64,
signals: &mut CrapThresholdSignals,
) {
let ceiling =
self.resolver
.effective_max_crap(self.relative, &function.name, function.line);
signals.min_ceiling = Some(signals.min_ceiling.map_or(ceiling, |m| m.min(ceiling)));
if (!self.enforce_crap || crap_rounded < ceiling) && crap_rounded >= CRAP_THRESHOLD {
signals.exempted += 1;
}
if crate::suppress::is_suppressed(
self.suppressions,
function.line,
crate::suppress::IssueKind::Complexity,
) {
signals.suppressed += 1;
return;
}
signals.unsuppressed_max = signals.unsuppressed_max.max(crap_rounded);
signals.unsuppressed_min_ceiling = Some(
signals
.unsuppressed_min_ceiling
.map_or(ceiling, |minimum| minimum.min(ceiling)),
);
if self.enforce_crap && crap_rounded >= ceiling {
signals.above += 1;
signals.eligible_functions.push((
function.name.clone(),
function.line,
function.cognitive,
));
}
}
}
#[derive(Debug, Clone, Copy)]
pub struct PerFunctionCrap {
pub(crate) line: u32,
pub(crate) col: u32,
pub(crate) crap: f64,
pub(crate) coverage_pct: Option<f64>,
pub(crate) coverage_tier: fallow_output::CoverageTier,
pub(crate) coverage_source: fallow_output::CoverageSource,
}
#[derive(Debug)]
struct IstanbulCrapResult {
pub max_crap: f64,
pub signals: CrapThresholdSignals,
pub matched: usize,
pub total: usize,
pub per_function: Vec<PerFunctionCrap>,
}
fn compute_crap_scores_istanbul(
complexity: &[fallow_types::extract::FunctionComplexity],
file_coverage: Option<&IstanbulFileCoverage>,
is_test_reachable: bool,
ceilings: &CrapCeilingLookup<'_>,
) -> IstanbulCrapResult {
if complexity.is_empty() {
return IstanbulCrapResult {
max_crap: 0.0,
signals: CrapThresholdSignals::default(),
matched: 0,
total: 0,
per_function: Vec::new(),
};
}
let mut max = 0.0_f64;
let mut signals = CrapThresholdSignals::default();
let mut matched = 0usize;
let mut total = 0usize;
let mut per_function = Vec::with_capacity(complexity.len());
for f in complexity {
if fallow_types::extract::is_synthetic_template_unit(&f.name)
|| fallow_types::extract::is_synthetic_module_unit(&f.name)
{
continue;
}
total += 1;
let cc = f64::from(ceilings.resolver.gate_cyclomatic(f));
let (crap, coverage_pct, tier, source) =
crap_for_function(f, cc, file_coverage, is_test_reachable, &mut matched);
let crap_rounded = (crap * 10.0).round() / 10.0;
max = max.max(crap);
ceilings.observe(f, crap_rounded, &mut signals);
per_function.push(PerFunctionCrap {
line: f.line,
col: f.col,
crap: crap_rounded,
coverage_pct,
coverage_tier: tier,
coverage_source: source,
});
}
IstanbulCrapResult {
max_crap: (max * 10.0).round() / 10.0,
signals,
matched,
total,
per_function,
}
}
#[expect(
clippy::suboptimal_flops,
reason = "cc * cc + cc matches the CRAP formula specification"
)]
fn crap_for_function(
f: &fallow_types::extract::FunctionComplexity,
cc: f64,
file_coverage: Option<&IstanbulFileCoverage>,
is_test_reachable: bool,
matched: &mut usize,
) -> (
f64,
Option<f64>,
fallow_output::CoverageTier,
fallow_output::CoverageSource,
) {
let lookup = file_coverage.and_then(|fc| fc.lookup_function(f));
if let Some(cov_pct) = lookup {
*matched += 1;
return (
crap_formula(cc, cov_pct),
Some(cov_pct),
fallow_output::CoverageTier::from_pct(cov_pct),
fallow_output::CoverageSource::Istanbul,
);
}
if is_test_reachable {
return (
crap_formula(cc, INDIRECT_TEST_COVERAGE_ESTIMATE),
None,
fallow_output::CoverageTier::from_pct(INDIRECT_TEST_COVERAGE_ESTIMATE),
fallow_output::CoverageSource::Estimated,
);
}
(
cc * cc + cc,
None,
fallow_output::CoverageTier::None,
fallow_output::CoverageSource::Estimated,
)
}
const DIRECT_TEST_COVERAGE_ESTIMATE: f64 = 85.0;
const INDIRECT_TEST_COVERAGE_ESTIMATE: f64 = 40.0;
const MAX_DIRECT_CALLER_EVIDENCE: usize = 5;
#[derive(Debug)]
struct EstimatedCrapResult {
pub max_crap: f64,
pub signals: CrapThresholdSignals,
pub per_function: Vec<PerFunctionCrap>,
}
fn compute_crap_scores_estimated(
complexity: &[fallow_types::extract::FunctionComplexity],
test_referenced_exports: &rustc_hash::FxHashSet<String>,
is_test_reachable: bool,
coverage_source: fallow_output::CoverageSource,
ceilings: &CrapCeilingLookup<'_>,
) -> EstimatedCrapResult {
if complexity.is_empty() {
return EstimatedCrapResult {
max_crap: 0.0,
signals: CrapThresholdSignals::default(),
per_function: Vec::new(),
};
}
let mut max = 0.0_f64;
let mut signals = CrapThresholdSignals::default();
let mut per_function = Vec::with_capacity(complexity.len());
for f in complexity {
if fallow_types::extract::is_synthetic_template_unit(&f.name)
|| fallow_types::extract::is_synthetic_module_unit(&f.name)
{
continue;
}
let cc = f64::from(ceilings.resolver.gate_cyclomatic(f));
let estimated_coverage = if test_referenced_exports.contains(f.identity_name()) {
DIRECT_TEST_COVERAGE_ESTIMATE
} else if is_test_reachable {
INDIRECT_TEST_COVERAGE_ESTIMATE
} else {
0.0
};
let crap = crap_formula(cc, estimated_coverage);
let crap_rounded = (crap * 10.0).round() / 10.0;
max = max.max(crap);
ceilings.observe(f, crap_rounded, &mut signals);
per_function.push(PerFunctionCrap {
line: f.line,
col: f.col,
crap: crap_rounded,
coverage_pct: None,
coverage_tier: fallow_output::CoverageTier::from_pct(estimated_coverage),
coverage_source,
});
}
EstimatedCrapResult {
max_crap: (max * 10.0).round() / 10.0,
signals,
per_function,
}
}
#[derive(Debug, Clone)]
struct TemplateInheritContext {
pub is_test_reachable: bool,
pub test_referenced_exports: rustc_hash::FxHashSet<String>,
pub provenance_owner: std::path::PathBuf,
}
fn build_template_inherit_contexts(
graph: &fallow_graph::graph::ModuleGraph,
test_coverage: StaticTestCoverage<'_>,
module_by_id: &rustc_hash::FxHashMap<crate::discover::FileId, &crate::source::ModuleInfo>,
file_paths: &rustc_hash::FxHashMap<crate::discover::FileId, &std::path::PathBuf>,
) -> rustc_hash::FxHashMap<crate::discover::FileId, TemplateInheritContext> {
let mut out = rustc_hash::FxHashMap::default();
for node in &graph.modules {
if let Some(context) =
template_inherit_context_for_node(node, graph, test_coverage, module_by_id, file_paths)
{
out.insert(node.file_id, context);
}
}
out
}
fn template_inherit_context_for_node(
node: &fallow_graph::graph::ModuleNode,
graph: &fallow_graph::graph::ModuleGraph,
test_coverage: StaticTestCoverage<'_>,
module_by_id: &rustc_hash::FxHashMap<crate::discover::FileId, &crate::source::ModuleInfo>,
file_paths: &rustc_hash::FxHashMap<crate::discover::FileId, &std::path::PathBuf>,
) -> Option<TemplateInheritContext> {
if !is_template_inherit_candidate(node, module_by_id, file_paths) {
return None;
}
let importers = graph.reverse_deps.get(node.file_id.0 as usize)?;
template_inherit_context_from_importers(
importers,
graph,
test_coverage,
module_by_id,
file_paths,
)
}
fn is_template_inherit_candidate(
node: &fallow_graph::graph::ModuleNode,
module_by_id: &rustc_hash::FxHashMap<crate::discover::FileId, &crate::source::ModuleInfo>,
file_paths: &rustc_hash::FxHashMap<crate::discover::FileId, &std::path::PathBuf>,
) -> bool {
let Some(path) = file_paths.get(&node.file_id) else {
return false;
};
if !path
.extension()
.and_then(|ext| ext.to_str())
.is_some_and(|ext| ext.eq_ignore_ascii_case("html"))
{
return false;
}
module_by_id.get(&node.file_id).is_some_and(|module| {
module
.complexity
.iter()
.any(|finding| finding.name.as_str() == "<template>")
})
}
fn template_inherit_context_from_importers(
importers: &[crate::discover::FileId],
graph: &fallow_graph::graph::ModuleGraph,
test_coverage: StaticTestCoverage<'_>,
module_by_id: &rustc_hash::FxHashMap<crate::discover::FileId, &crate::source::ModuleInfo>,
file_paths: &rustc_hash::FxHashMap<crate::discover::FileId, &std::path::PathBuf>,
) -> Option<TemplateInheritContext> {
let mut any_reachable = false;
let mut combined_refs = rustc_hash::FxHashSet::default();
let mut provenance: Option<std::path::PathBuf> = None;
let mut first_owner: Option<std::path::PathBuf> = None;
for &importer_id in importers {
let Some((owner_node, owner_path)) =
template_owner(importer_id, graph, module_by_id, file_paths)
else {
continue;
};
if first_owner.is_none() {
first_owner = Some((*owner_path).clone());
}
if test_coverage.covers_file(owner_node.file_id) {
any_reachable = true;
provenance.get_or_insert_with(|| (*owner_path).clone());
let refs = build_test_referenced_exports(
owner_node.file_id,
&owner_node.exports,
test_coverage,
);
combined_refs.extend(refs);
}
}
let provenance_owner = provenance.or(first_owner)?;
Some(TemplateInheritContext {
is_test_reachable: any_reachable,
test_referenced_exports: combined_refs,
provenance_owner,
})
}
fn template_owner<'a>(
importer_id: crate::discover::FileId,
graph: &'a fallow_graph::graph::ModuleGraph,
module_by_id: &rustc_hash::FxHashMap<crate::discover::FileId, &crate::source::ModuleInfo>,
file_paths: &'a rustc_hash::FxHashMap<crate::discover::FileId, &std::path::PathBuf>,
) -> Option<(&'a fallow_graph::graph::ModuleNode, &'a std::path::PathBuf)> {
let owner_node = graph.modules.get(importer_id.0 as usize)?;
let owner_path = *file_paths.get(&importer_id)?;
if !is_template_owner_path(owner_path) || graph.test_entry_points.contains(&importer_id) {
return None;
}
let owner_has_component = module_by_id
.get(&importer_id)
.is_some_and(|module| module.has_angular_component_template_url);
owner_has_component.then_some((owner_node, owner_path))
}
fn is_template_owner_path(path: &std::path::Path) -> bool {
path.extension()
.and_then(|ext| ext.to_str())
.is_some_and(|ext| {
matches!(
ext.to_ascii_lowercase().as_str(),
"ts" | "tsx" | "mts" | "cts"
)
})
}
fn collect_direct_callers(
graph: &fallow_graph::graph::ModuleGraph,
file_paths: &rustc_hash::FxHashMap<crate::discover::FileId, &std::path::PathBuf>,
) -> rustc_hash::FxHashMap<std::path::PathBuf, Vec<DirectCallerEvidence>> {
let mut callers_by_target = rustc_hash::FxHashMap::default();
for node in &graph.modules {
let Some(target_path) = file_paths.get(&node.file_id) else {
continue;
};
let mut callers = graph
.direct_importer_summaries(node.file_id)
.into_iter()
.filter_map(|summary| {
file_paths
.get(&summary.source)
.map(|caller_path| DirectCallerEvidence {
path: (*caller_path).clone(),
symbols: summary
.symbols
.into_iter()
.map(|symbol| DirectCallerSymbolEvidence {
imported: symbol.imported,
local: symbol.local,
type_only: symbol.type_only,
})
.collect(),
})
})
.collect::<Vec<_>>();
callers.sort_by(|a, b| a.path.cmp(&b.path));
callers.truncate(MAX_DIRECT_CALLER_EVIDENCE);
if !callers.is_empty() {
callers_by_target.insert((*target_path).clone(), callers);
}
}
callers_by_target
}
#[expect(
clippy::suboptimal_flops,
reason = "explicit multiplication matches the CRAP formula specification"
)]
fn crap_formula(cc: f64, coverage_pct: f64) -> f64 {
let uncovered = 1.0 - coverage_pct / 100.0;
cc * cc * uncovered * uncovered * uncovered + cc
}
const ANONYMOUS_FALLBACK_MAX_COLUMN_DRIFT: u32 = 16;
const ALIAS_FUZZ_MAX_LINE_DRIFT: u32 = 2;
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
struct IstanbulPosition {
line: u32,
col: u32,
}
impl IstanbulPosition {
const fn new(line: u32, col: u32) -> Self {
Self { line, col }
}
const fn distance_from(self, target: Self) -> (u32, u32) {
(
self.line.abs_diff(target.line),
self.col.abs_diff(target.col),
)
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
struct IstanbulSpan {
start: IstanbulPosition,
end: IstanbulPosition,
}
impl IstanbulSpan {
fn from_entry(
fn_entry: &oxc_coverage_instrument::FnEntry,
source_index: Option<&IstanbulSourceIndex<'_>>,
) -> Option<Self> {
let start = normalized_istanbul_position(
fn_entry.loc.start.line,
fn_entry.loc.start.column,
source_index,
)?;
let end = normalized_istanbul_position(
fn_entry.loc.end.line,
fn_entry.loc.end.column,
source_index,
)?;
(start.line > 0 && end.line > 0 && start < end).then_some(Self { start, end })
}
fn header_from_entry(
fn_entry: &oxc_coverage_instrument::FnEntry,
source_index: Option<&IstanbulSourceIndex<'_>>,
) -> Option<Self> {
let start = normalized_istanbul_position(
fn_entry.decl.start.line,
fn_entry.decl.start.column,
source_index,
)?;
let end = normalized_istanbul_position(
fn_entry.loc.start.line,
fn_entry.loc.start.column,
source_index,
)?;
(start.line > 0 && end.line > 0 && start < end).then_some(Self { start, end })
}
fn contains(self, position: IstanbulPosition) -> bool {
self.start <= position && position < self.end
}
fn strictly_contains(self, other: Self) -> bool {
self != other && self.start <= other.start && other.end <= self.end
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
struct IstanbulAlias {
position: IstanbulPosition,
primary: bool,
}
#[derive(Clone, Copy, Debug, Default)]
struct IstanbulAliasCounts {
primary: usize,
secondary: usize,
}
impl IstanbulAliasCounts {
fn add(&mut self, alias: IstanbulAlias) {
if alias.primary {
self.primary += 1;
} else {
self.secondary += 1;
}
}
const fn is_unique(self, alias: IstanbulAlias) -> bool {
if alias.primary {
self.primary == 1
} else {
self.primary == 0 && self.secondary == 1
}
}
const fn has_secondary_only_collision(self) -> bool {
self.primary == 0 && self.secondary > 1
}
}
fn is_anonymous_istanbul_name(name: &str) -> bool {
name.starts_with("(anonymous_")
}
struct IstanbulFunctionCoverage {
name: String,
coverage_pct: f64,
aliases: Vec<IstanbulAlias>,
decl_start: IstanbulPosition,
header_holds_other_fn: bool,
header_span: Option<IstanbulSpan>,
body_span: Option<IstanbulSpan>,
}
impl IstanbulFunctionCoverage {
fn answers_to(&self, name: &str) -> bool {
if self.name == name {
return true;
}
accessor_property_name(&self.name) == Some(name)
}
fn nearest_alias(
&self,
target: IstanbulPosition,
max_column_drift: Option<u32>,
) -> Option<(u32, u32)> {
self.aliases
.iter()
.filter_map(|alias| {
let distance = alias.position.distance_from(target);
if distance.0 > ALIAS_FUZZ_MAX_LINE_DRIFT {
return None;
}
if distance.0 > 0 && max_column_drift.is_some_and(|maximum| distance.1 > maximum) {
return None;
}
Some(distance)
})
.min()
}
}
pub struct IstanbulFileCoverage {
functions: Vec<IstanbulFunctionCoverage>,
alias_index: rustc_hash::FxHashMap<(String, u32, u32), usize>,
ambiguous_aliases: rustc_hash::FxHashSet<(String, u32, u32)>,
ambiguous_anonymous_aliases: rustc_hash::FxHashSet<IstanbulPosition>,
alias_lines: Vec<(u32, usize)>,
header_starts: Vec<(u32, usize)>,
max_header_height: u32,
relocated: bool,
}
fn mark_headers_holding_other_fns(functions: &mut [IstanbulFunctionCoverage]) {
let mut declarations: Vec<IstanbulPosition> = functions
.iter()
.map(|function| function.decl_start)
.collect();
declarations.sort_unstable();
for function in functions {
let Some(span) = function.header_span else {
continue;
};
let first = declarations.partition_point(|position| *position < span.start);
let past = declarations.partition_point(|position| *position < span.end);
function.header_holds_other_fn = past - first > 1;
}
}
struct IstanbulLineIndexes {
alias_lines: Vec<(u32, usize)>,
header_starts: Vec<(u32, usize)>,
max_header_height: u32,
}
impl IstanbulLineIndexes {
fn build(functions: &[IstanbulFunctionCoverage]) -> Self {
let mut alias_lines: Vec<(u32, usize)> = functions
.iter()
.enumerate()
.flat_map(|(function_index, function)| {
function
.aliases
.iter()
.map(move |alias| (alias.position.line, function_index))
})
.collect();
alias_lines.sort_unstable();
let mut header_starts: Vec<(u32, usize)> = Vec::new();
let mut max_header_height = 0;
for (function_index, function) in functions.iter().enumerate() {
if let Some(span) = function.header_span {
header_starts.push((span.start.line, function_index));
max_header_height = max_header_height.max(span.end.line - span.start.line);
}
}
header_starts.sort_unstable();
Self {
alias_lines,
header_starts,
max_header_height,
}
}
}
impl IstanbulFileCoverage {
pub fn lookup_function(
&self,
function: &fallow_types::extract::FunctionComplexity,
) -> Option<f64> {
if function.is_private_member {
return self.lookup_private(&function.name, function.line, function.col);
}
self.lookup(function.identity_name(), function.line, function.col)
}
fn new(mut functions: Vec<IstanbulFunctionCoverage>, relocated: bool) -> Self {
let mut named_alias_counts: rustc_hash::FxHashMap<
(String, IstanbulPosition),
IstanbulAliasCounts,
> = rustc_hash::FxHashMap::default();
let mut anonymous_alias_counts: rustc_hash::FxHashMap<
IstanbulPosition,
IstanbulAliasCounts,
> = rustc_hash::FxHashMap::default();
for function in &functions {
let is_anonymous = is_anonymous_istanbul_name(&function.name);
for alias in &function.aliases {
named_alias_counts
.entry((function.name.clone(), alias.position))
.or_default()
.add(*alias);
if is_anonymous {
anonymous_alias_counts
.entry(alias.position)
.or_default()
.add(*alias);
}
}
}
let mut ambiguous_aliases = rustc_hash::FxHashSet::default();
let mut ambiguous_anonymous_aliases = rustc_hash::FxHashSet::default();
for function in &mut functions {
let name = function.name.clone();
let is_anonymous = is_anonymous_istanbul_name(&name);
function.aliases.retain(|alias| {
let named = named_alias_counts
.get(&(name.clone(), alias.position))
.copied()
.unwrap_or_default();
let anonymous = is_anonymous
.then(|| anonymous_alias_counts.get(&alias.position).copied())
.flatten();
let unique = named.is_unique(*alias)
&& anonymous.is_none_or(|counts| counts.is_unique(*alias));
if unique {
return true;
}
if alias.primary {
ambiguous_aliases.insert((
name.clone(),
alias.position.line,
alias.position.col,
));
if anonymous.is_some_and(|counts| counts.primary > 1) {
ambiguous_anonymous_aliases.insert(alias.position);
}
} else {
if named.has_secondary_only_collision() {
ambiguous_aliases.insert((
name.clone(),
alias.position.line,
alias.position.col,
));
}
if anonymous.is_some_and(IstanbulAliasCounts::has_secondary_only_collision) {
ambiguous_anonymous_aliases.insert(alias.position);
}
}
false
});
}
let mut alias_index = rustc_hash::FxHashMap::default();
for (function_index, function) in functions.iter().enumerate() {
for alias in &function.aliases {
alias_index.insert(
(
function.name.clone(),
alias.position.line,
alias.position.col,
),
function_index,
);
if let Some(property) = accessor_property_name(&function.name) {
alias_index
.entry((
property.to_string(),
alias.position.line,
alias.position.col,
))
.or_insert(function_index);
}
}
}
mark_headers_holding_other_fns(&mut functions);
let indexes = IstanbulLineIndexes::build(&functions);
Self {
functions,
alias_index,
ambiguous_aliases,
ambiguous_anonymous_aliases,
alias_lines: indexes.alias_lines,
header_starts: indexes.header_starts,
max_header_height: indexes.max_header_height,
relocated,
}
}
fn fuzz_window(&self, target: IstanbulPosition) -> Vec<usize> {
let low = target.line.saturating_sub(ALIAS_FUZZ_MAX_LINE_DRIFT);
let high = target.line.saturating_add(ALIAS_FUZZ_MAX_LINE_DRIFT);
let first = self.alias_lines.partition_point(|(line, _)| *line < low);
let past = self.alias_lines.partition_point(|(line, _)| *line <= high);
let mut window: Vec<usize> = self.alias_lines[first..past]
.iter()
.map(|(_, function_index)| *function_index)
.collect();
window.sort_unstable();
window.dedup();
window
}
fn lookup_private(&self, name: &str, line: u32, col: u32) -> Option<f64> {
let exact_key = (name.to_string(), line, col);
if self.ambiguous_aliases.contains(&exact_key) {
return None;
}
if let Some(&function_index) = self.alias_index.get(&exact_key) {
return Some(self.functions[function_index].coverage_pct);
}
let target = IstanbulPosition::new(line, col);
let window = self.fuzz_window(target);
let signature_owners = self.header_spans_containing(target);
let mut candidates = self.named_candidates(name, target, &window, &signature_owners);
let (_, function_index) = candidates.next()?;
if candidates.next().is_some() {
return None;
}
Some(self.functions[function_index].coverage_pct)
}
fn named_candidates<'a>(
&'a self,
name: &'a str,
target: IstanbulPosition,
window: &'a [usize],
signature_owners: &'a [usize],
) -> impl Iterator<Item = ((u32, u32), usize)> + 'a {
window
.iter()
.copied()
.filter(move |function_index| self.functions[*function_index].answers_to(name))
.filter_map(move |function_index| {
self.functions[function_index]
.nearest_alias(target, None)
.map(|distance| (distance, function_index))
})
.filter(move |(distance, function_index)| {
*distance == (0, 0)
|| !self.foreign_signature_blocks(signature_owners, *function_index, target)
})
}
pub(crate) fn lookup(&self, name: &str, line: u32, col: u32) -> Option<f64> {
let exact_key = (name.to_string(), line, col);
if self.ambiguous_aliases.contains(&exact_key) {
return None;
}
if let Some(&function_index) = self.alias_index.get(&exact_key) {
return Some(self.functions[function_index].coverage_pct);
}
let target = IstanbulPosition::new(line, col);
let window = self.fuzz_window(target);
let signature_owners = self.header_spans_containing(target);
if let Some(function) = self
.named_candidates(name, target, &window, &signature_owners)
.min_by_key(|(distance, _)| *distance)
.map(|(_, function_index)| &self.functions[function_index])
{
return Some(function.coverage_pct);
}
if self.relocated
&& let Some(pct) = self.unambiguous_named_pct(name)
{
return Some(pct);
}
if self.ambiguous_anonymous_aliases.contains(&target) {
return self.unique_anonymous_header_match(&signature_owners);
}
let mut nearest_distance: Option<(u32, u32)> = None;
let mut nearest_functions = Vec::new();
for function_index in window {
let function = &self.functions[function_index];
if !is_anonymous_istanbul_name(&function.name) {
continue;
}
let Some(distance) =
function.nearest_alias(target, Some(ANONYMOUS_FALLBACK_MAX_COLUMN_DRIFT))
else {
continue;
};
if distance != (0, 0)
&& self.foreign_signature_blocks(&signature_owners, function_index, target)
{
continue;
}
match nearest_distance {
None => {
nearest_distance = Some(distance);
nearest_functions.push(function_index);
}
Some(previous) if distance < previous => {
nearest_distance = Some(distance);
nearest_functions.clear();
nearest_functions.push(function_index);
}
Some(previous) if distance == previous => {
nearest_functions.push(function_index);
}
Some(_) => {}
}
}
let established_match = match nearest_functions.as_slice() {
[] => None,
[function_index] => Some(self.functions[*function_index].coverage_pct),
tied => self.innermost_anonymous_match(tied, target),
};
if established_match.is_some() {
return established_match;
}
self.unique_anonymous_header_match(&signature_owners)
}
fn header_spans_containing(&self, target: IstanbulPosition) -> Vec<usize> {
let low = target.line.saturating_sub(self.max_header_height);
let first = self.header_starts.partition_point(|(line, _)| *line < low);
let past = self
.header_starts
.partition_point(|(line, _)| *line <= target.line);
self.header_starts[first..past]
.iter()
.filter(|(_, function_index)| {
self.functions[*function_index]
.header_span
.is_some_and(|span| span.contains(target))
})
.map(|(_, function_index)| *function_index)
.collect()
}
fn foreign_signature_blocks(
&self,
owners: &[usize],
candidate: usize,
target: IstanbulPosition,
) -> bool {
let function = &self.functions[candidate];
if function
.header_span
.is_some_and(|span| span.contains(target))
|| function.body_span.is_some_and(|span| span.contains(target))
{
return false;
}
owners.iter().any(|&owner| {
owner != candidate
&& self.functions[owner]
.header_span
.is_some_and(|span| span.contains(function.decl_start))
})
}
fn unique_anonymous_header_match(&self, signature_owners: &[usize]) -> Option<f64> {
let [function_index] = signature_owners else {
return None;
};
let function = &self.functions[*function_index];
if !is_anonymous_istanbul_name(&function.name) || function.header_holds_other_fn {
return None;
}
Some(function.coverage_pct)
}
fn innermost_anonymous_match(&self, tied: &[usize], target: IstanbulPosition) -> Option<f64> {
let containing: Option<Vec<_>> = tied
.iter()
.map(|&function_index| {
self.functions[function_index]
.body_span
.filter(|span| span.contains(target))
.map(|span| (function_index, span))
})
.collect();
let containing = containing?;
let mut winner = None;
for &(function_index, candidate_span) in &containing {
let is_strictly_innermost = containing.iter().all(|&(other_index, other_span)| {
other_index == function_index || other_span.strictly_contains(candidate_span)
});
if !is_strictly_innermost {
continue;
}
if winner.replace(function_index).is_some() {
return None;
}
}
winner.map(|function_index| self.functions[function_index].coverage_pct)
}
fn unambiguous_named_pct(&self, name: &str) -> Option<f64> {
let mut found: Option<f64> = None;
for function in &self.functions {
if !function.answers_to(name) {
continue;
}
match found {
None => found = Some(function.coverage_pct),
Some(previous) if previous.to_bits() == function.coverage_pct.to_bits() => {}
Some(_) => return None,
}
}
found
}
}
pub struct IstanbulCoverage {
files: rustc_hash::FxHashMap<std::path::PathBuf, IstanbulFileCoverage>,
format: fallow_output::CoverageInputFormat,
}
impl IstanbulCoverage {
pub fn get(&self, path: &std::path::Path) -> Option<&IstanbulFileCoverage> {
self.files.get(path)
}
pub fn file_count(&self) -> usize {
self.files.len()
}
pub const fn format(&self) -> fallow_output::CoverageInputFormat {
self.format
}
}
enum CrapCoverageResolution<'a> {
TemplateInherited(&'a TemplateInheritContext),
Istanbul {
file_coverage: Option<&'a IstanbulFileCoverage>,
},
StaticEstimated,
}
fn resolve_crap_coverage<'a>(
template_inherit: Option<&'a TemplateInheritContext>,
istanbul_coverage: Option<&'a IstanbulCoverage>,
path: &std::path::Path,
) -> CrapCoverageResolution<'a> {
if let Some(inherit_ctx) = template_inherit {
CrapCoverageResolution::TemplateInherited(inherit_ctx)
} else if let Some(istanbul) = istanbul_coverage {
let canonical = dunce::canonicalize(path).unwrap_or_else(|_| path.to_path_buf());
CrapCoverageResolution::Istanbul {
file_coverage: istanbul.get(&canonical),
}
} else {
CrapCoverageResolution::StaticEstimated
}
}
pub fn auto_detect_coverage(root: &std::path::Path) -> Option<std::path::PathBuf> {
let candidates = [
root.join("coverage/coverage-final.json"),
root.join(".nyc_output/coverage-final.json"),
];
candidates.into_iter().find(|p| p.is_file())
}
pub fn resolve_relative_to_root(
path: &std::path::Path,
project_root: Option<&std::path::Path>,
) -> std::path::PathBuf {
if fallow_types::path_util::is_absolute_path_any_platform(path) {
return path.to_path_buf();
}
match project_root {
Some(root) => root.join(path),
None => path.to_path_buf(),
}
}
#[cfg(test)]
fn load_istanbul_coverage(
path: &std::path::Path,
coverage_root: Option<&std::path::Path>,
project_root: Option<&std::path::Path>,
relocated: bool,
) -> Result<IstanbulCoverage, String> {
load_istanbul_coverage_for_sources(path, coverage_root, project_root, None, relocated)
}
pub(super) fn load_istanbul_coverage_for_sources(
path: &std::path::Path,
coverage_root: Option<&std::path::Path>,
project_root: Option<&std::path::Path>,
discovered_sources: Option<&rustc_hash::FxHashSet<std::path::PathBuf>>,
relocated: bool,
) -> Result<IstanbulCoverage, String> {
super::validate_coverage_root_absolute(coverage_root)?;
let resolved = resolve_relative_to_root(path, project_root);
match read_coverage_input(&resolved)? {
CoverageInput::Istanbul { file_path, json } => {
let raw = parse_coverage_map_tolerantly(&json).map_err(|e| {
format!(
"failed to parse coverage data from {}: {e}",
file_path.display()
)
})?;
let sources = CoverageMapSources {
coverage_root,
project_root,
discovered_sources,
relocated,
};
Ok(build_istanbul_coverage(
&raw,
&sources,
fallow_output::CoverageInputFormat::Istanbul,
))
}
CoverageInput::V8 { dump_files } => {
let scope = super::coverage_v8::V8ScriptScope {
coverage_root,
project_root,
discovered_sources,
};
let raw = super::coverage_v8::load_v8_coverage_map(&dump_files, &scope)?;
let sources = CoverageMapSources {
coverage_root: None,
project_root,
discovered_sources,
relocated,
};
Ok(build_istanbul_coverage(
&raw,
&sources,
fallow_output::CoverageInputFormat::V8,
))
}
}
}
#[derive(Clone, Copy)]
struct CoverageMapSources<'a> {
coverage_root: Option<&'a std::path::Path>,
project_root: Option<&'a std::path::Path>,
discovered_sources: Option<&'a rustc_hash::FxHashSet<std::path::PathBuf>>,
relocated: bool,
}
fn build_istanbul_coverage(
raw: &std::collections::BTreeMap<String, oxc_coverage_instrument::FileCoverage>,
sources: &CoverageMapSources<'_>,
format: fallow_output::CoverageInputFormat,
) -> IstanbulCoverage {
let CoverageMapSources {
coverage_root,
project_root,
discovered_sources,
relocated,
} = *sources;
let mut files = rustc_hash::FxHashMap::default();
for file_cov in raw.values() {
let raw_path = resolve_relative_to_root(std::path::Path::new(&file_cov.path), project_root);
let file_path = if let (Some(cov_root), Some(proj_root)) = (coverage_root, project_root) {
rebase_coverage_path(raw_path, cov_root, proj_root)
} else {
raw_path
};
let canonical = dunce::canonicalize(&file_path).unwrap_or(file_path);
let source = read_discovered_source(&canonical, discovered_sources, relocated);
let source_index = source
.as_deref()
.map(|source| IstanbulSourceIndex::new(source, &canonical));
let statement_owners = tally_statements_by_owner(file_cov);
let mut functions = Vec::with_capacity(file_cov.fn_map.len());
for (fn_id, fn_entry) in &file_cov.fn_map {
let coverage_pct = compute_function_statement_coverage(
file_cov,
fn_id,
statement_owners.get(fn_id.as_str()),
);
if let Some(function) =
istanbul_function_coverage(fn_entry, coverage_pct, source_index.as_ref())
{
functions.push(function);
}
}
files.insert(canonical, IstanbulFileCoverage::new(functions, relocated));
}
IstanbulCoverage { files, format }
}
#[expect(
clippy::filetype_is_file,
reason = "coverage provenance must admit regular files and reject every special file type"
)]
fn read_discovered_source(
path: &std::path::Path,
discovered_sources: Option<&rustc_hash::FxHashSet<std::path::PathBuf>>,
relocated: bool,
) -> Option<String> {
if relocated || !discovered_sources.is_some_and(|sources| sources.contains(path)) {
return None;
}
if std::fs::symlink_metadata(path).ok()?.file_type().is_file() {
std::fs::read_to_string(path).ok()
} else {
None
}
}
fn parse_coverage_map_tolerantly(
json: &str,
) -> Result<std::collections::BTreeMap<String, oxc_coverage_instrument::FileCoverage>, String> {
match oxc_coverage_instrument::parse_coverage_map(json) {
Ok(raw) => Ok(raw),
Err(strict_error) => {
let mut value: serde_json::Value =
serde_json::from_str(json).map_err(|_| strict_error.to_string())?;
let positions = clamp_negative_positions(&mut value);
let counts = clamp_negative_counts(&mut value);
if !positions && !counts {
return Err(strict_error.to_string());
}
serde_json::from_value(value).map_err(|_| strict_error.to_string())
}
}
}
fn clamp_negative_positions(value: &mut serde_json::Value) -> bool {
match value {
serde_json::Value::Object(entries) => {
let mut clamped = false;
for (key, child) in entries.iter_mut() {
if matches!(key.as_str(), "line" | "column")
&& child.as_i64().is_some_and(|number| number < 0)
{
*child = serde_json::Value::from(0);
clamped = true;
continue;
}
clamped |= clamp_negative_positions(child);
}
clamped
}
serde_json::Value::Array(items) => items.iter_mut().fold(false, |clamped, item| {
clamped | clamp_negative_positions(item)
}),
_ => false,
}
}
const COVERAGE_COUNT_KEYS: [&str; 4] = ["s", "f", "b", "b_T"];
fn clamp_negative_counts(value: &mut serde_json::Value) -> bool {
let serde_json::Value::Object(files) = value else {
return false;
};
let mut clamped = false;
for file in files.values_mut() {
let serde_json::Value::Object(sections) = file else {
continue;
};
for key in COVERAGE_COUNT_KEYS {
if let Some(counts) = sections.get_mut(key) {
clamped |= clamp_negative_numbers(counts);
}
}
}
clamped
}
fn clamp_negative_numbers(value: &mut serde_json::Value) -> bool {
match value {
serde_json::Value::Number(number) if number.as_f64().is_some_and(|n| n < 0.0) => {
*value = serde_json::Value::from(0);
true
}
serde_json::Value::Object(entries) => entries.values_mut().fold(false, |clamped, child| {
clamped | clamp_negative_numbers(child)
}),
serde_json::Value::Array(items) => items.iter_mut().fold(false, |clamped, item| {
clamped | clamp_negative_numbers(item)
}),
_ => false,
}
}
fn accessor_property_name(name: &str) -> Option<&str> {
let property = name
.strip_prefix("get ")
.or_else(|| name.strip_prefix("set "))?;
(!property.is_empty() && !property.contains(' ')).then_some(property)
}
fn rebase_coverage_path(
raw_path: std::path::PathBuf,
coverage_root: &std::path::Path,
project_root: &std::path::Path,
) -> std::path::PathBuf {
if let Ok(rel) = raw_path.strip_prefix(coverage_root) {
return project_root.join(rel);
}
if let Ok(canonical) = dunce::canonicalize(&raw_path)
&& let Ok(rel) = canonical.strip_prefix(coverage_root)
{
return project_root.join(rel);
}
raw_path
}
fn istanbul_function_coverage(
fn_entry: &oxc_coverage_instrument::FnEntry,
coverage_pct: f64,
source_index: Option<&IstanbulSourceIndex<'_>>,
) -> Option<IstanbulFunctionCoverage> {
let body_span = IstanbulSpan::from_entry(fn_entry, source_index);
let header_span = IstanbulSpan::header_from_entry(fn_entry, source_index);
let decl_start = normalized_istanbul_position(
fn_entry.decl.start.line,
fn_entry.decl.start.column,
source_index,
)?;
let effective_position = normalized_istanbul_position(
effective_istanbul_fn_line(fn_entry),
fn_entry.decl.start.column,
source_index,
);
let candidates = [
effective_position.map(|position| IstanbulAlias {
position,
primary: true,
}),
Some(IstanbulAlias {
position: decl_start,
primary: true,
}),
body_span.map(|span| IstanbulAlias {
position: span.start,
primary: false,
}),
named_function_syntax_alias(fn_entry, source_index),
];
let mut aliases: Vec<IstanbulAlias> = Vec::with_capacity(candidates.len());
for candidate in candidates.into_iter().flatten() {
if !aliases
.iter()
.any(|alias| alias.position == candidate.position)
{
aliases.push(candidate);
}
}
Some(IstanbulFunctionCoverage {
name: fn_entry.name.clone(),
coverage_pct,
aliases,
decl_start,
header_holds_other_fn: false,
header_span,
body_span,
})
}
struct IstanbulSourceIndex<'a> {
source: &'a str,
line_starts: Vec<usize>,
non_ascii_lines: rustc_hash::FxHashMap<usize, Utf16LineIndex>,
named_function_starts: rustc_hash::FxHashMap<usize, usize>,
}
struct Utf16LineIndex {
utf16_len: u32,
byte_len: usize,
checkpoints: Vec<Utf16Checkpoint>,
}
struct Utf16Checkpoint {
utf16_start: u32,
utf16_end: u32,
byte_end: usize,
}
impl<'a> IstanbulSourceIndex<'a> {
fn new(source: &'a str, path: &std::path::Path) -> Self {
let mut line_starts = vec![0];
line_starts.extend(
source
.bytes()
.enumerate()
.filter_map(|(index, byte)| (byte == b'\n').then_some(index + 1)),
);
let non_ascii_lines = utf16_line_indexes(source, &line_starts);
let named_function_starts = named_function_starts_from_clean_parse(source, path);
Self {
source,
line_starts,
non_ascii_lines,
named_function_starts,
}
}
fn byte_position(&self, line: u32, utf16_column: u32) -> Option<IstanbulPosition> {
let line_index = usize::try_from(line.checked_sub(1)?).ok()?;
let line_start = *self.line_starts.get(line_index)?;
let line_end = self
.line_starts
.get(line_index + 1)
.copied()
.map_or(self.source.len(), |next_start| next_start - 1);
let line_source = self.source.get(line_start..line_end)?;
let byte_column = if let Some(index) = self.non_ascii_lines.get(&line_index) {
index.byte_column(utf16_column)?
} else {
let byte_column = usize::try_from(utf16_column).ok()?;
(byte_column <= line_source.len()).then_some(byte_column)?
};
Some(IstanbulPosition::new(
line,
u32::try_from(byte_column).ok()?,
))
}
fn absolute_offset(&self, line: u32, utf16_column: u32) -> Option<usize> {
let position = self.byte_position(line, utf16_column)?;
let line_index = usize::try_from(position.line.checked_sub(1)?).ok()?;
self.line_starts
.get(line_index)?
.checked_add(position.col as usize)
}
fn position_at_offset(&self, offset: usize) -> Option<IstanbulPosition> {
if offset > self.source.len() {
return None;
}
let line_index = self.line_starts.partition_point(|start| *start <= offset) - 1;
Some(IstanbulPosition::new(
u32::try_from(line_index + 1).ok()?,
u32::try_from(offset.checked_sub(self.line_starts[line_index])?).ok()?,
))
}
fn named_function_start(
&self,
fn_entry: &oxc_coverage_instrument::FnEntry,
) -> Option<IstanbulPosition> {
let declaration_offset =
self.absolute_offset(fn_entry.decl.start.line, fn_entry.decl.start.column)?;
let syntax_offset = *self.named_function_starts.get(&declaration_offset)?;
self.position_at_offset(syntax_offset)
}
}
impl Utf16LineIndex {
fn byte_column(&self, utf16_column: u32) -> Option<usize> {
if utf16_column > self.utf16_len {
return None;
}
let completed = self
.checkpoints
.partition_point(|checkpoint| checkpoint.utf16_end <= utf16_column);
if let Some(next) = self.checkpoints.get(completed)
&& utf16_column > next.utf16_start
{
return None;
}
let (utf16_base, byte_base) = completed
.checked_sub(1)
.and_then(|index| self.checkpoints.get(index))
.map_or((0, 0), |checkpoint| {
(checkpoint.utf16_end, checkpoint.byte_end)
});
let ascii_width = usize::try_from(utf16_column.checked_sub(utf16_base)?).ok()?;
let byte_column = byte_base.checked_add(ascii_width)?;
(byte_column <= self.byte_len).then_some(byte_column)
}
}
fn utf16_line_indexes(
source: &str,
line_starts: &[usize],
) -> rustc_hash::FxHashMap<usize, Utf16LineIndex> {
let mut indexes = rustc_hash::FxHashMap::default();
for (line_index, &line_start) in line_starts.iter().enumerate() {
let line_end = line_starts
.get(line_index + 1)
.copied()
.map_or(source.len(), |next_start| next_start - 1);
let Some(line) = source.get(line_start..line_end) else {
continue;
};
if line.is_ascii() {
continue;
}
let mut utf16_column = 0_u32;
let mut checkpoints = Vec::new();
for (byte_column, character) in line.char_indices() {
let utf16_width = character.len_utf16() as u32;
if !character.is_ascii() {
checkpoints.push(Utf16Checkpoint {
utf16_start: utf16_column,
utf16_end: utf16_column.saturating_add(utf16_width),
byte_end: byte_column.saturating_add(character.len_utf8()),
});
}
utf16_column = utf16_column.saturating_add(utf16_width);
}
indexes.insert(
line_index,
Utf16LineIndex {
utf16_len: utf16_column,
byte_len: line.len(),
checkpoints,
},
);
}
indexes
}
fn named_function_starts_from_clean_parse(
source: &str,
path: &std::path::Path,
) -> rustc_hash::FxHashMap<usize, usize> {
let source_type = match path.extension().and_then(|extension| extension.to_str()) {
Some("gts") => oxc_span::SourceType::ts(),
Some("gjs") => oxc_span::SourceType::mjs(),
_ => oxc_span::SourceType::from_path(path).unwrap_or_default(),
};
if let Some(starts) = collect_named_function_starts(source, source_type) {
return starts;
}
if source_type.is_jsx() {
return rustc_hash::FxHashMap::default();
}
let jsx_source_type = if source_type.is_typescript() {
oxc_span::SourceType::tsx()
} else {
oxc_span::SourceType::jsx()
};
collect_named_function_starts(source, jsx_source_type).unwrap_or_default()
}
fn collect_named_function_starts(
source: &str,
source_type: oxc_span::SourceType,
) -> Option<rustc_hash::FxHashMap<usize, usize>> {
fallow_extract::parse_bounded_or_default(source, source_type, |parsed| {
if parsed.fatal_error || !parsed.diagnostics.is_empty() {
return None;
}
let mut starts = rustc_hash::FxHashMap::default();
let mut collector = NamedFunctionSyntaxCollector {
starts: &mut starts,
};
oxc_ast_visit::Visit::visit_program(&mut collector, &parsed.program);
Some(starts)
})
}
struct NamedFunctionSyntaxCollector<'a> {
starts: &'a mut rustc_hash::FxHashMap<usize, usize>,
}
impl<'ast> oxc_ast_visit::Visit<'ast> for NamedFunctionSyntaxCollector<'_> {
fn visit_function(
&mut self,
function: &oxc_ast::ast::Function<'ast>,
flags: oxc_syntax::scope::ScopeFlags,
) {
if let Some(identifier) = &function.id
&& let (Ok(identifier_start), Ok(syntax_start)) = (
usize::try_from(identifier.span.start),
usize::try_from(function.span.start),
)
{
self.starts.insert(identifier_start, syntax_start);
}
oxc_ast_visit::walk::walk_function(self, function, flags);
}
}
fn normalized_istanbul_position(
line: u32,
column: u32,
source_index: Option<&IstanbulSourceIndex<'_>>,
) -> Option<IstanbulPosition> {
match source_index {
Some(index) => index.byte_position(line, column),
None => Some(IstanbulPosition::new(line, column)),
}
}
fn named_function_syntax_alias(
fn_entry: &oxc_coverage_instrument::FnEntry,
source_index: Option<&IstanbulSourceIndex<'_>>,
) -> Option<IstanbulAlias> {
if is_anonymous_istanbul_name(&fn_entry.name) {
return None;
}
Some(IstanbulAlias {
position: source_index?.named_function_start(fn_entry)?,
primary: true,
})
}
fn effective_istanbul_fn_line(fn_entry: &oxc_coverage_instrument::FnEntry) -> u32 {
if fn_entry.line > 0 {
fn_entry.line
} else {
fn_entry.decl.start.line
}
}
#[derive(Default)]
struct StatementTally {
covered: u32,
total: u32,
}
fn istanbul_range_contains(
outer: &oxc_coverage_instrument::Location,
inner: &oxc_coverage_instrument::Location,
) -> bool {
let after_start = inner.start.line > outer.start.line
|| (inner.start.line == outer.start.line && inner.start.column >= outer.start.column);
let before_end = inner.end.line < outer.end.line
|| (inner.end.line == outer.end.line && inner.end.column <= outer.end.column);
after_start && before_end
}
fn tally_statements_by_owner(
file_cov: &oxc_coverage_instrument::FileCoverage,
) -> rustc_hash::FxHashMap<&str, StatementTally> {
let mut owners: rustc_hash::FxHashMap<&str, StatementTally> = rustc_hash::FxHashMap::default();
for (stmt_id, stmt_loc) in &file_cov.statement_map {
let mut owner: Option<(&str, &oxc_coverage_instrument::Location)> = None;
for (fn_id, fn_entry) in &file_cov.fn_map {
if !istanbul_range_contains(&fn_entry.loc, stmt_loc) {
continue;
}
let inner_than_current = owner.is_none_or(|(_, best)| {
let candidate = (
(fn_entry.loc.start.line, fn_entry.loc.start.column),
std::cmp::Reverse((fn_entry.loc.end.line, fn_entry.loc.end.column)),
);
let incumbent = (
(best.start.line, best.start.column),
std::cmp::Reverse((best.end.line, best.end.column)),
);
candidate > incumbent
});
if inner_than_current {
owner = Some((fn_id.as_str(), &fn_entry.loc));
}
}
let Some((fn_id, _)) = owner else {
continue;
};
let tally = owners.entry(fn_id).or_default();
tally.total += 1;
if file_cov.s.get(stmt_id).copied().unwrap_or(0) > 0 {
tally.covered += 1;
}
}
owners
}
fn compute_function_statement_coverage(
file_cov: &oxc_coverage_instrument::FileCoverage,
fn_id: &str,
owned: Option<&StatementTally>,
) -> f64 {
match owned {
Some(tally) if tally.total > 0 => f64::from(tally.covered) / f64::from(tally.total) * 100.0,
_ => {
let hit = file_cov.f.get(fn_id).copied().unwrap_or(0);
if hit > 0 { 100.0 } else { 0.0 }
}
}
}
fn count_unused_exports_by_path(
unused_exports: &[crate::results::UnusedExportFinding],
) -> rustc_hash::FxHashMap<&std::path::Path, usize> {
let mut map: rustc_hash::FxHashMap<&std::path::Path, usize> = rustc_hash::FxHashMap::default();
for exp in unused_exports {
*map.entry(exp.export.path.as_path()).or_default() += 1;
}
map
}
fn compute_maintainability_index(
complexity_density: f64,
dead_code_ratio: f64,
fan_out: usize,
lines: u32,
) -> f64 {
let dampening = (f64::from(lines) / fallow_output::MI_DENSITY_MIN_LINES).min(1.0);
let fan_out_penalty = ((fan_out as f64).ln_1p() * 4.0).min(15.0);
#[expect(
clippy::suboptimal_flops,
reason = "formula matches documented specification"
)]
let score = 100.0
- (complexity_density * 30.0 * dampening)
- (dead_code_ratio * 20.0)
- fan_out_penalty;
score.clamp(0.0, 100.0)
}
fn file_score_structural_concern(score: &FileHealthScore) -> f64 {
(100.0 - score.maintainability_index).clamp(0.0, 100.0)
}
#[must_use]
pub fn file_score_fully_crap_exempt(score: &FileHealthScore, max_crap_threshold: f64) -> bool {
let signal = file_score_crap_signal(score, max_crap_threshold);
max_crap_threshold <= 0.0
|| (score.crap_above_threshold == 0
&& if signal.suppression_aware {
signal.maximum >= CRAP_THRESHOLD
} else {
score.crap_exempted > 0
})
}
pub struct FileScoreCrapSignal {
pub maximum: f64,
pub threshold: f64,
suppression_aware: bool,
}
impl FileScoreCrapSignal {
pub const fn metric(&self) -> &'static str {
if self.suppression_aware {
"crap_unsuppressed_max"
} else {
"crap_max"
}
}
}
pub fn file_score_crap_signal(score: &FileHealthScore, global: f64) -> FileScoreCrapSignal {
match (
score.crap_unsuppressed_max,
score.crap_unsuppressed_effective_threshold,
) {
(Some(maximum), Some(threshold)) => FileScoreCrapSignal {
maximum,
threshold,
suppression_aware: true,
},
_ => FileScoreCrapSignal {
maximum: score.crap_max,
threshold: score.crap_effective_threshold.unwrap_or(global),
suppression_aware: false,
},
}
}
fn file_score_crap_concern(score: &FileHealthScore, max_crap_threshold: f64) -> f64 {
if file_score_fully_crap_exempt(score, max_crap_threshold) {
return 0.0;
}
let signal = file_score_crap_signal(score, max_crap_threshold);
let crap_max = signal.maximum;
let t = signal.threshold;
let half = t / 2.0;
let saturation = t * 10.0 / 3.0;
if crap_max <= 0.0 {
0.0
} else if crap_max < half {
(crap_max / half) * 45.0
} else if crap_max < t {
((crap_max - half) / half).mul_add(30.0, 45.0)
} else if crap_max < saturation {
((crap_max - t) / (saturation - t)).mul_add(25.0, 75.0)
} else {
100.0
}
}
fn file_score_triage_concern(score: &FileHealthScore, max_crap_threshold: f64) -> f64 {
file_score_structural_concern(score).max(file_score_crap_concern(score, max_crap_threshold))
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FileScoreConcern {
Structural,
Risk,
}
impl FileScoreConcern {
pub const fn label(self) -> &'static str {
match self {
Self::Structural => "structure",
Self::Risk => "risk",
}
}
}
pub fn file_score_concern_axis(
score: &FileHealthScore,
max_crap_threshold: f64,
) -> FileScoreConcern {
let crap_concern = file_score_crap_concern(score, max_crap_threshold);
if crap_concern <= 0.0 {
FileScoreConcern::Structural
} else if crap_concern >= file_score_structural_concern(score) {
FileScoreConcern::Risk
} else {
FileScoreConcern::Structural
}
}
fn compare_file_score_triage(
a: &FileHealthScore,
b: &FileHealthScore,
max_crap_threshold: f64,
) -> std::cmp::Ordering {
file_score_triage_concern(b, max_crap_threshold)
.total_cmp(&file_score_triage_concern(a, max_crap_threshold))
.then_with(|| {
file_score_crap_signal(b, max_crap_threshold)
.maximum
.total_cmp(&file_score_crap_signal(a, max_crap_threshold).maximum)
})
.then_with(|| a.maintainability_index.total_cmp(&b.maintainability_index))
.then_with(|| a.path.cmp(&b.path))
}
#[derive(Clone, Copy)]
pub(super) struct FileScoreComputeInput<'a> {
pub(super) modules: &'a [crate::source::ModuleInfo],
pub(super) file_paths:
&'a rustc_hash::FxHashMap<crate::discover::FileId, &'a std::path::PathBuf>,
pub(super) changed_files: Option<&'a rustc_hash::FxHashSet<std::path::PathBuf>>,
pub(super) istanbul_coverage: Option<&'a IstanbulCoverage>,
pub(super) root: &'a std::path::Path,
pub(super) crap_thresholds: CrapScoreThresholds<'a>,
pub(super) with_static_links: bool,
pub(super) test_reach: Option<&'a super::test_reach::TestReachGraph>,
}
pub(super) fn compute_file_scores(
input: FileScoreComputeInput<'_>,
analysis_output: crate::results::DeadCodeAnalysisArtifacts,
) -> Result<FileScoreOutput, String> {
let FileScoreComputeInput {
modules,
file_paths,
changed_files,
istanbul_coverage,
root,
crap_thresholds,
with_static_links,
test_reach,
} = input;
let retained_graph = analysis_output.graph.ok_or("graph not available")?;
let test_coverage = test_reach.map_or_else(
|| retained_graph.static_test_coverage(),
super::test_reach::TestReachGraph::static_test_coverage,
);
let graph = retained_graph.as_graph();
let results = &analysis_output.results;
let circular_files = collect_circular_files(results);
let top_complex_fns = collect_top_complex_fns(modules, file_paths);
let cycle_members = collect_cycle_members(results);
let direct_callers = collect_direct_callers(graph, file_paths);
let unused_export_names = collect_unused_export_names(results);
let unused_files: rustc_hash::FxHashSet<&std::path::Path> = results
.unused_files
.iter()
.map(|f| f.file.path.as_path())
.collect();
let unused_exports_by_path = count_unused_exports_by_path(&results.unused_exports);
let FileScoreCoverageSetup {
module_by_id,
coverage,
} = prepare_file_score_coverage_setup(modules, file_paths, results, graph, test_coverage, root);
let template_inherit =
build_template_inherit_contexts(graph, test_coverage, &module_by_id, file_paths);
let mut acc = accumulate_file_scores(
unused_export_names,
&FileScoreLoopCtx {
graph,
test_coverage,
file_paths,
module_by_id: &module_by_id,
unused_files: &unused_files,
unused_exports_by_path: &unused_exports_by_path,
template_inherit: &template_inherit,
istanbul_coverage,
root,
crap_thresholds,
},
);
acc.scores = finalize_file_score_list(
acc.scores,
changed_files,
crap_thresholds.resolver.global.crap,
);
Ok(build_file_score_output(FileScoreOutputParts {
graph,
with_static_links,
file_paths,
results,
scores: acc.scores,
coverage,
circular_files,
top_complex_fns,
crap_target_fns: acc.crap_target_fns,
entry_points: acc.entry_points,
value_export_counts: acc.value_export_counts,
unused_export_names: acc.unused_export_names,
cycle_members,
direct_callers,
istanbul_matched: acc.istanbul_matched,
istanbul_total: acc.istanbul_total,
istanbul_files_joined: acc.istanbul_files_joined,
istanbul_files_total: acc.istanbul_files_total,
coverage_input_format: istanbul_coverage.map(IstanbulCoverage::format),
per_function_crap: acc.per_function_crap,
template_inherit,
}))
}
struct FileScoreLoopCtx<'a> {
graph: &'a fallow_graph::graph::ModuleGraph,
test_coverage: StaticTestCoverage<'a>,
file_paths: &'a rustc_hash::FxHashMap<crate::discover::FileId, &'a std::path::PathBuf>,
module_by_id: &'a rustc_hash::FxHashMap<crate::discover::FileId, &'a crate::source::ModuleInfo>,
unused_files: &'a rustc_hash::FxHashSet<&'a std::path::Path>,
unused_exports_by_path: &'a rustc_hash::FxHashMap<&'a std::path::Path, usize>,
template_inherit: &'a rustc_hash::FxHashMap<crate::discover::FileId, TemplateInheritContext>,
istanbul_coverage: Option<&'a IstanbulCoverage>,
root: &'a std::path::Path,
crap_thresholds: CrapScoreThresholds<'a>,
}
struct FileScoreAccumulator {
scores: Vec<FileHealthScore>,
entry_points: rustc_hash::FxHashSet<std::path::PathBuf>,
value_export_counts: rustc_hash::FxHashMap<std::path::PathBuf, usize>,
unused_export_names: rustc_hash::FxHashMap<std::path::PathBuf, Vec<String>>,
crap_target_fns: rustc_hash::FxHashMap<std::path::PathBuf, Vec<(String, u32, u16)>>,
per_function_crap: rustc_hash::FxHashMap<std::path::PathBuf, Vec<PerFunctionCrap>>,
istanbul_matched: usize,
istanbul_files_joined: usize,
istanbul_files_total: usize,
istanbul_total: usize,
}
impl FileScoreAccumulator {
fn with_capacity(modules: usize) -> Self {
FileScoreAccumulator {
scores: Vec::with_capacity(modules),
entry_points: rustc_hash::FxHashSet::default(),
value_export_counts: rustc_hash::FxHashMap::default(),
unused_export_names: rustc_hash::FxHashMap::default(),
crap_target_fns: rustc_hash::FxHashMap::default(),
per_function_crap: rustc_hash::FxHashMap::default(),
istanbul_matched: 0,
istanbul_total: 0,
istanbul_files_joined: 0,
istanbul_files_total: 0,
}
}
}
fn accumulate_file_scores(
unused_export_names: rustc_hash::FxHashMap<std::path::PathBuf, Vec<String>>,
ctx: &FileScoreLoopCtx<'_>,
) -> FileScoreAccumulator {
let mut acc = FileScoreAccumulator {
unused_export_names,
istanbul_files_total: ctx
.istanbul_coverage
.map_or(0, IstanbulCoverage::file_count),
..FileScoreAccumulator::with_capacity(ctx.graph.modules.len())
};
for node in &ctx.graph.modules {
let Some(path) = ctx.file_paths.get(&node.file_id) else {
continue;
};
record_entry_point(&mut acc.entry_points, node, path);
let score = compute_one_file_score(&mut acc, ctx, node, path);
acc.scores.push(score);
}
acc
}
fn finalize_file_score_list(
mut scores: Vec<FileHealthScore>,
changed_files: Option<&rustc_hash::FxHashSet<std::path::PathBuf>>,
max_crap_threshold: f64,
) -> Vec<FileHealthScore> {
if let Some(changed) = changed_files {
scores.retain(|s| changed.contains(&s.path));
}
scores.retain(|s| s.function_count > 0);
scores.sort_by(|a, b| compare_file_score_triage(a, b, max_crap_threshold));
scores
}
fn compute_one_file_score(
acc: &mut FileScoreAccumulator,
ctx: &FileScoreLoopCtx<'_>,
node: &fallow_graph::graph::ModuleNode,
path: &std::path::Path,
) -> FileHealthScore {
let fan_in = ctx
.graph
.reverse_deps
.get(node.file_id.0 as usize)
.map_or(0, Vec::len);
let fan_out = node.edge_range.len();
let (total_cyclomatic, total_cognitive, function_count, lines) = ctx
.module_by_id
.get(&node.file_id)
.map_or((0, 0, 0, 0), |module| aggregate_complexity(module));
let value_exports = node.exports.iter().filter(|e| !e.is_type_only).count();
let path_owned = path.to_path_buf();
acc.value_export_counts
.insert(path_owned.clone(), value_exports);
record_unused_file_export_names(
path_owned.as_path(),
&node.exports,
ctx.unused_files,
&mut acc.unused_export_names,
);
let (dead_code_ratio_rounded, complexity_density_rounded, maintainability_index_rounded) =
compute_file_score_metrics(node, &path_owned, ctx, total_cyclomatic, lines, fan_out);
let relative = path_owned.strip_prefix(ctx.root).unwrap_or(&path_owned);
let suppressions = ctx
.module_by_id
.get(&node.file_id)
.map_or(&[][..], |module| module.suppressions.as_slice());
let ceilings = CrapCeilingLookup::new(ctx.crap_thresholds, relative, suppressions);
let crap = compute_file_score_crap(node, ctx, &path_owned, &ceilings);
acc.istanbul_matched += crap.istanbul_matched;
acc.istanbul_total += crap.istanbul_total;
acc.istanbul_files_joined += usize::from(crap.coverage_file_joined);
record_per_function_crap(&mut acc.per_function_crap, &path_owned, crap.per_function);
let mut eligible_functions = crap.signals.eligible_functions;
if !eligible_functions.is_empty() {
eligible_functions.sort_by_key(|function| std::cmp::Reverse(function.2));
eligible_functions.truncate(MAX_FUNCTION_EVIDENCE);
acc.crap_target_fns
.insert(path_owned.clone(), eligible_functions);
}
let global_crap = ctx.crap_thresholds.resolver.global.crap;
let crap_effective_threshold = crap
.signals
.min_ceiling
.filter(|ceiling| (*ceiling - global_crap).abs() > f64::EPSILON);
FileHealthScore {
path: path_owned,
fan_in,
fan_out,
dead_code_ratio: dead_code_ratio_rounded,
complexity_density: complexity_density_rounded,
maintainability_index: maintainability_index_rounded,
total_cyclomatic,
total_cognitive,
function_count,
lines,
crap_max: crap.max,
crap_above_threshold: crap.signals.above,
crap_exempted: crap.signals.exempted,
crap_suppressed: crap.signals.suppressed,
crap_unsuppressed_max: (crap.signals.suppressed > 0)
.then_some(crap.signals.unsuppressed_max),
crap_unsuppressed_effective_threshold: (crap.signals.suppressed > 0)
.then_some(crap.signals.unsuppressed_min_ceiling.unwrap_or(global_crap)),
crap_effective_threshold,
}
}
fn compute_file_score_metrics(
node: &fallow_graph::graph::ModuleNode,
path: &std::path::Path,
ctx: &FileScoreLoopCtx<'_>,
total_cyclomatic: u32,
lines: u32,
fan_out: usize,
) -> (f64, f64, f64) {
let dead_code_ratio = compute_dead_code_ratio(
path,
&node.exports,
ctx.unused_files,
ctx.unused_exports_by_path,
);
let complexity_density = compute_complexity_density(total_cyclomatic, lines);
let dead_code_ratio_rounded = (dead_code_ratio * 100.0).round() / 100.0;
let complexity_density_rounded = (complexity_density * 100.0).round() / 100.0;
let maintainability_index = compute_maintainability_index(
complexity_density_rounded,
dead_code_ratio_rounded,
fan_out,
lines,
);
(
dead_code_ratio_rounded,
complexity_density_rounded,
(maintainability_index * 10.0).round() / 10.0,
)
}
fn build_file_score_output(parts: FileScoreOutputParts<'_>) -> FileScoreOutput {
let total_exports: usize = parts.graph.modules.iter().map(|m| m.exports.len()).sum();
let unused_deps = parts.results.unused_dependencies.len()
+ parts.results.unused_dev_dependencies.len()
+ parts.results.unused_optional_dependencies.len();
let analysis_snapshot =
build_analysis_counts_snapshot(parts.graph, parts.file_paths, parts.results, unused_deps);
let analysis_counts =
build_file_score_analysis_counts(parts.results, total_exports, unused_deps);
let template_inherit_provenance =
build_template_inherit_provenance(parts.template_inherit, parts.file_paths);
FileScoreOutput {
scores: parts.scores,
coverage: parts.coverage,
circular_files: parts.circular_files,
top_complex_fns: parts.top_complex_fns,
crap_target_fns: parts.crap_target_fns,
entry_points: parts.entry_points,
value_export_counts: parts.value_export_counts,
unused_export_names: parts.unused_export_names,
cycle_members: parts.cycle_members,
direct_callers: parts.direct_callers,
analysis_counts,
prop_drilling_chains: parts.results.prop_drilling_chains.clone(),
render_fan_in: parts.results.render_fan_in.clone(),
analysis_snapshot,
istanbul_matched: parts.istanbul_matched,
istanbul_total: parts.istanbul_total,
istanbul_files_joined: parts.istanbul_files_joined,
istanbul_files_total: parts.istanbul_files_total,
coverage_input_format: parts.coverage_input_format,
per_function_crap: parts.per_function_crap,
template_inherit_provenance,
static_links: parts
.with_static_links
.then(|| crate::change_coupling::StaticLinkIndex::from_graph(parts.graph)),
}
}
fn build_file_score_analysis_counts(
results: &crate::results::AnalysisResults,
total_exports: usize,
unused_deps: usize,
) -> crate::vital_signs::AnalysisCounts {
crate::vital_signs::AnalysisCounts {
total_exports,
dead_files: results.unused_files.len(),
dead_exports: results.unused_exports.len() + results.unused_types.len(),
unused_deps,
circular_deps: results.circular_dependencies.len(),
total_deps: 0usize,
}
}
fn build_template_inherit_provenance(
template_inherit: rustc_hash::FxHashMap<crate::discover::FileId, TemplateInheritContext>,
file_paths: &rustc_hash::FxHashMap<crate::discover::FileId, &std::path::PathBuf>,
) -> rustc_hash::FxHashMap<std::path::PathBuf, std::path::PathBuf> {
template_inherit
.into_iter()
.filter_map(|(file_id, ctx)| {
file_paths
.get(&file_id)
.map(|path| ((**path).clone(), ctx.provenance_owner))
})
.collect()
}
fn record_entry_point(
entry_points: &mut rustc_hash::FxHashSet<std::path::PathBuf>,
node: &fallow_graph::graph::ModuleNode,
path: &std::path::Path,
) {
if node.is_entry_point() {
entry_points.insert(path.to_path_buf());
}
}
fn record_unused_file_export_names(
path: &std::path::Path,
exports: &[fallow_graph::graph::ExportSymbol],
unused_files: &rustc_hash::FxHashSet<&std::path::Path>,
unused_export_names: &mut rustc_hash::FxHashMap<std::path::PathBuf, Vec<String>>,
) {
if !unused_files.contains(path) || unused_export_names.contains_key(path) {
return;
}
let names: Vec<String> = exports
.iter()
.filter(|export| !export.is_type_only)
.map(|export| export.name.to_string())
.collect();
if !names.is_empty() {
unused_export_names.insert(path.to_path_buf(), names);
}
}
struct FileScoreCrap {
max: f64,
signals: CrapThresholdSignals,
per_function: Vec<PerFunctionCrap>,
istanbul_matched: usize,
istanbul_total: usize,
coverage_file_joined: bool,
}
impl FileScoreCrap {
fn empty() -> Self {
Self {
max: 0.0,
signals: CrapThresholdSignals::default(),
per_function: Vec::new(),
istanbul_matched: 0,
istanbul_total: 0,
coverage_file_joined: false,
}
}
fn estimated(result: EstimatedCrapResult) -> Self {
Self {
max: result.max_crap,
signals: result.signals,
per_function: result.per_function,
istanbul_matched: 0,
istanbul_total: 0,
coverage_file_joined: false,
}
}
fn istanbul(result: IstanbulCrapResult, coverage_file_joined: bool) -> Self {
Self {
max: result.max_crap,
signals: result.signals,
per_function: result.per_function,
istanbul_matched: result.matched,
istanbul_total: result.total,
coverage_file_joined,
}
}
}
fn compute_file_score_crap(
node: &fallow_graph::graph::ModuleNode,
ctx: &FileScoreLoopCtx<'_>,
path: &std::path::Path,
ceilings: &CrapCeilingLookup<'_>,
) -> FileScoreCrap {
let Some(module) = ctx.module_by_id.get(&node.file_id).copied() else {
return FileScoreCrap::empty();
};
let is_coverage_suppressed = crate::suppress::is_file_suppressed(
&module.suppressions,
fallow_types::suppress::IssueKind::CoverageGaps,
);
let is_test_reachable = ctx.test_coverage.covers_file(node.file_id) || is_coverage_suppressed;
let resolution = resolve_crap_coverage(
ctx.template_inherit.get(&node.file_id),
ctx.istanbul_coverage,
path,
);
match resolution {
CrapCoverageResolution::TemplateInherited(inherit_ctx) => {
compute_template_inherited_crap(module, inherit_ctx, ceilings)
}
CrapCoverageResolution::Istanbul { file_coverage } => {
compute_istanbul_file_crap(module, file_coverage, is_test_reachable, ceilings)
}
CrapCoverageResolution::StaticEstimated => {
compute_static_file_crap(module, node, ctx.test_coverage, is_test_reachable, ceilings)
}
}
}
fn compute_template_inherited_crap(
module: &crate::source::ModuleInfo,
inherit_ctx: &TemplateInheritContext,
ceilings: &CrapCeilingLookup<'_>,
) -> FileScoreCrap {
FileScoreCrap::estimated(compute_crap_scores_estimated(
&module.complexity,
&inherit_ctx.test_referenced_exports,
inherit_ctx.is_test_reachable,
fallow_output::CoverageSource::EstimatedComponentInherited,
ceilings,
))
}
fn compute_istanbul_file_crap(
module: &crate::source::ModuleInfo,
file_coverage: Option<&IstanbulFileCoverage>,
is_test_reachable: bool,
ceilings: &CrapCeilingLookup<'_>,
) -> FileScoreCrap {
FileScoreCrap::istanbul(
compute_crap_scores_istanbul(
&module.complexity,
file_coverage,
is_test_reachable,
ceilings,
),
file_coverage.is_some(),
)
}
fn compute_static_file_crap(
module: &crate::source::ModuleInfo,
node: &fallow_graph::graph::ModuleNode,
test_coverage: StaticTestCoverage<'_>,
is_test_reachable: bool,
ceilings: &CrapCeilingLookup<'_>,
) -> FileScoreCrap {
let test_refs = build_test_referenced_exports(node.file_id, &node.exports, test_coverage);
FileScoreCrap::estimated(compute_crap_scores_estimated(
&module.complexity,
&test_refs,
is_test_reachable,
fallow_output::CoverageSource::Estimated,
ceilings,
))
}
fn record_per_function_crap(
per_function_crap: &mut rustc_hash::FxHashMap<std::path::PathBuf, Vec<PerFunctionCrap>>,
path: &std::path::Path,
per_function: Vec<PerFunctionCrap>,
) {
if !per_function.is_empty() {
per_function_crap.insert(path.to_path_buf(), per_function);
}
}
struct FileScoreCoverageSetup<'a> {
module_by_id: rustc_hash::FxHashMap<crate::discover::FileId, &'a crate::source::ModuleInfo>,
coverage: CoverageGapData,
}
fn prepare_file_score_coverage_setup<'a>(
modules: &'a [crate::source::ModuleInfo],
file_paths: &rustc_hash::FxHashMap<crate::discover::FileId, &std::path::PathBuf>,
results: &crate::results::AnalysisResults,
graph: &fallow_graph::graph::ModuleGraph,
test_coverage: StaticTestCoverage<'_>,
root: &std::path::Path,
) -> FileScoreCoverageSetup<'a> {
let module_by_id: rustc_hash::FxHashMap<_, _> =
modules.iter().map(|m| (m.file_id, m)).collect();
let unused_exports: rustc_hash::FxHashSet<(&std::path::Path, String)> = results
.unused_exports
.iter()
.map(|export| {
(
export.export.path.as_path(),
export.export.export_name.clone(),
)
})
.collect();
let coverage = compute_coverage_gaps(
graph,
test_coverage,
file_paths,
&module_by_id,
&unused_exports,
root,
);
FileScoreCoverageSetup {
module_by_id,
coverage,
}
}
fn collect_circular_files(
results: &crate::results::AnalysisResults,
) -> rustc_hash::FxHashSet<std::path::PathBuf> {
results
.circular_dependencies
.iter()
.flat_map(|c| c.cycle.files.iter().cloned())
.collect()
}
fn collect_top_complex_fns(
modules: &[crate::source::ModuleInfo],
file_paths: &rustc_hash::FxHashMap<crate::discover::FileId, &std::path::PathBuf>,
) -> rustc_hash::FxHashMap<std::path::PathBuf, Vec<(String, u32, u16)>> {
let mut top_complex_fns = rustc_hash::FxHashMap::default();
for module in modules {
if module.complexity.is_empty() {
continue;
}
let Some(path) = file_paths.get(&module.file_id) else {
continue;
};
let mut funcs: Vec<(String, u32, u16)> = module
.complexity
.iter()
.filter(|f| !fallow_types::extract::is_synthetic_module_unit(&f.name))
.map(|f| (f.name.clone(), f.line, f.cognitive))
.collect();
funcs.sort_by_key(|f| std::cmp::Reverse(f.2));
funcs.truncate(MAX_FUNCTION_EVIDENCE);
if funcs.first().is_some_and(|worst| worst.2 > 0) {
top_complex_fns.insert((*path).clone(), funcs);
}
}
top_complex_fns
}
fn collect_cycle_members(
results: &crate::results::AnalysisResults,
) -> rustc_hash::FxHashMap<std::path::PathBuf, Vec<std::path::PathBuf>> {
let mut cycle_members: rustc_hash::FxHashMap<std::path::PathBuf, Vec<std::path::PathBuf>> =
rustc_hash::FxHashMap::default();
for cycle in &results.circular_dependencies {
for file in &cycle.cycle.files {
let others: Vec<std::path::PathBuf> = cycle
.cycle
.files
.iter()
.filter(|f| *f != file)
.cloned()
.collect();
cycle_members
.entry(file.clone())
.or_default()
.extend(others);
}
}
for members in cycle_members.values_mut() {
members.sort();
members.dedup();
}
cycle_members
}
fn collect_unused_export_names(
results: &crate::results::AnalysisResults,
) -> rustc_hash::FxHashMap<std::path::PathBuf, Vec<String>> {
let mut unused_export_names: rustc_hash::FxHashMap<std::path::PathBuf, Vec<String>> =
rustc_hash::FxHashMap::default();
for exp in &results.unused_exports {
unused_export_names
.entry(exp.export.path.clone())
.or_default()
.push(exp.export.export_name.clone());
}
unused_export_names
}
fn build_analysis_counts_snapshot(
graph: &fallow_graph::graph::ModuleGraph,
file_paths: &rustc_hash::FxHashMap<crate::discover::FileId, &std::path::PathBuf>,
results: &crate::results::AnalysisResults,
unused_deps: usize,
) -> AnalysisCountsSnapshot {
let mut module_export_counts = rustc_hash::FxHashMap::with_capacity_and_hasher(
graph.modules.len(),
rustc_hash::FxBuildHasher,
);
for module in &graph.modules {
if let Some(path) = file_paths.get(&module.file_id) {
module_export_counts.insert((*path).clone(), module.exports.len());
}
}
let mut unused_export_paths =
Vec::with_capacity(results.unused_exports.len() + results.unused_types.len());
unused_export_paths.extend(results.unused_exports.iter().map(|e| e.export.path.clone()));
unused_export_paths.extend(results.unused_types.iter().map(|e| e.export.path.clone()));
let mut unused_dep_package_paths = Vec::with_capacity(unused_deps);
unused_dep_package_paths.extend(
results
.unused_dependencies
.iter()
.map(|d| d.dep.path.clone()),
);
unused_dep_package_paths.extend(
results
.unused_dev_dependencies
.iter()
.map(|d| d.dep.path.clone()),
);
unused_dep_package_paths.extend(
results
.unused_optional_dependencies
.iter()
.map(|d| d.dep.path.clone()),
);
AnalysisCountsSnapshot {
unused_file_paths: results
.unused_files
.iter()
.map(|f| f.file.path.clone())
.collect(),
unused_export_paths,
unused_dep_package_paths,
circular_dep_groups: results
.circular_dependencies
.iter()
.map(|c| c.cycle.files.clone())
.collect(),
module_export_counts,
}
}
#[cfg(test)]
#[path = "scoring_private_member_tests.rs"]
mod private_member_tests;
#[cfg(test)]
#[path = "scoring_tests.rs"]
mod tests;