use std::collections::VecDeque;
use std::path::{Path, PathBuf};
use fallow_types::extract::{ImportedName, ModuleInfo};
use fallow_types::semantic::SemanticNamespace;
use fallow_types::trace_chain::{
ChainHop, StarExportAmbiguity, SymbolChainQuery, SymbolChainTrace, UnresolvedCallee,
UnresolvedReason,
};
use rustc_hash::{FxHashMap, FxHashSet};
use crate::discover::FileId;
use crate::graph::{ExportNamespace, ImportedSymbol, ModuleGraph};
use crate::trace::trace_impl::find_module;
#[must_use]
pub fn trace_symbol_chain(
graph: &ModuleGraph,
modules: &[ModuleInfo],
root: &Path,
query: SymbolChainQuery<'_>,
) -> Option<SymbolChainTrace> {
let SymbolChainQuery {
file,
symbol,
depth,
directions,
} = query;
let module = find_module(graph, root, file)?;
let rel_file = relativize(&module.path, root);
let symbol_found = module.exports.iter().any(|e| e.name.to_string() == *symbol);
let star_export_ambiguity = (!symbol_found)
.then(|| star_export_ambiguity(graph, root, module.file_id, symbol))
.flatten();
let module_by_id: FxHashMap<FileId, &ModuleInfo> =
modules.iter().map(|m| (m.file_id, m)).collect();
let callers = directions.callers.then(|| {
if symbol_found {
collect_callers(graph, root, module.file_id, symbol, depth)
} else {
Vec::new()
}
});
let callees_walk = directions.callees.then(|| {
if symbol_found {
collect_callees(graph, &module_by_id, root, module.file_id, depth)
} else {
(Vec::new(), Vec::new())
}
});
let (callees, unresolved_callees) = match callees_walk {
Some((resolved, unresolved)) => (Some(resolved), Some(unresolved)),
None => (None, None),
};
let reason = build_reason(
symbol_found,
star_export_ambiguity.as_ref(),
callers.as_deref(),
callees.as_deref(),
unresolved_callees.as_deref(),
);
Some(SymbolChainTrace {
file: rel_file,
symbol: symbol.to_string(),
symbol_found,
depth,
best_effort: true,
callers,
callees,
unresolved_callees,
star_export_ambiguity,
reason,
})
}
fn star_export_ambiguity(
graph: &ModuleGraph,
root: &Path,
module_id: FileId,
symbol: &str,
) -> Option<StarExportAmbiguity> {
let collisions: Vec<_> = graph
.ambiguous_star_exports_on(module_id)
.into_iter()
.filter(|collision| collision.name.as_ref() == symbol)
.collect();
if collisions.is_empty() {
return None;
}
let mut sources: Vec<PathBuf> = collisions
.iter()
.flat_map(|collision| collision.contributors.iter())
.filter_map(|contributor| graph.modules.get(contributor.0 as usize))
.map(|module| relativize(&module.path, root))
.collect();
sources.sort();
sources.dedup();
let mut namespaces: Vec<SemanticNamespace> = collisions
.iter()
.map(|collision| match collision.namespace {
ExportNamespace::Type => SemanticNamespace::Type,
ExportNamespace::Value => SemanticNamespace::Value,
})
.collect();
namespaces.sort_unstable_by_key(|namespace| namespace_order(*namespace));
namespaces.dedup();
Some(StarExportAmbiguity {
sources,
namespaces,
})
}
const fn namespace_order(namespace: SemanticNamespace) -> u8 {
match namespace {
SemanticNamespace::Type => 0,
SemanticNamespace::Value => 1,
}
}
fn collect_callers(
graph: &ModuleGraph,
root: &Path,
target: FileId,
symbol: &str,
depth: u32,
) -> Vec<ChainHop> {
let mut hops = Vec::new();
let mut visited: FxHashSet<(FileId, String)> = FxHashSet::default();
let mut queue: VecDeque<(FileId, String, u32)> = VecDeque::new();
queue.push_back((target, symbol.to_owned(), 1));
while let Some((target, symbol, current_depth)) = queue.pop_front() {
if current_depth > depth {
continue;
}
for (importer, sym) in importing_symbols(graph, target, &symbol) {
if !visited.insert((importer, sym.local_name.clone())) {
continue;
}
hops.push(ChainHop {
file: module_path(graph, importer, root),
imported_as: imported_name_label(&sym.imported_name),
local_name: sym.local_name.clone(),
type_only: sym.is_type_only,
depth: current_depth,
});
queue.push_back((importer, sym.local_name.clone(), current_depth + 1));
}
}
sort_hops(&mut hops);
hops
}
fn collect_callees(
graph: &ModuleGraph,
module_by_id: &FxHashMap<FileId, &ModuleInfo>,
root: &Path,
module_id: FileId,
depth: u32,
) -> (Vec<ChainHop>, Vec<UnresolvedCallee>) {
let mut resolved = Vec::new();
let mut visited: FxHashSet<FileId> = FxHashSet::default();
visited.insert(module_id);
let mut queue: VecDeque<(FileId, u32)> = VecDeque::new();
queue.push_back((module_id, 1));
while let Some((module_id, current_depth)) = queue.pop_front() {
if current_depth > depth {
continue;
}
for (edge_target, symbols) in graph.outgoing_symbol_edges(module_id) {
let target_path = module_path(graph, edge_target, root);
for sym in symbols {
if matches!(sym.imported_name, ImportedName::SideEffect) {
continue;
}
resolved.push(ChainHop {
file: target_path.clone(),
imported_as: imported_name_label(&sym.imported_name),
local_name: sym.local_name.clone(),
type_only: sym.is_type_only,
depth: current_depth,
});
}
if visited.insert(edge_target) {
queue.push_back((edge_target, current_depth + 1));
}
}
}
sort_hops(&mut resolved);
let unresolved = module_by_id
.get(&module_id)
.map(|info| collect_unresolved_callees(info))
.unwrap_or_default();
(resolved, unresolved)
}
fn importing_symbols<'g>(
graph: &'g ModuleGraph,
target: FileId,
symbol: &'g str,
) -> impl Iterator<Item = (FileId, &'g ImportedSymbol)> + 'g {
graph
.importers_of(target)
.iter()
.flat_map(move |&importer| {
graph
.outgoing_symbol_edges(importer)
.filter(move |(edge_target, _)| *edge_target == target)
.flat_map(|(_, symbols)| symbols.iter())
.filter(move |sym| imported_name_matches(&sym.imported_name, symbol))
.map(move |sym| (importer, sym))
})
}
fn module_path(graph: &ModuleGraph, file_id: FileId, root: &Path) -> PathBuf {
graph
.modules
.get(file_id.0 as usize)
.map_or_else(|| PathBuf::from("<unknown>"), |m| relativize(&m.path, root))
}
fn sort_hops(hops: &mut [ChainHop]) {
hops.sort_by(|a, b| {
a.depth
.cmp(&b.depth)
.then_with(|| a.file.cmp(&b.file))
.then_with(|| a.local_name.cmp(&b.local_name))
});
}
fn collect_unresolved_callees(info: &ModuleInfo) -> Vec<UnresolvedCallee> {
let import_locals: FxHashSet<&str> =
info.imports.iter().map(|i| i.local_name.as_str()).collect();
let mut out = Vec::new();
let mut seen: FxHashSet<&str> = FxHashSet::default();
for callee in info.callee_uses.iter().filter(|callee| !callee.is_new) {
let path = callee.callee_path.as_str();
let leading = path.split('.').next().unwrap_or(path);
if import_locals.contains(leading) {
continue;
}
if !seen.insert(path) {
continue;
}
let reason = if path.contains('.') {
UnresolvedReason::MemberOrDynamic
} else {
UnresolvedReason::LocalOrGlobal
};
out.push(UnresolvedCallee {
callee: path.to_string(),
reason,
});
}
out.sort_by(|a, b| a.callee.cmp(&b.callee));
out
}
fn build_reason(
symbol_found: bool,
ambiguity: Option<&StarExportAmbiguity>,
callers: Option<&[ChainHop]>,
callees: Option<&[ChainHop]>,
unresolved: Option<&[UnresolvedCallee]>,
) -> String {
if let Some(ambiguity) = ambiguity {
let sources = ambiguity
.sources
.iter()
.map(|source| source.to_string_lossy().replace('\\', "/"))
.collect::<Vec<_>>()
.join(", ");
return format!(
"two or more `export *` sources of this file declare this name, so it resolves as ambiguous and the file exports nothing under it (ECMA-262 ResolveExport); colliding origins: {sources}"
);
}
if !symbol_found {
return "symbol not found as an export of this file, so the chains are empty".to_string();
}
let mut parts = Vec::new();
if let Some(callers) = callers {
parts.push(format!("{} caller hop(s)", callers.len()));
}
if let Some(callees) = callees {
parts.push(format!("{} resolved callee hop(s)", callees.len()));
}
if let Some(unresolved) = unresolved {
parts.push(format!(
"{} unresolved callee(s) reported",
unresolved.len()
));
}
format!("best-effort syntactic chain: {}", parts.join(", "))
}
fn imported_name_matches(name: &ImportedName, symbol: &str) -> bool {
match name {
ImportedName::Named(n) => n == symbol,
ImportedName::Default => symbol == "default",
ImportedName::Namespace => true,
ImportedName::SideEffect => false,
}
}
fn imported_name_label(name: &ImportedName) -> String {
match name {
ImportedName::Named(name) => name.clone(),
ImportedName::Default => "default".to_string(),
ImportedName::Namespace => "*".to_string(),
ImportedName::SideEffect => "side-effect".to_string(),
}
}
fn relativize(path: &Path, root: &Path) -> PathBuf {
path.strip_prefix(root).unwrap_or(path).to_path_buf()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn imported_name_matches_named_default_namespace() {
assert!(imported_name_matches(
&ImportedName::Named("foo".to_string()),
"foo"
));
assert!(!imported_name_matches(
&ImportedName::Named("bar".to_string()),
"foo"
));
assert!(imported_name_matches(&ImportedName::Default, "default"));
assert!(!imported_name_matches(&ImportedName::Default, "foo"));
assert!(imported_name_matches(&ImportedName::Namespace, "anything"));
assert!(!imported_name_matches(&ImportedName::SideEffect, "foo"));
}
#[test]
fn unresolved_reason_classifies_member_vs_bare() {
let info = ModuleInfo {
callee_uses: vec![
fallow_types::extract::CalleeUse {
callee_path: "localHelper".to_string(),
span_starts: vec![0],
is_new: false,
},
fallow_types::extract::CalleeUse {
callee_path: "obj.method".to_string(),
span_starts: vec![10],
is_new: false,
},
],
..ModuleInfo::empty(FileId(1))
};
let unresolved = collect_unresolved_callees(&info);
assert_eq!(unresolved.len(), 2);
assert_eq!(unresolved[0].callee, "localHelper");
assert_eq!(unresolved[0].reason, UnresolvedReason::LocalOrGlobal);
assert_eq!(unresolved[1].callee, "obj.method");
assert_eq!(unresolved[1].reason, UnresolvedReason::MemberOrDynamic);
}
#[test]
fn new_expressions_are_not_listed_as_unresolved() {
let info = ModuleInfo {
callee_uses: vec![
fallow_types::extract::CalleeUse {
callee_path: "Foo".to_string(),
span_starts: vec![0],
is_new: true,
},
fallow_types::extract::CalleeUse {
callee_path: "localHelper".to_string(),
span_starts: vec![10],
is_new: false,
},
],
..ModuleInfo::empty(FileId(1))
};
let unresolved = collect_unresolved_callees(&info);
assert_eq!(unresolved.len(), 1);
assert_eq!(unresolved[0].callee, "localHelper");
}
#[test]
fn imported_callees_are_not_listed_as_unresolved() {
let info = ModuleInfo {
imports: vec![fallow_types::extract::ImportInfo {
source: "./dep".to_string(),
imported_name: ImportedName::Named("dep".to_string()),
local_name: "dep".to_string(),
is_type_only: false,
is_type_only_star: false,
from_style: false,
span: oxc_span::Span::default(),
source_span: oxc_span::Span::default(),
}],
callee_uses: vec![
fallow_types::extract::CalleeUse {
callee_path: "dep".to_string(),
span_starts: vec![0],
is_new: false,
},
fallow_types::extract::CalleeUse {
callee_path: "ghost".to_string(),
span_starts: vec![5],
is_new: false,
},
],
..ModuleInfo::empty(FileId(1))
};
let unresolved = collect_unresolved_callees(&info);
assert_eq!(unresolved.len(), 1);
assert_eq!(unresolved[0].callee, "ghost");
}
}