use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use serde::Serialize;
use tree_sitter::{Parser, Tree};
use crate::calls::extract_calls_full;
use crate::edit::line_col_to_byte;
use crate::error::AftError;
use crate::imports::{self, ImportBlock};
use crate::language::LanguageProvider;
use crate::parser::{detect_language, grammar_for, LangId};
use crate::symbols::SymbolKind;
type SharedPath = Arc<PathBuf>;
type SharedStr = Arc<str>;
type ReverseIndex = HashMap<PathBuf, HashMap<String, Vec<IndexedCallerSite>>>;
#[derive(Debug, Clone)]
pub struct CallSite {
pub callee_name: String,
pub full_callee: String,
pub line: u32,
pub byte_start: usize,
pub byte_end: usize,
}
#[derive(Debug, Clone, Serialize)]
pub struct SymbolMeta {
pub kind: SymbolKind,
pub exported: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub signature: Option<String>,
}
#[derive(Debug, Clone)]
pub struct FileCallData {
pub calls_by_symbol: HashMap<String, Vec<CallSite>>,
pub exported_symbols: Vec<String>,
pub symbol_metadata: HashMap<String, SymbolMeta>,
pub import_block: ImportBlock,
pub lang: LangId,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum EdgeResolution {
Resolved { file: PathBuf, symbol: String },
Unresolved { callee_name: String },
}
#[derive(Debug, Clone, Serialize)]
pub struct CallerSite {
pub caller_file: PathBuf,
pub caller_symbol: String,
pub line: u32,
pub col: u32,
pub resolved: bool,
}
#[derive(Debug, Clone)]
struct IndexedCallerSite {
caller_file: SharedPath,
caller_symbol: SharedStr,
line: u32,
col: u32,
resolved: bool,
}
#[derive(Debug, Clone, Serialize)]
pub struct CallerGroup {
pub file: String,
pub callers: Vec<CallerEntry>,
}
#[derive(Debug, Clone, Serialize)]
pub struct CallerEntry {
pub symbol: String,
pub line: u32,
}
#[derive(Debug, Clone, Serialize)]
pub struct CallersResult {
pub symbol: String,
pub file: String,
pub callers: Vec<CallerGroup>,
pub total_callers: usize,
pub scanned_files: usize,
}
#[derive(Debug, Clone, Serialize)]
pub struct CallTreeNode {
pub name: String,
pub file: String,
pub line: u32,
#[serde(skip_serializing_if = "Option::is_none")]
pub signature: Option<String>,
pub resolved: bool,
pub children: Vec<CallTreeNode>,
}
const MAIN_INIT_NAMES: &[&str] = &["main", "init", "setup", "bootstrap", "run"];
pub fn is_entry_point(name: &str, kind: &SymbolKind, exported: bool, lang: LangId) -> bool {
if exported && *kind == SymbolKind::Function {
return true;
}
let lower = name.to_lowercase();
if MAIN_INIT_NAMES.contains(&lower.as_str()) {
return true;
}
match lang {
LangId::TypeScript | LangId::JavaScript | LangId::Tsx => {
matches!(lower.as_str(), "describe" | "it" | "test")
|| lower.starts_with("test")
|| lower.starts_with("spec")
}
LangId::Python => {
lower.starts_with("test_") || matches!(name, "setUp" | "tearDown")
}
LangId::Rust => {
lower.starts_with("test_")
}
LangId::Go => {
name.starts_with("Test")
}
LangId::C
| LangId::Cpp
| LangId::Zig
| LangId::CSharp
| LangId::Html
| LangId::Markdown => false,
}
}
#[derive(Debug, Clone, Serialize)]
pub struct TraceHop {
pub symbol: String,
pub file: String,
pub line: u32,
#[serde(skip_serializing_if = "Option::is_none")]
pub signature: Option<String>,
pub is_entry_point: bool,
}
#[derive(Debug, Clone, Serialize)]
pub struct TracePath {
pub hops: Vec<TraceHop>,
}
#[derive(Debug, Clone, Serialize)]
pub struct TraceToResult {
pub target_symbol: String,
pub target_file: String,
pub paths: Vec<TracePath>,
pub total_paths: usize,
pub entry_points_found: usize,
pub max_depth_reached: bool,
pub truncated_paths: usize,
}
#[derive(Debug, Clone, Serialize)]
pub struct ImpactCaller {
pub caller_symbol: String,
pub caller_file: String,
pub line: u32,
#[serde(skip_serializing_if = "Option::is_none")]
pub signature: Option<String>,
pub is_entry_point: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub call_expression: Option<String>,
pub parameters: Vec<String>,
}
#[derive(Debug, Clone, Serialize)]
pub struct ImpactResult {
pub symbol: String,
pub file: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub signature: Option<String>,
pub parameters: Vec<String>,
pub total_affected: usize,
pub affected_files: usize,
pub callers: Vec<ImpactCaller>,
}
#[derive(Debug, Clone, Serialize)]
pub struct DataFlowHop {
pub file: String,
pub symbol: String,
pub variable: String,
pub line: u32,
pub flow_type: String,
pub approximate: bool,
}
#[derive(Debug, Clone, Serialize)]
pub struct TraceDataResult {
pub expression: String,
pub origin_file: String,
pub origin_symbol: String,
pub hops: Vec<DataFlowHop>,
pub depth_limited: bool,
}
pub fn extract_parameters(signature: &str, lang: LangId) -> Vec<String> {
let start = match signature.find('(') {
Some(i) => i + 1,
None => return Vec::new(),
};
let end = match signature[start..].find(')') {
Some(i) => start + i,
None => return Vec::new(),
};
let params_str = &signature[start..end].trim();
if params_str.is_empty() {
return Vec::new();
}
let parts = split_params(params_str);
let mut result = Vec::new();
for part in parts {
let trimmed = part.trim();
if trimmed.is_empty() {
continue;
}
match lang {
LangId::Rust => {
let normalized = trimmed.replace(' ', "");
if normalized == "self"
|| normalized == "&self"
|| normalized == "&mutself"
|| normalized == "mutself"
{
continue;
}
}
LangId::Python => {
if trimmed == "self" || trimmed.starts_with("self:") {
continue;
}
}
_ => {}
}
let name = extract_param_name(trimmed, lang);
if !name.is_empty() {
result.push(name);
}
}
result
}
fn split_params(s: &str) -> Vec<String> {
let mut parts = Vec::new();
let mut current = String::new();
let mut depth = 0i32;
for ch in s.chars() {
match ch {
'<' | '[' | '{' | '(' => {
depth += 1;
current.push(ch);
}
'>' | ']' | '}' | ')' => {
depth -= 1;
current.push(ch);
}
',' if depth == 0 => {
parts.push(current.clone());
current.clear();
}
_ => {
current.push(ch);
}
}
}
if !current.is_empty() {
parts.push(current);
}
parts
}
fn extract_param_name(param: &str, lang: LangId) -> String {
let trimmed = param.trim();
let working = if trimmed.starts_with("...") {
&trimmed[3..]
} else if trimmed.starts_with("**") {
&trimmed[2..]
} else if trimmed.starts_with('*') && lang == LangId::Python {
&trimmed[1..]
} else {
trimmed
};
let working = if lang == LangId::Rust && working.starts_with("mut ") {
&working[4..]
} else {
working
};
let name = working
.split(|c: char| c == ':' || c == '=')
.next()
.unwrap_or("")
.trim();
let name = name.trim_end_matches('?');
if lang == LangId::Go && !name.contains(' ') {
return name.to_string();
}
if lang == LangId::Go {
return name.split_whitespace().next().unwrap_or("").to_string();
}
name.to_string()
}
pub struct CallGraph {
data: HashMap<PathBuf, FileCallData>,
project_root: PathBuf,
project_files: Option<Vec<PathBuf>>,
reverse_index: Option<ReverseIndex>,
}
impl CallGraph {
pub fn new(project_root: PathBuf) -> Self {
Self {
data: HashMap::new(),
project_root,
project_files: None,
reverse_index: None,
}
}
pub fn project_root(&self) -> &Path {
&self.project_root
}
fn resolve_cross_file_edge_with_exports<F>(
full_callee: &str,
short_name: &str,
caller_file: &Path,
import_block: &ImportBlock,
mut file_exports_symbol: F,
) -> EdgeResolution
where
F: FnMut(&Path, &str) -> bool,
{
let caller_dir = caller_file.parent().unwrap_or(Path::new("."));
if full_callee.contains('.') {
let parts: Vec<&str> = full_callee.splitn(2, '.').collect();
if parts.len() == 2 {
let namespace = parts[0];
let member = parts[1];
for imp in &import_block.imports {
if imp.namespace_import.as_deref() == Some(namespace) {
if let Some(resolved_path) =
resolve_module_path(caller_dir, &imp.module_path)
{
return EdgeResolution::Resolved {
file: resolved_path,
symbol: member.to_owned(),
};
}
}
}
}
}
for imp in &import_block.imports {
if imp.names.iter().any(|name| name == short_name) {
if let Some(resolved_path) = resolve_module_path(caller_dir, &imp.module_path) {
return EdgeResolution::Resolved {
file: resolved_path,
symbol: short_name.to_owned(),
};
}
}
if imp.default_import.as_deref() == Some(short_name) {
if let Some(resolved_path) = resolve_module_path(caller_dir, &imp.module_path) {
return EdgeResolution::Resolved {
file: resolved_path,
symbol: "default".to_owned(),
};
}
}
}
if let Some((original_name, resolved_path)) =
resolve_aliased_import(short_name, import_block, caller_dir)
{
return EdgeResolution::Resolved {
file: resolved_path,
symbol: original_name,
};
}
for imp in &import_block.imports {
if let Some(resolved_path) = resolve_module_path(caller_dir, &imp.module_path) {
if resolved_path.is_dir() {
if let Some(index_path) = find_index_file(&resolved_path) {
if file_exports_symbol(&index_path, short_name) {
return EdgeResolution::Resolved {
file: index_path,
symbol: short_name.to_owned(),
};
}
}
} else if file_exports_symbol(&resolved_path, short_name) {
return EdgeResolution::Resolved {
file: resolved_path,
symbol: short_name.to_owned(),
};
}
}
}
EdgeResolution::Unresolved {
callee_name: short_name.to_owned(),
}
}
pub fn build_file(&mut self, path: &Path) -> Result<&FileCallData, AftError> {
let canon = self.canonicalize(path)?;
if !self.data.contains_key(&canon) {
let file_data = build_file_data(&canon)?;
self.data.insert(canon.clone(), file_data);
}
Ok(&self.data[&canon])
}
pub fn resolve_cross_file_edge(
&mut self,
full_callee: &str,
short_name: &str,
caller_file: &Path,
import_block: &ImportBlock,
) -> EdgeResolution {
Self::resolve_cross_file_edge_with_exports(
full_callee,
short_name,
caller_file,
import_block,
|path, symbol_name| self.file_exports_symbol(path, symbol_name),
)
}
fn file_exports_symbol(&mut self, path: &Path, symbol_name: &str) -> bool {
match self.build_file(path) {
Ok(data) => data.exported_symbols.iter().any(|name| name == symbol_name),
Err(_) => false,
}
}
fn file_exports_symbol_cached(&self, path: &Path, symbol_name: &str) -> bool {
self.lookup_file_data(path)
.map(|data| data.exported_symbols.iter().any(|name| name == symbol_name))
.unwrap_or(false)
}
pub fn forward_tree(
&mut self,
file: &Path,
symbol: &str,
max_depth: usize,
) -> Result<CallTreeNode, AftError> {
let mut visited = HashSet::new();
self.forward_tree_inner(file, symbol, max_depth, 0, &mut visited)
}
fn forward_tree_inner(
&mut self,
file: &Path,
symbol: &str,
max_depth: usize,
current_depth: usize,
visited: &mut HashSet<(PathBuf, String)>,
) -> Result<CallTreeNode, AftError> {
let canon = self.canonicalize(file)?;
let visit_key = (canon.clone(), symbol.to_string());
if visited.contains(&visit_key) {
let (line, signature) = get_symbol_meta(&canon, symbol);
return Ok(CallTreeNode {
name: symbol.to_string(),
file: self.relative_path(&canon),
line,
signature,
resolved: true,
children: vec![], });
}
visited.insert(visit_key.clone());
let file_data = build_file_data(&canon)?;
let import_block = file_data.import_block.clone();
let _lang = file_data.lang;
let call_sites = file_data
.calls_by_symbol
.get(symbol)
.cloned()
.unwrap_or_default();
let (sym_line, sym_signature) = get_symbol_meta(&canon, symbol);
self.data.insert(canon.clone(), file_data);
let mut children = Vec::new();
if current_depth < max_depth {
for call_site in &call_sites {
let edge = self.resolve_cross_file_edge(
&call_site.full_callee,
&call_site.callee_name,
&canon,
&import_block,
);
match edge {
EdgeResolution::Resolved {
file: ref target_file,
ref symbol,
} => {
match self.forward_tree_inner(
target_file,
symbol,
max_depth,
current_depth + 1,
visited,
) {
Ok(child) => children.push(child),
Err(_) => {
children.push(CallTreeNode {
name: call_site.callee_name.clone(),
file: self.relative_path(target_file),
line: call_site.line,
signature: None,
resolved: false,
children: vec![],
});
}
}
}
EdgeResolution::Unresolved { callee_name } => {
children.push(CallTreeNode {
name: callee_name,
file: self.relative_path(&canon),
line: call_site.line,
signature: None,
resolved: false,
children: vec![],
});
}
}
}
}
visited.remove(&visit_key);
Ok(CallTreeNode {
name: symbol.to_string(),
file: self.relative_path(&canon),
line: sym_line,
signature: sym_signature,
resolved: true,
children,
})
}
pub fn project_files(&mut self) -> &[PathBuf] {
if self.project_files.is_none() {
let project_root = self.project_root.clone();
self.project_files = Some(walk_project_files(&project_root).collect());
}
self.project_files.as_deref().unwrap_or(&[])
}
fn build_reverse_index(&mut self) {
let all_files = self.project_files().to_vec();
for f in &all_files {
let _ = self.build_file(f);
}
let mut reverse: ReverseIndex = HashMap::new();
for caller_file in &all_files {
let canon_caller = Arc::new(
std::fs::canonicalize(caller_file).unwrap_or_else(|_| caller_file.clone()),
);
let file_data = match self
.data
.get(caller_file)
.or_else(|| self.data.get(canon_caller.as_ref()))
{
Some(d) => d,
None => continue,
};
for (symbol_name, call_sites) in &file_data.calls_by_symbol {
let caller_symbol: SharedStr = Arc::from(symbol_name.as_str());
for call_site in call_sites {
let edge = Self::resolve_cross_file_edge_with_exports(
&call_site.full_callee,
&call_site.callee_name,
canon_caller.as_ref(),
&file_data.import_block,
|path, symbol_name| self.file_exports_symbol_cached(path, symbol_name),
);
let (target_file, target_symbol, resolved) = match edge {
EdgeResolution::Resolved { file, symbol } => (file, symbol, true),
EdgeResolution::Unresolved { callee_name } => {
(canon_caller.as_ref().clone(), callee_name, false)
}
};
reverse
.entry(target_file)
.or_default()
.entry(target_symbol)
.or_default()
.push(IndexedCallerSite {
caller_file: Arc::clone(&canon_caller),
caller_symbol: Arc::clone(&caller_symbol),
line: call_site.line,
col: 0,
resolved,
});
}
}
}
self.reverse_index = Some(reverse);
}
fn reverse_sites(&self, file: &Path, symbol: &str) -> Option<&[IndexedCallerSite]> {
self.reverse_index
.as_ref()?
.get(file)?
.get(symbol)
.map(Vec::as_slice)
}
pub fn callers_of(
&mut self,
file: &Path,
symbol: &str,
depth: usize,
) -> Result<CallersResult, AftError> {
let canon = self.canonicalize(file)?;
self.build_file(&canon)?;
if self.reverse_index.is_none() {
self.build_reverse_index();
}
let scanned_files = self.project_files.as_ref().map(|f| f.len()).unwrap_or(0);
let effective_depth = if depth == 0 { 1 } else { depth };
let mut visited = HashSet::new();
let mut all_sites: Vec<CallerSite> = Vec::new();
self.collect_callers_recursive(
&canon,
symbol,
effective_depth,
0,
&mut visited,
&mut all_sites,
);
let mut groups_map: HashMap<PathBuf, Vec<CallerEntry>> = HashMap::new();
let total_callers = all_sites.len();
for site in all_sites {
let caller_file: PathBuf = site.caller_file;
let caller_symbol: String = site.caller_symbol;
let line = site.line;
let entry = CallerEntry {
symbol: caller_symbol,
line,
};
if let Some(entries) = groups_map.get_mut(&caller_file) {
entries.push(entry);
} else {
groups_map.insert(caller_file, vec![entry]);
}
}
let mut callers: Vec<CallerGroup> = groups_map
.into_iter()
.map(|(file_path, entries)| CallerGroup {
file: self.relative_path(&file_path),
callers: entries,
})
.collect();
callers.sort_by(|a, b| a.file.cmp(&b.file));
Ok(CallersResult {
symbol: symbol.to_string(),
file: self.relative_path(&canon),
callers,
total_callers,
scanned_files,
})
}
pub fn trace_to(
&mut self,
file: &Path,
symbol: &str,
max_depth: usize,
) -> Result<TraceToResult, AftError> {
let canon = self.canonicalize(file)?;
self.build_file(&canon)?;
if self.reverse_index.is_none() {
self.build_reverse_index();
}
let target_rel = self.relative_path(&canon);
let effective_max = if max_depth == 0 { 10 } else { max_depth };
if self.reverse_index.is_none() {
return Err(AftError::ParseError {
message: format!(
"reverse index unavailable after building callers for {}",
canon.display()
),
});
}
let (target_line, target_sig) = get_symbol_meta(&canon, symbol);
let target_is_entry = self
.lookup_file_data(&canon)
.and_then(|fd| {
let meta = fd.symbol_metadata.get(symbol)?;
Some(is_entry_point(symbol, &meta.kind, meta.exported, fd.lang))
})
.unwrap_or(false);
type PathElem = (SharedPath, SharedStr, u32, Option<String>);
let mut complete_paths: Vec<Vec<PathElem>> = Vec::new();
let mut max_depth_reached = false;
let mut truncated_paths: usize = 0;
let initial: Vec<PathElem> = vec![(
Arc::new(canon.clone()),
Arc::from(symbol),
target_line,
target_sig,
)];
if target_is_entry {
complete_paths.push(initial.clone());
}
let mut queue: Vec<(Vec<PathElem>, usize)> = vec![(initial, 0)];
while let Some((path, depth)) = queue.pop() {
if depth >= effective_max {
max_depth_reached = true;
continue;
}
let Some((current_file, current_symbol, _, _)) = path.last() else {
continue;
};
let callers = match self.reverse_sites(current_file.as_ref(), current_symbol.as_ref()) {
Some(sites) => sites,
None => {
if path.len() > 1 {
truncated_paths += 1;
}
continue;
}
};
let mut has_new_path = false;
for site in callers {
if path.iter().any(|(file_path, sym, _, _)| {
file_path.as_ref() == site.caller_file.as_ref()
&& sym.as_ref() == site.caller_symbol.as_ref()
}) {
continue;
}
has_new_path = true;
let (caller_line, caller_sig) =
get_symbol_meta(site.caller_file.as_ref(), site.caller_symbol.as_ref());
let mut new_path = path.clone();
new_path.push((
Arc::clone(&site.caller_file),
Arc::clone(&site.caller_symbol),
caller_line,
caller_sig,
));
let caller_is_entry = self
.lookup_file_data(site.caller_file.as_ref())
.and_then(|fd| {
let meta = fd.symbol_metadata.get(site.caller_symbol.as_ref())?;
Some(is_entry_point(
site.caller_symbol.as_ref(),
&meta.kind,
meta.exported,
fd.lang,
))
})
.unwrap_or(false);
if caller_is_entry {
complete_paths.push(new_path.clone());
}
queue.push((new_path, depth + 1));
}
if !has_new_path && path.len() > 1 {
truncated_paths += 1;
}
}
let mut paths: Vec<TracePath> = complete_paths
.into_iter()
.map(|mut elems| {
elems.reverse();
let hops: Vec<TraceHop> = elems
.iter()
.enumerate()
.map(|(i, (file_path, sym, line, sig))| {
let is_ep = if i == 0 {
self.lookup_file_data(file_path.as_ref())
.and_then(|fd| {
let meta = fd.symbol_metadata.get(sym.as_ref())?;
Some(is_entry_point(
sym.as_ref(),
&meta.kind,
meta.exported,
fd.lang,
))
})
.unwrap_or(false)
} else {
false
};
TraceHop {
symbol: sym.to_string(),
file: self.relative_path(file_path.as_ref()),
line: *line,
signature: sig.clone(),
is_entry_point: is_ep,
}
})
.collect();
TracePath { hops }
})
.collect();
paths.sort_by(|a, b| {
let a_entry = a.hops.first().map(|h| h.symbol.as_str()).unwrap_or("");
let b_entry = b.hops.first().map(|h| h.symbol.as_str()).unwrap_or("");
a_entry.cmp(b_entry).then(a.hops.len().cmp(&b.hops.len()))
});
let mut entry_point_names: HashSet<String> = HashSet::new();
for p in &paths {
if let Some(first) = p.hops.first() {
if first.is_entry_point {
entry_point_names.insert(first.symbol.clone());
}
}
}
let total_paths = paths.len();
let entry_points_found = entry_point_names.len();
Ok(TraceToResult {
target_symbol: symbol.to_string(),
target_file: target_rel,
paths,
total_paths,
entry_points_found,
max_depth_reached,
truncated_paths,
})
}
pub fn impact(
&mut self,
file: &Path,
symbol: &str,
depth: usize,
) -> Result<ImpactResult, AftError> {
let canon = self.canonicalize(file)?;
self.build_file(&canon)?;
if self.reverse_index.is_none() {
self.build_reverse_index();
}
let effective_depth = if depth == 0 { 1 } else { depth };
let (target_signature, target_parameters, target_lang) = {
let file_data = match self.data.get(&canon) {
Some(d) => d,
None => {
return Err(AftError::InvalidRequest {
message: "file data missing after build".to_string(),
})
}
};
let meta = file_data.symbol_metadata.get(symbol);
let sig = meta.and_then(|m| m.signature.clone());
let lang = file_data.lang;
let params = sig
.as_deref()
.map(|s| extract_parameters(s, lang))
.unwrap_or_default();
(sig, params, lang)
};
let mut visited = HashSet::new();
let mut all_sites: Vec<CallerSite> = Vec::new();
self.collect_callers_recursive(
&canon,
symbol,
effective_depth,
0,
&mut visited,
&mut all_sites,
);
let mut seen: HashSet<(PathBuf, String, u32)> = HashSet::new();
all_sites.retain(|site| {
seen.insert((
site.caller_file.clone(),
site.caller_symbol.clone(),
site.line,
))
});
let mut callers = Vec::new();
let mut affected_file_set = HashSet::new();
for site in &all_sites {
if let Err(e) = self.build_file(site.caller_file.as_path()) {
log::debug!(
"callgraph: skipping caller file {}: {}",
site.caller_file.display(),
e
);
}
let (sig, is_ep, params, _lang) = {
if let Some(fd) = self.lookup_file_data(site.caller_file.as_path()) {
let meta = fd.symbol_metadata.get(&site.caller_symbol);
let sig = meta.and_then(|m| m.signature.clone());
let kind = meta.map(|m| m.kind.clone()).unwrap_or(SymbolKind::Function);
let exported = meta.map(|m| m.exported).unwrap_or(false);
let is_ep = is_entry_point(&site.caller_symbol, &kind, exported, fd.lang);
let lang = fd.lang;
let params = sig
.as_deref()
.map(|s| extract_parameters(s, lang))
.unwrap_or_default();
(sig, is_ep, params, lang)
} else {
(None, false, Vec::new(), target_lang)
}
};
let call_expression = self.read_source_line(site.caller_file.as_path(), site.line);
let rel_file = self.relative_path(site.caller_file.as_path());
affected_file_set.insert(rel_file.clone());
callers.push(ImpactCaller {
caller_symbol: site.caller_symbol.clone(),
caller_file: rel_file,
line: site.line,
signature: sig,
is_entry_point: is_ep,
call_expression,
parameters: params,
});
}
callers.sort_by(|a, b| a.caller_file.cmp(&b.caller_file).then(a.line.cmp(&b.line)));
let total_affected = callers.len();
let affected_files = affected_file_set.len();
Ok(ImpactResult {
symbol: symbol.to_string(),
file: self.relative_path(&canon),
signature: target_signature,
parameters: target_parameters,
total_affected,
affected_files,
callers,
})
}
pub fn trace_data(
&mut self,
file: &Path,
symbol: &str,
expression: &str,
max_depth: usize,
) -> Result<TraceDataResult, AftError> {
let canon = self.canonicalize(file)?;
let rel_file = self.relative_path(&canon);
self.build_file(&canon)?;
{
let fd = match self.data.get(&canon) {
Some(d) => d,
None => {
return Err(AftError::InvalidRequest {
message: "file data missing after build".to_string(),
})
}
};
let has_symbol = fd.calls_by_symbol.contains_key(symbol)
|| fd.exported_symbols.iter().any(|name| name == symbol)
|| fd.symbol_metadata.contains_key(symbol);
if !has_symbol {
return Err(AftError::InvalidRequest {
message: format!(
"trace_data: symbol '{}' not found in {}",
symbol,
file.display()
),
});
}
}
let mut hops = Vec::new();
let mut depth_limited = false;
self.trace_data_inner(
&canon,
symbol,
expression,
max_depth,
0,
&mut hops,
&mut depth_limited,
&mut HashSet::new(),
);
Ok(TraceDataResult {
expression: expression.to_string(),
origin_file: rel_file,
origin_symbol: symbol.to_string(),
hops,
depth_limited,
})
}
fn trace_data_inner(
&mut self,
file: &Path,
symbol: &str,
tracking_name: &str,
max_depth: usize,
current_depth: usize,
hops: &mut Vec<DataFlowHop>,
depth_limited: &mut bool,
visited: &mut HashSet<(PathBuf, String, String)>,
) {
let visit_key = (
file.to_path_buf(),
symbol.to_string(),
tracking_name.to_string(),
);
if visited.contains(&visit_key) {
return; }
visited.insert(visit_key);
let source = match std::fs::read_to_string(file) {
Ok(s) => s,
Err(_) => return,
};
let lang = match detect_language(file) {
Some(l) => l,
None => return,
};
let grammar = grammar_for(lang);
let mut parser = Parser::new();
if parser.set_language(&grammar).is_err() {
return;
}
let tree = match parser.parse(&source, None) {
Some(t) => t,
None => return,
};
let symbols = list_symbols_from_tree(&source, &tree, lang, file);
let sym_info = match symbols.iter().find(|s| s.name == symbol) {
Some(s) => s,
None => return,
};
let body_start = line_col_to_byte(&source, sym_info.start_line, sym_info.start_col);
let body_end = line_col_to_byte(&source, sym_info.end_line, sym_info.end_col);
let root = tree.root_node();
let body_node = match find_node_covering_range(root, body_start, body_end) {
Some(n) => n,
None => return,
};
let mut tracked_names: Vec<String> = vec![tracking_name.to_string()];
let rel_file = self.relative_path(file);
self.walk_for_data_flow(
body_node,
&source,
&mut tracked_names,
file,
symbol,
&rel_file,
lang,
max_depth,
current_depth,
hops,
depth_limited,
visited,
);
}
#[allow(clippy::too_many_arguments)]
fn walk_for_data_flow(
&mut self,
node: tree_sitter::Node,
source: &str,
tracked_names: &mut Vec<String>,
file: &Path,
symbol: &str,
rel_file: &str,
lang: LangId,
max_depth: usize,
current_depth: usize,
hops: &mut Vec<DataFlowHop>,
depth_limited: &mut bool,
visited: &mut HashSet<(PathBuf, String, String)>,
) {
let kind = node.kind();
let is_var_decl = matches!(
kind,
"variable_declarator"
| "assignment_expression"
| "augmented_assignment_expression"
| "assignment"
| "let_declaration"
| "short_var_declaration"
);
if is_var_decl {
if let Some((new_name, init_text, line, is_approx)) =
self.extract_assignment_info(node, source, lang, tracked_names)
{
if !is_approx {
hops.push(DataFlowHop {
file: rel_file.to_string(),
symbol: symbol.to_string(),
variable: new_name.clone(),
line,
flow_type: "assignment".to_string(),
approximate: false,
});
tracked_names.push(new_name);
} else {
hops.push(DataFlowHop {
file: rel_file.to_string(),
symbol: symbol.to_string(),
variable: init_text,
line,
flow_type: "assignment".to_string(),
approximate: true,
});
return;
}
}
}
if kind == "call_expression" || kind == "call" || kind == "macro_invocation" {
self.check_call_for_data_flow(
node,
source,
tracked_names,
file,
symbol,
rel_file,
lang,
max_depth,
current_depth,
hops,
depth_limited,
visited,
);
}
let mut cursor = node.walk();
if cursor.goto_first_child() {
loop {
let child = cursor.node();
self.walk_for_data_flow(
child,
source,
tracked_names,
file,
symbol,
rel_file,
lang,
max_depth,
current_depth,
hops,
depth_limited,
visited,
);
if !cursor.goto_next_sibling() {
break;
}
}
}
}
fn extract_assignment_info(
&self,
node: tree_sitter::Node,
source: &str,
_lang: LangId,
tracked_names: &[String],
) -> Option<(String, String, u32, bool)> {
let kind = node.kind();
let line = node.start_position().row as u32 + 1;
match kind {
"variable_declarator" => {
let name_node = node.child_by_field_name("name")?;
let value_node = node.child_by_field_name("value")?;
let name_text = node_text(name_node, source);
let value_text = node_text(value_node, source);
if name_node.kind() == "object_pattern" || name_node.kind() == "array_pattern" {
if tracked_names.iter().any(|t| value_text.contains(t)) {
return Some((name_text.clone(), name_text, line, true));
}
return None;
}
if tracked_names.iter().any(|t| {
value_text == *t
|| value_text.starts_with(&format!("{}.", t))
|| value_text.starts_with(&format!("{}[", t))
}) {
return Some((name_text, value_text, line, false));
}
None
}
"assignment_expression" | "augmented_assignment_expression" => {
let left = node.child_by_field_name("left")?;
let right = node.child_by_field_name("right")?;
let left_text = node_text(left, source);
let right_text = node_text(right, source);
if tracked_names.iter().any(|t| right_text == *t) {
return Some((left_text, right_text, line, false));
}
None
}
"assignment" => {
let left = node.child_by_field_name("left")?;
let right = node.child_by_field_name("right")?;
let left_text = node_text(left, source);
let right_text = node_text(right, source);
if tracked_names.iter().any(|t| right_text == *t) {
return Some((left_text, right_text, line, false));
}
None
}
"let_declaration" | "short_var_declaration" => {
let left = node
.child_by_field_name("pattern")
.or_else(|| node.child_by_field_name("left"))?;
let right = node
.child_by_field_name("value")
.or_else(|| node.child_by_field_name("right"))?;
let left_text = node_text(left, source);
let right_text = node_text(right, source);
if tracked_names.iter().any(|t| right_text == *t) {
return Some((left_text, right_text, line, false));
}
None
}
_ => None,
}
}
#[allow(clippy::too_many_arguments)]
fn check_call_for_data_flow(
&mut self,
node: tree_sitter::Node,
source: &str,
tracked_names: &[String],
file: &Path,
_symbol: &str,
rel_file: &str,
_lang: LangId,
max_depth: usize,
current_depth: usize,
hops: &mut Vec<DataFlowHop>,
depth_limited: &mut bool,
visited: &mut HashSet<(PathBuf, String, String)>,
) {
let args_node = find_child_by_kind(node, "arguments")
.or_else(|| find_child_by_kind(node, "argument_list"));
let args_node = match args_node {
Some(n) => n,
None => return,
};
let mut arg_positions: Vec<(usize, String)> = Vec::new(); let mut arg_idx = 0;
let mut cursor = args_node.walk();
if cursor.goto_first_child() {
loop {
let child = cursor.node();
let child_kind = child.kind();
if child_kind == "(" || child_kind == ")" || child_kind == "," {
if !cursor.goto_next_sibling() {
break;
}
continue;
}
let arg_text = node_text(child, source);
if child_kind == "spread_element" || child_kind == "dictionary_splat" {
if tracked_names.iter().any(|t| arg_text.contains(t)) {
hops.push(DataFlowHop {
file: rel_file.to_string(),
symbol: _symbol.to_string(),
variable: arg_text,
line: child.start_position().row as u32 + 1,
flow_type: "parameter".to_string(),
approximate: true,
});
}
if !cursor.goto_next_sibling() {
break;
}
arg_idx += 1;
continue;
}
if tracked_names.iter().any(|t| arg_text == *t) {
arg_positions.push((arg_idx, arg_text));
}
arg_idx += 1;
if !cursor.goto_next_sibling() {
break;
}
}
}
if arg_positions.is_empty() {
return;
}
let (full_callee, short_callee) = extract_callee_names(node, source);
let full_callee = match full_callee {
Some(f) => f,
None => return,
};
let short_callee = match short_callee {
Some(s) => s,
None => return,
};
let import_block = {
match self.data.get(file) {
Some(fd) => fd.import_block.clone(),
None => return,
}
};
let edge = self.resolve_cross_file_edge(&full_callee, &short_callee, file, &import_block);
match edge {
EdgeResolution::Resolved {
file: target_file,
symbol: target_symbol,
} => {
if current_depth + 1 > max_depth {
*depth_limited = true;
return;
}
if let Err(e) = self.build_file(&target_file) {
log::debug!(
"callgraph: skipping target file {}: {}",
target_file.display(),
e
);
}
let (params, _target_lang) = {
match self.data.get(&target_file) {
Some(fd) => {
let meta = fd.symbol_metadata.get(&target_symbol);
let sig = meta.and_then(|m| m.signature.clone());
let params = sig
.as_deref()
.map(|s| extract_parameters(s, fd.lang))
.unwrap_or_default();
(params, fd.lang)
}
None => return,
}
};
let target_rel = self.relative_path(&target_file);
for (pos, _tracked) in &arg_positions {
if let Some(param_name) = params.get(*pos) {
hops.push(DataFlowHop {
file: target_rel.clone(),
symbol: target_symbol.clone(),
variable: param_name.clone(),
line: get_symbol_meta(&target_file, &target_symbol).0,
flow_type: "parameter".to_string(),
approximate: false,
});
self.trace_data_inner(
&target_file.clone(),
&target_symbol.clone(),
param_name,
max_depth,
current_depth + 1,
hops,
depth_limited,
visited,
);
}
}
}
EdgeResolution::Unresolved { callee_name } => {
let has_local = self
.data
.get(file)
.map(|fd| {
fd.calls_by_symbol.contains_key(&callee_name)
|| fd.symbol_metadata.contains_key(&callee_name)
})
.unwrap_or(false);
if has_local {
let (params, _target_lang) = {
let Some(fd) = self.data.get(file) else {
return;
};
let meta = fd.symbol_metadata.get(&callee_name);
let sig = meta.and_then(|m| m.signature.clone());
let params = sig
.as_deref()
.map(|s| extract_parameters(s, fd.lang))
.unwrap_or_default();
(params, fd.lang)
};
let file_rel = self.relative_path(file);
for (pos, _tracked) in &arg_positions {
if let Some(param_name) = params.get(*pos) {
hops.push(DataFlowHop {
file: file_rel.clone(),
symbol: callee_name.clone(),
variable: param_name.clone(),
line: get_symbol_meta(file, &callee_name).0,
flow_type: "parameter".to_string(),
approximate: false,
});
self.trace_data_inner(
file,
&callee_name.clone(),
param_name,
max_depth,
current_depth + 1,
hops,
depth_limited,
visited,
);
}
}
} else {
for (_pos, tracked) in &arg_positions {
hops.push(DataFlowHop {
file: self.relative_path(file),
symbol: callee_name.clone(),
variable: tracked.clone(),
line: node.start_position().row as u32 + 1,
flow_type: "parameter".to_string(),
approximate: true,
});
}
}
}
}
}
fn read_source_line(&self, path: &Path, line: u32) -> Option<String> {
let content = std::fs::read_to_string(path).ok()?;
content
.lines()
.nth(line.saturating_sub(1) as usize)
.map(|l| l.trim().to_string())
}
fn collect_callers_recursive(
&self,
file: &Path,
symbol: &str,
max_depth: usize,
current_depth: usize,
visited: &mut HashSet<(PathBuf, SharedStr)>,
result: &mut Vec<CallerSite>,
) {
if current_depth >= max_depth {
return;
}
let canon = std::fs::canonicalize(file).unwrap_or_else(|_| file.to_path_buf());
let key_symbol: SharedStr = Arc::from(symbol);
if !visited.insert((canon.clone(), Arc::clone(&key_symbol))) {
return; }
if let Some(sites) = self.reverse_sites(&canon, key_symbol.as_ref()) {
for site in sites {
result.push(CallerSite {
caller_file: site.caller_file.as_ref().clone(),
caller_symbol: site.caller_symbol.to_string(),
line: site.line,
col: site.col,
resolved: site.resolved,
});
if current_depth + 1 < max_depth {
self.collect_callers_recursive(
site.caller_file.as_ref(),
site.caller_symbol.as_ref(),
max_depth,
current_depth + 1,
visited,
result,
);
}
}
}
}
pub fn invalidate_file(&mut self, path: &Path) {
self.data.remove(path);
if let Ok(canon) = self.canonicalize(path) {
self.data.remove(&canon);
}
self.reverse_index = None;
self.project_files = None;
}
fn relative_path(&self, path: &Path) -> String {
path.strip_prefix(&self.project_root)
.unwrap_or(path)
.display()
.to_string()
}
fn canonicalize(&self, path: &Path) -> Result<PathBuf, AftError> {
let full_path = if path.is_relative() {
self.project_root.join(path)
} else {
path.to_path_buf()
};
Ok(std::fs::canonicalize(&full_path).unwrap_or(full_path))
}
fn lookup_file_data(&self, path: &Path) -> Option<&FileCallData> {
if let Some(fd) = self.data.get(path) {
return Some(fd);
}
let canon = std::fs::canonicalize(path).ok()?;
self.data.get(&canon).or_else(|| {
self.data.iter().find_map(|(k, v)| {
if std::fs::canonicalize(k).ok().as_ref() == Some(&canon) {
Some(v)
} else {
None
}
})
})
}
}
fn build_file_data(path: &Path) -> Result<FileCallData, AftError> {
let lang = detect_language(path).ok_or_else(|| AftError::InvalidRequest {
message: format!("unsupported file for call graph: {}", path.display()),
})?;
let source = std::fs::read_to_string(path).map_err(|e| AftError::FileNotFound {
path: format!("{}: {}", path.display(), e),
})?;
let grammar = grammar_for(lang);
let mut parser = Parser::new();
parser
.set_language(&grammar)
.map_err(|e| AftError::ParseError {
message: format!("grammar init failed for {:?}: {}", lang, e),
})?;
let tree = parser
.parse(&source, None)
.ok_or_else(|| AftError::ParseError {
message: format!("parse failed for {}", path.display()),
})?;
let import_block = imports::parse_imports(&source, &tree, lang);
let symbols = list_symbols_from_tree(&source, &tree, lang, path);
let mut calls_by_symbol: HashMap<String, Vec<CallSite>> = HashMap::new();
let root = tree.root_node();
for sym in &symbols {
let byte_start = line_col_to_byte(&source, sym.start_line, sym.start_col);
let byte_end = line_col_to_byte(&source, sym.end_line, sym.end_col);
let raw_calls = extract_calls_full(&source, root, byte_start, byte_end, lang);
let sites: Vec<CallSite> = raw_calls
.into_iter()
.filter(|(_, short, _)| *short != sym.name) .map(|(full, short, line)| CallSite {
callee_name: short,
full_callee: full,
line,
byte_start,
byte_end,
})
.collect();
if !sites.is_empty() {
calls_by_symbol.insert(sym.name.clone(), sites);
}
}
let exported_symbols: Vec<String> = symbols
.iter()
.filter(|s| s.exported)
.map(|s| s.name.clone())
.collect();
let symbol_metadata: HashMap<String, SymbolMeta> = symbols
.iter()
.map(|s| {
(
s.name.clone(),
SymbolMeta {
kind: s.kind.clone(),
exported: s.exported,
signature: s.signature.clone(),
},
)
})
.collect();
Ok(FileCallData {
calls_by_symbol,
exported_symbols,
symbol_metadata,
import_block,
lang,
})
}
#[derive(Debug)]
#[allow(dead_code)]
struct SymbolInfo {
name: String,
kind: SymbolKind,
start_line: u32,
start_col: u32,
end_line: u32,
end_col: u32,
exported: bool,
signature: Option<String>,
}
fn list_symbols_from_tree(
_source: &str,
_tree: &Tree,
_lang: LangId,
path: &Path,
) -> Vec<SymbolInfo> {
let mut file_parser = crate::parser::FileParser::new();
match file_parser.parse(path) {
Ok(_) => {}
Err(_) => return vec![],
}
let provider = crate::parser::TreeSitterProvider::new();
match provider.list_symbols(path) {
Ok(symbols) => symbols
.into_iter()
.map(|s| SymbolInfo {
name: s.name,
kind: s.kind,
start_line: s.range.start_line,
start_col: s.range.start_col,
end_line: s.range.end_line,
end_col: s.range.end_col,
exported: s.exported,
signature: s.signature,
})
.collect(),
Err(_) => vec![],
}
}
fn get_symbol_meta(path: &Path, symbol_name: &str) -> (u32, Option<String>) {
let provider = crate::parser::TreeSitterProvider::new();
match provider.list_symbols(path) {
Ok(symbols) => {
for s in &symbols {
if s.name == symbol_name {
return (s.range.start_line + 1, s.signature.clone());
}
}
(1, None)
}
Err(_) => (1, None),
}
}
fn node_text(node: tree_sitter::Node, source: &str) -> String {
source[node.start_byte()..node.end_byte()].to_string()
}
fn find_node_covering_range(
root: tree_sitter::Node,
start: usize,
end: usize,
) -> Option<tree_sitter::Node> {
let mut best = None;
let mut cursor = root.walk();
fn walk_covering<'a>(
cursor: &mut tree_sitter::TreeCursor<'a>,
start: usize,
end: usize,
best: &mut Option<tree_sitter::Node<'a>>,
) {
let node = cursor.node();
if node.start_byte() <= start && node.end_byte() >= end {
*best = Some(node);
if cursor.goto_first_child() {
loop {
walk_covering(cursor, start, end, best);
if !cursor.goto_next_sibling() {
break;
}
}
cursor.goto_parent();
}
}
}
walk_covering(&mut cursor, start, end, &mut best);
best
}
fn find_child_by_kind<'a>(
node: tree_sitter::Node<'a>,
kind: &str,
) -> Option<tree_sitter::Node<'a>> {
let mut cursor = node.walk();
if cursor.goto_first_child() {
loop {
if cursor.node().kind() == kind {
return Some(cursor.node());
}
if !cursor.goto_next_sibling() {
break;
}
}
}
None
}
fn extract_callee_names(node: tree_sitter::Node, source: &str) -> (Option<String>, Option<String>) {
let callee = match node.child_by_field_name("function") {
Some(c) => c,
None => return (None, None),
};
let full = node_text(callee, source);
let short = if full.contains('.') {
full.rsplit('.').next().unwrap_or(&full).to_string()
} else {
full.clone()
};
(Some(full), Some(short))
}
pub(crate) fn resolve_module_path(from_dir: &Path, module_path: &str) -> Option<PathBuf> {
if !module_path.starts_with('.') {
return None;
}
let base = from_dir.join(module_path);
if base.is_file() {
return Some(std::fs::canonicalize(&base).unwrap_or(base));
}
let extensions = [".ts", ".tsx", ".js", ".jsx"];
for ext in &extensions {
let with_ext = base.with_extension(ext.trim_start_matches('.'));
if with_ext.is_file() {
return Some(std::fs::canonicalize(&with_ext).unwrap_or(with_ext));
}
}
if base.is_dir() {
if let Some(index) = find_index_file(&base) {
return Some(index);
}
}
None
}
fn find_index_file(dir: &Path) -> Option<PathBuf> {
let candidates = ["index.ts", "index.tsx", "index.js", "index.jsx"];
for name in &candidates {
let p = dir.join(name);
if p.is_file() {
return Some(std::fs::canonicalize(&p).unwrap_or(p));
}
}
None
}
fn resolve_aliased_import(
local_name: &str,
import_block: &ImportBlock,
caller_dir: &Path,
) -> Option<(String, PathBuf)> {
for imp in &import_block.imports {
if let Some(original) = find_alias_original(&imp.raw_text, local_name) {
if let Some(resolved_path) = resolve_module_path(caller_dir, &imp.module_path) {
return Some((original, resolved_path));
}
}
}
None
}
fn find_alias_original(raw_import: &str, local_name: &str) -> Option<String> {
let search = format!(" as {}", local_name);
if let Some(pos) = raw_import.find(&search) {
let before = &raw_import[..pos];
let original = before
.rsplit(|c: char| c == '{' || c == ',' || c.is_whitespace())
.find(|s| !s.is_empty())?;
return Some(original.to_string());
}
None
}
pub fn walk_project_files(root: &Path) -> impl Iterator<Item = PathBuf> {
use ignore::WalkBuilder;
let walker = WalkBuilder::new(root)
.hidden(true) .git_ignore(true) .git_global(true) .git_exclude(true) .filter_entry(|entry| {
let name = entry.file_name().to_string_lossy();
if entry.file_type().map_or(false, |ft| ft.is_dir()) {
return !matches!(
name.as_ref(),
"node_modules" | "target" | "venv" | ".venv" | ".git" | "__pycache__"
| ".tox" | "dist" | "build"
);
}
true
})
.build();
walker
.filter_map(|entry| entry.ok())
.filter(|entry| entry.file_type().map_or(false, |ft| ft.is_file()))
.filter(|entry| detect_language(entry.path()).is_some())
.map(|entry| entry.into_path())
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
use tempfile::TempDir;
fn setup_ts_project() -> TempDir {
let dir = TempDir::new().unwrap();
fs::write(
dir.path().join("main.ts"),
r#"import { helper, compute } from './utils';
import * as math from './math';
export function main() {
const a = helper(1);
const b = compute(a, 2);
const c = math.add(a, b);
return c;
}
"#,
)
.unwrap();
fs::write(
dir.path().join("utils.ts"),
r#"import { double } from './helpers';
export function helper(x: number): number {
return double(x);
}
export function compute(a: number, b: number): number {
return a + b;
}
"#,
)
.unwrap();
fs::write(
dir.path().join("helpers.ts"),
r#"export function double(x: number): number {
return x * 2;
}
export function triple(x: number): number {
return x * 3;
}
"#,
)
.unwrap();
fs::write(
dir.path().join("math.ts"),
r#"export function add(a: number, b: number): number {
return a + b;
}
export function subtract(a: number, b: number): number {
return a - b;
}
"#,
)
.unwrap();
dir
}
fn setup_alias_project() -> TempDir {
let dir = TempDir::new().unwrap();
fs::write(
dir.path().join("main.ts"),
r#"import { helper as h } from './utils';
export function main() {
return h(42);
}
"#,
)
.unwrap();
fs::write(
dir.path().join("utils.ts"),
r#"export function helper(x: number): number {
return x + 1;
}
"#,
)
.unwrap();
dir
}
fn setup_cycle_project() -> TempDir {
let dir = TempDir::new().unwrap();
fs::write(
dir.path().join("a.ts"),
r#"import { funcB } from './b';
export function funcA() {
return funcB();
}
"#,
)
.unwrap();
fs::write(
dir.path().join("b.ts"),
r#"import { funcA } from './a';
export function funcB() {
return funcA();
}
"#,
)
.unwrap();
dir
}
#[test]
fn callgraph_single_file_call_extraction() {
let dir = setup_ts_project();
let mut graph = CallGraph::new(dir.path().to_path_buf());
let file_data = graph.build_file(&dir.path().join("main.ts")).unwrap();
let main_calls = &file_data.calls_by_symbol["main"];
let callee_names: Vec<&str> = main_calls.iter().map(|c| c.callee_name.as_str()).collect();
assert!(
callee_names.contains(&"helper"),
"main should call helper, got: {:?}",
callee_names
);
assert!(
callee_names.contains(&"compute"),
"main should call compute, got: {:?}",
callee_names
);
assert!(
callee_names.contains(&"add"),
"main should call math.add (short name: add), got: {:?}",
callee_names
);
}
#[test]
fn callgraph_file_data_has_exports() {
let dir = setup_ts_project();
let mut graph = CallGraph::new(dir.path().to_path_buf());
let file_data = graph.build_file(&dir.path().join("utils.ts")).unwrap();
assert!(
file_data.exported_symbols.contains(&"helper".to_string()),
"utils.ts should export helper, got: {:?}",
file_data.exported_symbols
);
assert!(
file_data.exported_symbols.contains(&"compute".to_string()),
"utils.ts should export compute, got: {:?}",
file_data.exported_symbols
);
}
#[test]
fn callgraph_resolve_direct_import() {
let dir = setup_ts_project();
let mut graph = CallGraph::new(dir.path().to_path_buf());
let main_path = dir.path().join("main.ts");
let file_data = graph.build_file(&main_path).unwrap();
let import_block = file_data.import_block.clone();
let edge = graph.resolve_cross_file_edge("helper", "helper", &main_path, &import_block);
match edge {
EdgeResolution::Resolved { file, symbol } => {
assert!(
file.ends_with("utils.ts"),
"helper should resolve to utils.ts, got: {:?}",
file
);
assert_eq!(symbol, "helper");
}
EdgeResolution::Unresolved { callee_name } => {
panic!("Expected resolved, got unresolved: {}", callee_name);
}
}
}
#[test]
fn callgraph_resolve_namespace_import() {
let dir = setup_ts_project();
let mut graph = CallGraph::new(dir.path().to_path_buf());
let main_path = dir.path().join("main.ts");
let file_data = graph.build_file(&main_path).unwrap();
let import_block = file_data.import_block.clone();
let edge = graph.resolve_cross_file_edge("math.add", "add", &main_path, &import_block);
match edge {
EdgeResolution::Resolved { file, symbol } => {
assert!(
file.ends_with("math.ts"),
"math.add should resolve to math.ts, got: {:?}",
file
);
assert_eq!(symbol, "add");
}
EdgeResolution::Unresolved { callee_name } => {
panic!("Expected resolved, got unresolved: {}", callee_name);
}
}
}
#[test]
fn callgraph_resolve_aliased_import() {
let dir = setup_alias_project();
let mut graph = CallGraph::new(dir.path().to_path_buf());
let main_path = dir.path().join("main.ts");
let file_data = graph.build_file(&main_path).unwrap();
let import_block = file_data.import_block.clone();
let edge = graph.resolve_cross_file_edge("h", "h", &main_path, &import_block);
match edge {
EdgeResolution::Resolved { file, symbol } => {
assert!(
file.ends_with("utils.ts"),
"h (alias for helper) should resolve to utils.ts, got: {:?}",
file
);
assert_eq!(symbol, "helper");
}
EdgeResolution::Unresolved { callee_name } => {
panic!("Expected resolved, got unresolved: {}", callee_name);
}
}
}
#[test]
fn callgraph_unresolved_edge_marked() {
let dir = setup_ts_project();
let mut graph = CallGraph::new(dir.path().to_path_buf());
let main_path = dir.path().join("main.ts");
let file_data = graph.build_file(&main_path).unwrap();
let import_block = file_data.import_block.clone();
let edge =
graph.resolve_cross_file_edge("unknownFunc", "unknownFunc", &main_path, &import_block);
assert_eq!(
edge,
EdgeResolution::Unresolved {
callee_name: "unknownFunc".to_string()
},
"Unknown callee should be unresolved"
);
}
#[test]
fn callgraph_cycle_detection_stops() {
let dir = setup_cycle_project();
let mut graph = CallGraph::new(dir.path().to_path_buf());
let tree = graph
.forward_tree(&dir.path().join("a.ts"), "funcA", 10)
.unwrap();
assert_eq!(tree.name, "funcA");
assert!(tree.resolved);
fn count_depth(node: &CallTreeNode) -> usize {
if node.children.is_empty() {
1
} else {
1 + node
.children
.iter()
.map(|c| count_depth(c))
.max()
.unwrap_or(0)
}
}
let depth = count_depth(&tree);
assert!(
depth <= 4,
"Cycle should be detected and bounded, depth was: {}",
depth
);
}
#[test]
fn callgraph_depth_limit_truncates() {
let dir = setup_ts_project();
let mut graph = CallGraph::new(dir.path().to_path_buf());
let tree = graph
.forward_tree(&dir.path().join("main.ts"), "main", 1)
.unwrap();
assert_eq!(tree.name, "main");
for child in &tree.children {
assert!(
child.children.is_empty(),
"At depth 1, child '{}' should have no children, got {:?}",
child.name,
child.children.len()
);
}
}
#[test]
fn callgraph_depth_zero_no_children() {
let dir = setup_ts_project();
let mut graph = CallGraph::new(dir.path().to_path_buf());
let tree = graph
.forward_tree(&dir.path().join("main.ts"), "main", 0)
.unwrap();
assert_eq!(tree.name, "main");
assert!(
tree.children.is_empty(),
"At depth 0, should have no children"
);
}
#[test]
fn callgraph_forward_tree_cross_file() {
let dir = setup_ts_project();
let mut graph = CallGraph::new(dir.path().to_path_buf());
let tree = graph
.forward_tree(&dir.path().join("main.ts"), "main", 5)
.unwrap();
assert_eq!(tree.name, "main");
assert!(tree.resolved);
let helper_child = tree.children.iter().find(|c| c.name == "helper");
assert!(
helper_child.is_some(),
"main should have helper as child, children: {:?}",
tree.children.iter().map(|c| &c.name).collect::<Vec<_>>()
);
let helper = helper_child.unwrap();
assert!(
helper.file.ends_with("utils.ts") || helper.file == "utils.ts",
"helper should be in utils.ts, got: {}",
helper.file
);
let double_child = helper.children.iter().find(|c| c.name == "double");
assert!(
double_child.is_some(),
"helper should call double, children: {:?}",
helper.children.iter().map(|c| &c.name).collect::<Vec<_>>()
);
let double = double_child.unwrap();
assert!(
double.file.ends_with("helpers.ts") || double.file == "helpers.ts",
"double should be in helpers.ts, got: {}",
double.file
);
}
#[test]
fn callgraph_walker_excludes_gitignored() {
let dir = TempDir::new().unwrap();
fs::write(dir.path().join(".gitignore"), "ignored_dir/\n").unwrap();
fs::write(dir.path().join("main.ts"), "export function main() {}").unwrap();
fs::create_dir(dir.path().join("ignored_dir")).unwrap();
fs::write(
dir.path().join("ignored_dir").join("secret.ts"),
"export function secret() {}",
)
.unwrap();
fs::create_dir(dir.path().join("node_modules")).unwrap();
fs::write(
dir.path().join("node_modules").join("dep.ts"),
"export function dep() {}",
)
.unwrap();
std::process::Command::new("git")
.args(["init"])
.current_dir(dir.path())
.output()
.unwrap();
let files: Vec<PathBuf> = walk_project_files(dir.path()).collect();
let file_names: Vec<String> = files
.iter()
.map(|f| f.file_name().unwrap().to_string_lossy().to_string())
.collect();
assert!(
file_names.contains(&"main.ts".to_string()),
"Should include main.ts, got: {:?}",
file_names
);
assert!(
!file_names.contains(&"secret.ts".to_string()),
"Should exclude gitignored secret.ts, got: {:?}",
file_names
);
assert!(
!file_names.contains(&"dep.ts".to_string()),
"Should exclude node_modules, got: {:?}",
file_names
);
}
#[test]
fn callgraph_walker_only_source_files() {
let dir = TempDir::new().unwrap();
fs::write(dir.path().join("main.ts"), "export function main() {}").unwrap();
fs::write(dir.path().join("readme.md"), "# Hello").unwrap();
fs::write(dir.path().join("data.json"), "{}").unwrap();
let files: Vec<PathBuf> = walk_project_files(dir.path()).collect();
let file_names: Vec<String> = files
.iter()
.map(|f| f.file_name().unwrap().to_string_lossy().to_string())
.collect();
assert!(file_names.contains(&"main.ts".to_string()));
assert!(
file_names.contains(&"readme.md".to_string()),
"Markdown is now a supported source language"
);
assert!(
!file_names.contains(&"data.json".to_string()),
"Should not include non-source files"
);
}
#[test]
fn callgraph_find_alias_original_simple() {
let raw = "import { foo as bar } from './utils';";
assert_eq!(find_alias_original(raw, "bar"), Some("foo".to_string()));
}
#[test]
fn callgraph_find_alias_original_multiple() {
let raw = "import { foo as bar, baz as qux } from './utils';";
assert_eq!(find_alias_original(raw, "bar"), Some("foo".to_string()));
assert_eq!(find_alias_original(raw, "qux"), Some("baz".to_string()));
}
#[test]
fn callgraph_find_alias_no_match() {
let raw = "import { foo } from './utils';";
assert_eq!(find_alias_original(raw, "foo"), None);
}
#[test]
fn callgraph_callers_of_direct() {
let dir = setup_ts_project();
let mut graph = CallGraph::new(dir.path().to_path_buf());
let result = graph
.callers_of(&dir.path().join("helpers.ts"), "double", 1)
.unwrap();
assert_eq!(result.symbol, "double");
assert!(result.total_callers > 0, "double should have callers");
assert!(result.scanned_files > 0, "should have scanned files");
let utils_group = result.callers.iter().find(|g| g.file.contains("utils.ts"));
assert!(
utils_group.is_some(),
"double should be called from utils.ts, groups: {:?}",
result.callers.iter().map(|g| &g.file).collect::<Vec<_>>()
);
let group = utils_group.unwrap();
let helper_caller = group.callers.iter().find(|c| c.symbol == "helper");
assert!(
helper_caller.is_some(),
"double should be called by helper, callers: {:?}",
group.callers.iter().map(|c| &c.symbol).collect::<Vec<_>>()
);
}
#[test]
fn callgraph_callers_of_no_callers() {
let dir = setup_ts_project();
let mut graph = CallGraph::new(dir.path().to_path_buf());
let result = graph
.callers_of(&dir.path().join("main.ts"), "main", 1)
.unwrap();
assert_eq!(result.symbol, "main");
assert_eq!(result.total_callers, 0, "main should have no callers");
assert!(result.callers.is_empty());
}
#[test]
fn callgraph_callers_recursive_depth() {
let dir = setup_ts_project();
let mut graph = CallGraph::new(dir.path().to_path_buf());
let result = graph
.callers_of(&dir.path().join("helpers.ts"), "double", 2)
.unwrap();
assert!(
result.total_callers >= 2,
"with depth 2, double should have >= 2 callers (direct + transitive), got {}",
result.total_callers
);
let main_group = result.callers.iter().find(|g| g.file.contains("main.ts"));
assert!(
main_group.is_some(),
"recursive callers should include main.ts, groups: {:?}",
result.callers.iter().map(|g| &g.file).collect::<Vec<_>>()
);
}
#[test]
fn callgraph_invalidate_file_clears_reverse_index() {
let dir = setup_ts_project();
let mut graph = CallGraph::new(dir.path().to_path_buf());
let _ = graph
.callers_of(&dir.path().join("helpers.ts"), "double", 1)
.unwrap();
assert!(
graph.reverse_index.is_some(),
"reverse index should be built"
);
graph.invalidate_file(&dir.path().join("utils.ts"));
assert!(
graph.reverse_index.is_none(),
"invalidate_file should clear reverse index"
);
let canon = std::fs::canonicalize(dir.path().join("utils.ts")).unwrap();
assert!(
!graph.data.contains_key(&canon),
"invalidate_file should remove file from data cache"
);
assert!(
graph.project_files.is_none(),
"invalidate_file should clear project_files"
);
}
#[test]
fn is_entry_point_exported_function() {
assert!(is_entry_point(
"handleRequest",
&SymbolKind::Function,
true,
LangId::TypeScript
));
}
#[test]
fn is_entry_point_exported_method_is_not_entry() {
assert!(!is_entry_point(
"handleRequest",
&SymbolKind::Method,
true,
LangId::TypeScript
));
}
#[test]
fn is_entry_point_main_init_patterns() {
for name in &["main", "Main", "MAIN", "init", "setup", "bootstrap", "run"] {
assert!(
is_entry_point(name, &SymbolKind::Function, false, LangId::TypeScript),
"{} should be an entry point",
name
);
}
}
#[test]
fn is_entry_point_test_patterns_ts() {
assert!(is_entry_point(
"describe",
&SymbolKind::Function,
false,
LangId::TypeScript
));
assert!(is_entry_point(
"it",
&SymbolKind::Function,
false,
LangId::TypeScript
));
assert!(is_entry_point(
"test",
&SymbolKind::Function,
false,
LangId::TypeScript
));
assert!(is_entry_point(
"testValidation",
&SymbolKind::Function,
false,
LangId::TypeScript
));
assert!(is_entry_point(
"specHelper",
&SymbolKind::Function,
false,
LangId::TypeScript
));
}
#[test]
fn is_entry_point_test_patterns_python() {
assert!(is_entry_point(
"test_login",
&SymbolKind::Function,
false,
LangId::Python
));
assert!(is_entry_point(
"setUp",
&SymbolKind::Function,
false,
LangId::Python
));
assert!(is_entry_point(
"tearDown",
&SymbolKind::Function,
false,
LangId::Python
));
assert!(!is_entry_point(
"testSomething",
&SymbolKind::Function,
false,
LangId::Python
));
}
#[test]
fn is_entry_point_test_patterns_rust() {
assert!(is_entry_point(
"test_parse",
&SymbolKind::Function,
false,
LangId::Rust
));
assert!(!is_entry_point(
"TestSomething",
&SymbolKind::Function,
false,
LangId::Rust
));
}
#[test]
fn is_entry_point_test_patterns_go() {
assert!(is_entry_point(
"TestParsing",
&SymbolKind::Function,
false,
LangId::Go
));
assert!(!is_entry_point(
"testParsing",
&SymbolKind::Function,
false,
LangId::Go
));
}
#[test]
fn is_entry_point_non_exported_non_main_is_not_entry() {
assert!(!is_entry_point(
"helperUtil",
&SymbolKind::Function,
false,
LangId::TypeScript
));
}
#[test]
fn callgraph_symbol_metadata_populated() {
let dir = setup_ts_project();
let mut graph = CallGraph::new(dir.path().to_path_buf());
let file_data = graph.build_file(&dir.path().join("utils.ts")).unwrap();
assert!(
file_data.symbol_metadata.contains_key("helper"),
"symbol_metadata should contain helper"
);
let meta = &file_data.symbol_metadata["helper"];
assert_eq!(meta.kind, SymbolKind::Function);
assert!(meta.exported, "helper should be exported");
}
fn setup_trace_project() -> TempDir {
let dir = TempDir::new().unwrap();
fs::write(
dir.path().join("main.ts"),
r#"import { processData } from './utils';
export function main() {
const result = processData("hello");
return result;
}
"#,
)
.unwrap();
fs::write(
dir.path().join("service.ts"),
r#"import { processData } from './utils';
export function handleRequest(input: string): string {
return processData(input);
}
"#,
)
.unwrap();
fs::write(
dir.path().join("utils.ts"),
r#"import { validate } from './helpers';
export function processData(input: string): string {
const valid = validate(input);
if (!valid) {
throw new Error("invalid input");
}
return input.toUpperCase();
}
"#,
)
.unwrap();
fs::write(
dir.path().join("helpers.ts"),
r#"export function validate(input: string): boolean {
return checkFormat(input);
}
function checkFormat(input: string): boolean {
return input.length > 0 && /^[a-zA-Z]+$/.test(input);
}
"#,
)
.unwrap();
fs::write(
dir.path().join("test_helpers.ts"),
r#"import { validate } from './helpers';
function testValidation() {
const result = validate("hello");
console.log(result);
}
"#,
)
.unwrap();
std::process::Command::new("git")
.args(["init"])
.current_dir(dir.path())
.output()
.unwrap();
dir
}
#[test]
fn trace_to_multi_path() {
let dir = setup_trace_project();
let mut graph = CallGraph::new(dir.path().to_path_buf());
let result = graph
.trace_to(&dir.path().join("helpers.ts"), "checkFormat", 10)
.unwrap();
assert_eq!(result.target_symbol, "checkFormat");
assert!(
result.total_paths >= 2,
"checkFormat should have at least 2 paths, got {} (paths: {:?})",
result.total_paths,
result
.paths
.iter()
.map(|p| p.hops.iter().map(|h| h.symbol.as_str()).collect::<Vec<_>>())
.collect::<Vec<_>>()
);
for path in &result.paths {
assert!(
path.hops.first().unwrap().is_entry_point,
"First hop should be an entry point, got: {}",
path.hops.first().unwrap().symbol
);
assert_eq!(
path.hops.last().unwrap().symbol,
"checkFormat",
"Last hop should be checkFormat"
);
}
assert!(
result.entry_points_found >= 2,
"should find at least 2 entry points, got {}",
result.entry_points_found
);
}
#[test]
fn trace_to_single_path() {
let dir = setup_trace_project();
let mut graph = CallGraph::new(dir.path().to_path_buf());
let result = graph
.trace_to(&dir.path().join("helpers.ts"), "validate", 10)
.unwrap();
assert_eq!(result.target_symbol, "validate");
assert!(
result.total_paths >= 2,
"validate should have at least 2 paths, got {}",
result.total_paths
);
}
#[test]
fn trace_to_cycle_detection() {
let dir = setup_cycle_project();
let mut graph = CallGraph::new(dir.path().to_path_buf());
let result = graph
.trace_to(&dir.path().join("a.ts"), "funcA", 10)
.unwrap();
assert_eq!(result.target_symbol, "funcA");
}
#[test]
fn trace_to_depth_limit() {
let dir = setup_trace_project();
let mut graph = CallGraph::new(dir.path().to_path_buf());
let result = graph
.trace_to(&dir.path().join("helpers.ts"), "checkFormat", 1)
.unwrap();
assert_eq!(result.target_symbol, "checkFormat");
let deep_result = graph
.trace_to(&dir.path().join("helpers.ts"), "checkFormat", 10)
.unwrap();
assert!(
result.total_paths <= deep_result.total_paths,
"shallow trace should find <= paths compared to deep: {} vs {}",
result.total_paths,
deep_result.total_paths
);
}
#[test]
fn trace_to_entry_point_target() {
let dir = setup_trace_project();
let mut graph = CallGraph::new(dir.path().to_path_buf());
let result = graph
.trace_to(&dir.path().join("main.ts"), "main", 10)
.unwrap();
assert_eq!(result.target_symbol, "main");
assert!(
result.total_paths >= 1,
"main should have at least 1 path (itself), got {}",
result.total_paths
);
let trivial = result.paths.iter().find(|p| p.hops.len() == 1);
assert!(
trivial.is_some(),
"should have a trivial path with just the entry point itself"
);
}
#[test]
fn extract_parameters_typescript() {
let params = extract_parameters(
"function processData(input: string, count: number): void",
LangId::TypeScript,
);
assert_eq!(params, vec!["input", "count"]);
}
#[test]
fn extract_parameters_typescript_optional() {
let params = extract_parameters(
"function fetch(url: string, options?: RequestInit): Promise<Response>",
LangId::TypeScript,
);
assert_eq!(params, vec!["url", "options"]);
}
#[test]
fn extract_parameters_typescript_defaults() {
let params = extract_parameters(
"function greet(name: string, greeting: string = \"hello\"): string",
LangId::TypeScript,
);
assert_eq!(params, vec!["name", "greeting"]);
}
#[test]
fn extract_parameters_typescript_rest() {
let params = extract_parameters(
"function sum(...numbers: number[]): number",
LangId::TypeScript,
);
assert_eq!(params, vec!["numbers"]);
}
#[test]
fn extract_parameters_python_self_skipped() {
let params = extract_parameters(
"def process(self, data: str, count: int) -> bool",
LangId::Python,
);
assert_eq!(params, vec!["data", "count"]);
}
#[test]
fn extract_parameters_python_no_self() {
let params = extract_parameters("def validate(input: str) -> bool", LangId::Python);
assert_eq!(params, vec!["input"]);
}
#[test]
fn extract_parameters_python_star_args() {
let params = extract_parameters("def func(*args, **kwargs)", LangId::Python);
assert_eq!(params, vec!["args", "kwargs"]);
}
#[test]
fn extract_parameters_rust_self_skipped() {
let params = extract_parameters(
"fn process(&self, data: &str, count: usize) -> bool",
LangId::Rust,
);
assert_eq!(params, vec!["data", "count"]);
}
#[test]
fn extract_parameters_rust_mut_self_skipped() {
let params = extract_parameters("fn update(&mut self, value: i32)", LangId::Rust);
assert_eq!(params, vec!["value"]);
}
#[test]
fn extract_parameters_rust_no_self() {
let params = extract_parameters("fn validate(input: &str) -> bool", LangId::Rust);
assert_eq!(params, vec!["input"]);
}
#[test]
fn extract_parameters_rust_mut_param() {
let params = extract_parameters("fn process(mut buf: Vec<u8>, len: usize)", LangId::Rust);
assert_eq!(params, vec!["buf", "len"]);
}
#[test]
fn extract_parameters_go() {
let params = extract_parameters(
"func ProcessData(input string, count int) error",
LangId::Go,
);
assert_eq!(params, vec!["input", "count"]);
}
#[test]
fn extract_parameters_empty() {
let params = extract_parameters("function noArgs(): void", LangId::TypeScript);
assert!(
params.is_empty(),
"no-arg function should return empty params"
);
}
#[test]
fn extract_parameters_no_parens() {
let params = extract_parameters("const x = 42", LangId::TypeScript);
assert!(params.is_empty(), "no parens should return empty params");
}
#[test]
fn extract_parameters_javascript() {
let params = extract_parameters("function handleClick(event, target)", LangId::JavaScript);
assert_eq!(params, vec!["event", "target"]);
}
}