1use std::collections::{HashMap, HashSet};
8use std::path::{Path, PathBuf};
9
10use serde::Serialize;
11use tree_sitter::{Parser, Tree};
12
13use crate::calls::extract_calls_full;
14use crate::error::AftError;
15use crate::imports::{self, ImportBlock};
16use crate::language::LanguageProvider;
17use crate::parser::{detect_language, grammar_for, LangId};
18use crate::symbols::SymbolKind;
19
20#[derive(Debug, Clone)]
26pub struct CallSite {
27 pub callee_name: String,
29 pub full_callee: String,
31 pub line: u32,
33 pub byte_start: usize,
35 pub byte_end: usize,
36}
37
38#[derive(Debug, Clone, Serialize)]
40pub struct SymbolMeta {
41 pub kind: SymbolKind,
43 pub exported: bool,
45 #[serde(skip_serializing_if = "Option::is_none")]
47 pub signature: Option<String>,
48}
49
50#[derive(Debug, Clone)]
53pub struct FileCallData {
54 pub calls_by_symbol: HashMap<String, Vec<CallSite>>,
56 pub exported_symbols: Vec<String>,
58 pub symbol_metadata: HashMap<String, SymbolMeta>,
60 pub import_block: ImportBlock,
62 pub lang: LangId,
64}
65
66#[derive(Debug, Clone, PartialEq, Eq)]
68pub enum EdgeResolution {
69 Resolved { file: PathBuf, symbol: String },
71 Unresolved { callee_name: String },
73}
74
75#[derive(Debug, Clone, Serialize)]
77pub struct CallerSite {
78 pub caller_file: PathBuf,
80 pub caller_symbol: String,
82 pub line: u32,
84 pub col: u32,
86 pub resolved: bool,
88}
89
90#[derive(Debug, Clone, Serialize)]
92pub struct CallerGroup {
93 pub file: String,
95 pub callers: Vec<CallerEntry>,
97}
98
99#[derive(Debug, Clone, Serialize)]
101pub struct CallerEntry {
102 pub symbol: String,
103 pub line: u32,
105}
106
107#[derive(Debug, Clone, Serialize)]
109pub struct CallersResult {
110 pub symbol: String,
112 pub file: String,
114 pub callers: Vec<CallerGroup>,
116 pub total_callers: usize,
118 pub scanned_files: usize,
120}
121
122#[derive(Debug, Clone, Serialize)]
124pub struct CallTreeNode {
125 pub name: String,
127 pub file: String,
129 pub line: u32,
131 #[serde(skip_serializing_if = "Option::is_none")]
133 pub signature: Option<String>,
134 pub resolved: bool,
136 pub children: Vec<CallTreeNode>,
138}
139
140const MAIN_INIT_NAMES: &[&str] = &["main", "init", "setup", "bootstrap", "run"];
146
147pub fn is_entry_point(name: &str, kind: &SymbolKind, exported: bool, lang: LangId) -> bool {
154 if exported && *kind == SymbolKind::Function {
156 return true;
157 }
158
159 let lower = name.to_lowercase();
161 if MAIN_INIT_NAMES.contains(&lower.as_str()) {
162 return true;
163 }
164
165 match lang {
167 LangId::TypeScript | LangId::JavaScript | LangId::Tsx => {
168 matches!(lower.as_str(), "describe" | "it" | "test")
170 || lower.starts_with("test")
171 || lower.starts_with("spec")
172 }
173 LangId::Python => {
174 lower.starts_with("test_") || matches!(name, "setUp" | "tearDown")
176 }
177 LangId::Rust => {
178 lower.starts_with("test_")
180 }
181 LangId::Go => {
182 name.starts_with("Test")
184 }
185 LangId::Markdown => false,
186 }
187}
188
189#[derive(Debug, Clone, Serialize)]
195pub struct TraceHop {
196 pub symbol: String,
198 pub file: String,
200 pub line: u32,
202 #[serde(skip_serializing_if = "Option::is_none")]
204 pub signature: Option<String>,
205 pub is_entry_point: bool,
207}
208
209#[derive(Debug, Clone, Serialize)]
211pub struct TracePath {
212 pub hops: Vec<TraceHop>,
214}
215
216#[derive(Debug, Clone, Serialize)]
218pub struct TraceToResult {
219 pub target_symbol: String,
221 pub target_file: String,
223 pub paths: Vec<TracePath>,
225 pub total_paths: usize,
227 pub entry_points_found: usize,
229 pub max_depth_reached: bool,
231 pub truncated_paths: usize,
233}
234
235#[derive(Debug, Clone, Serialize)]
241pub struct ImpactCaller {
242 pub caller_symbol: String,
244 pub caller_file: String,
246 pub line: u32,
248 #[serde(skip_serializing_if = "Option::is_none")]
250 pub signature: Option<String>,
251 pub is_entry_point: bool,
253 #[serde(skip_serializing_if = "Option::is_none")]
255 pub call_expression: Option<String>,
256 pub parameters: Vec<String>,
258}
259
260#[derive(Debug, Clone, Serialize)]
262pub struct ImpactResult {
263 pub symbol: String,
265 pub file: String,
267 #[serde(skip_serializing_if = "Option::is_none")]
269 pub signature: Option<String>,
270 pub parameters: Vec<String>,
272 pub total_affected: usize,
274 pub affected_files: usize,
276 pub callers: Vec<ImpactCaller>,
278}
279
280#[derive(Debug, Clone, Serialize)]
286pub struct DataFlowHop {
287 pub file: String,
289 pub symbol: String,
291 pub variable: String,
293 pub line: u32,
295 pub flow_type: String,
297 pub approximate: bool,
299}
300
301#[derive(Debug, Clone, Serialize)]
304pub struct TraceDataResult {
305 pub expression: String,
307 pub origin_file: String,
309 pub origin_symbol: String,
311 pub hops: Vec<DataFlowHop>,
313 pub depth_limited: bool,
315}
316
317pub fn extract_parameters(signature: &str, lang: LangId) -> Vec<String> {
323 let start = match signature.find('(') {
325 Some(i) => i + 1,
326 None => return Vec::new(),
327 };
328 let end = match signature[start..].find(')') {
329 Some(i) => start + i,
330 None => return Vec::new(),
331 };
332
333 let params_str = &signature[start..end].trim();
334 if params_str.is_empty() {
335 return Vec::new();
336 }
337
338 let parts = split_params(params_str);
340
341 let mut result = Vec::new();
342 for part in parts {
343 let trimmed = part.trim();
344 if trimmed.is_empty() {
345 continue;
346 }
347
348 match lang {
350 LangId::Rust => {
351 let normalized = trimmed.replace(' ', "");
352 if normalized == "self"
353 || normalized == "&self"
354 || normalized == "&mutself"
355 || normalized == "mutself"
356 {
357 continue;
358 }
359 }
360 LangId::Python => {
361 if trimmed == "self" || trimmed.starts_with("self:") {
362 continue;
363 }
364 }
365 _ => {}
366 }
367
368 let name = extract_param_name(trimmed, lang);
370 if !name.is_empty() {
371 result.push(name);
372 }
373 }
374
375 result
376}
377
378fn split_params(s: &str) -> Vec<String> {
380 let mut parts = Vec::new();
381 let mut current = String::new();
382 let mut depth = 0i32;
383
384 for ch in s.chars() {
385 match ch {
386 '<' | '[' | '{' | '(' => {
387 depth += 1;
388 current.push(ch);
389 }
390 '>' | ']' | '}' | ')' => {
391 depth -= 1;
392 current.push(ch);
393 }
394 ',' if depth == 0 => {
395 parts.push(current.clone());
396 current.clear();
397 }
398 _ => {
399 current.push(ch);
400 }
401 }
402 }
403 if !current.is_empty() {
404 parts.push(current);
405 }
406 parts
407}
408
409fn extract_param_name(param: &str, lang: LangId) -> String {
417 let trimmed = param.trim();
418
419 let working = if trimmed.starts_with("...") {
421 &trimmed[3..]
422 } else if trimmed.starts_with("**") {
423 &trimmed[2..]
424 } else if trimmed.starts_with('*') && lang == LangId::Python {
425 &trimmed[1..]
426 } else {
427 trimmed
428 };
429
430 let working = if lang == LangId::Rust && working.starts_with("mut ") {
432 &working[4..]
433 } else {
434 working
435 };
436
437 let name = working
440 .split(|c: char| c == ':' || c == '=')
441 .next()
442 .unwrap_or("")
443 .trim();
444
445 let name = name.trim_end_matches('?');
447
448 if lang == LangId::Go && !name.contains(' ') {
450 return name.to_string();
451 }
452 if lang == LangId::Go {
453 return name.split_whitespace().next().unwrap_or("").to_string();
454 }
455
456 name.to_string()
457}
458
459pub struct CallGraph {
468 data: HashMap<PathBuf, FileCallData>,
470 project_root: PathBuf,
472 project_files: Option<Vec<PathBuf>>,
474 reverse_index: Option<HashMap<(PathBuf, String), Vec<CallerSite>>>,
477}
478
479impl CallGraph {
480 pub fn new(project_root: PathBuf) -> Self {
482 Self {
483 data: HashMap::new(),
484 project_root,
485 project_files: None,
486 reverse_index: None,
487 }
488 }
489
490 pub fn project_root(&self) -> &Path {
492 &self.project_root
493 }
494
495 pub fn build_file(&mut self, path: &Path) -> Result<&FileCallData, AftError> {
497 let canon = self.canonicalize(path)?;
498
499 if !self.data.contains_key(&canon) {
500 let file_data = build_file_data(&canon)?;
501 self.data.insert(canon.clone(), file_data);
502 }
503
504 Ok(&self.data[&canon])
505 }
506
507 pub fn resolve_cross_file_edge(
512 &mut self,
513 full_callee: &str,
514 short_name: &str,
515 caller_file: &Path,
516 import_block: &ImportBlock,
517 ) -> EdgeResolution {
518 let caller_dir = caller_file.parent().unwrap_or(Path::new("."));
525
526 if full_callee.contains('.') {
528 let parts: Vec<&str> = full_callee.splitn(2, '.').collect();
529 if parts.len() == 2 {
530 let namespace = parts[0];
531 let member = parts[1];
532
533 for imp in &import_block.imports {
534 if imp.namespace_import.as_deref() == Some(namespace) {
535 if let Some(resolved_path) =
536 resolve_module_path(caller_dir, &imp.module_path)
537 {
538 return EdgeResolution::Resolved {
539 file: resolved_path,
540 symbol: member.to_string(),
541 };
542 }
543 }
544 }
545 }
546 }
547
548 for imp in &import_block.imports {
550 if imp.names.contains(&short_name.to_string()) {
552 if let Some(resolved_path) = resolve_module_path(caller_dir, &imp.module_path) {
553 return EdgeResolution::Resolved {
555 file: resolved_path,
556 symbol: short_name.to_string(),
557 };
558 }
559 }
560
561 if imp.default_import.as_deref() == Some(short_name) {
563 if let Some(resolved_path) = resolve_module_path(caller_dir, &imp.module_path) {
564 return EdgeResolution::Resolved {
565 file: resolved_path,
566 symbol: "default".to_string(),
567 };
568 }
569 }
570 }
571
572 if let Some((original_name, resolved_path)) =
577 resolve_aliased_import(short_name, import_block, caller_dir)
578 {
579 return EdgeResolution::Resolved {
580 file: resolved_path,
581 symbol: original_name,
582 };
583 }
584
585 for imp in &import_block.imports {
588 if let Some(resolved_path) = resolve_module_path(caller_dir, &imp.module_path) {
589 if resolved_path.is_dir() {
591 if let Some(index_path) = find_index_file(&resolved_path) {
592 if self.file_exports_symbol(&index_path, short_name) {
594 return EdgeResolution::Resolved {
595 file: index_path,
596 symbol: short_name.to_string(),
597 };
598 }
599 }
600 } else if self.file_exports_symbol(&resolved_path, short_name) {
601 return EdgeResolution::Resolved {
602 file: resolved_path,
603 symbol: short_name.to_string(),
604 };
605 }
606 }
607 }
608
609 EdgeResolution::Unresolved {
610 callee_name: short_name.to_string(),
611 }
612 }
613
614 fn file_exports_symbol(&mut self, path: &Path, symbol_name: &str) -> bool {
616 match self.build_file(path) {
617 Ok(data) => data.exported_symbols.contains(&symbol_name.to_string()),
618 Err(_) => false,
619 }
620 }
621
622 pub fn forward_tree(
627 &mut self,
628 file: &Path,
629 symbol: &str,
630 max_depth: usize,
631 ) -> Result<CallTreeNode, AftError> {
632 let mut visited = HashSet::new();
633 self.forward_tree_inner(file, symbol, max_depth, 0, &mut visited)
634 }
635
636 fn forward_tree_inner(
637 &mut self,
638 file: &Path,
639 symbol: &str,
640 max_depth: usize,
641 current_depth: usize,
642 visited: &mut HashSet<(PathBuf, String)>,
643 ) -> Result<CallTreeNode, AftError> {
644 let canon = self.canonicalize(file)?;
645 let visit_key = (canon.clone(), symbol.to_string());
646
647 if visited.contains(&visit_key) {
649 let (line, signature) = get_symbol_meta(&canon, symbol);
650 return Ok(CallTreeNode {
651 name: symbol.to_string(),
652 file: self.relative_path(&canon),
653 line,
654 signature,
655 resolved: true,
656 children: vec![], });
658 }
659
660 visited.insert(visit_key.clone());
661
662 let file_data = build_file_data(&canon)?;
664 let import_block = file_data.import_block.clone();
665 let _lang = file_data.lang;
666
667 let call_sites = file_data
669 .calls_by_symbol
670 .get(symbol)
671 .cloned()
672 .unwrap_or_default();
673
674 let (sym_line, sym_signature) = get_symbol_meta(&canon, symbol);
676
677 self.data.insert(canon.clone(), file_data);
679
680 let mut children = Vec::new();
682
683 if current_depth < max_depth {
684 for call_site in &call_sites {
685 let edge = self.resolve_cross_file_edge(
686 &call_site.full_callee,
687 &call_site.callee_name,
688 &canon,
689 &import_block,
690 );
691
692 match edge {
693 EdgeResolution::Resolved {
694 file: ref target_file,
695 ref symbol,
696 } => {
697 match self.forward_tree_inner(
698 target_file,
699 symbol,
700 max_depth,
701 current_depth + 1,
702 visited,
703 ) {
704 Ok(child) => children.push(child),
705 Err(_) => {
706 children.push(CallTreeNode {
708 name: call_site.callee_name.clone(),
709 file: self.relative_path(target_file),
710 line: call_site.line,
711 signature: None,
712 resolved: false,
713 children: vec![],
714 });
715 }
716 }
717 }
718 EdgeResolution::Unresolved { callee_name } => {
719 children.push(CallTreeNode {
720 name: callee_name,
721 file: self.relative_path(&canon),
722 line: call_site.line,
723 signature: None,
724 resolved: false,
725 children: vec![],
726 });
727 }
728 }
729 }
730 }
731
732 visited.remove(&visit_key);
733
734 Ok(CallTreeNode {
735 name: symbol.to_string(),
736 file: self.relative_path(&canon),
737 line: sym_line,
738 signature: sym_signature,
739 resolved: true,
740 children,
741 })
742 }
743
744 pub fn project_files(&mut self) -> &[PathBuf] {
746 if self.project_files.is_none() {
747 let project_root = self.project_root.clone();
748 self.project_files = Some(walk_project_files(&project_root).collect());
749 }
750 self.project_files.as_deref().unwrap_or(&[])
751 }
752
753 fn build_reverse_index(&mut self) {
759 let all_files = self.project_files().to_vec();
761
762 for f in &all_files {
764 if !self.data.contains_key(f) {
765 if let Ok(fd) = build_file_data(f) {
766 self.data.insert(f.clone(), fd);
767 }
768 }
769 }
770
771 let mut reverse: HashMap<(PathBuf, String), Vec<CallerSite>> = HashMap::new();
773
774 for caller_file in &all_files {
775 let canon_caller =
777 std::fs::canonicalize(caller_file).unwrap_or_else(|_| caller_file.clone());
778 let file_data = match self
779 .data
780 .get(caller_file)
781 .or_else(|| self.data.get(&canon_caller))
782 {
783 Some(d) => d.clone(),
784 None => continue,
785 };
786
787 for (symbol_name, call_sites) in &file_data.calls_by_symbol {
788 for call_site in call_sites {
789 let edge = self.resolve_cross_file_edge(
790 &call_site.full_callee,
791 &call_site.callee_name,
792 caller_file,
793 &file_data.import_block,
794 );
795
796 match edge {
797 EdgeResolution::Resolved {
798 file: ref target_file,
799 ref symbol,
800 } => {
801 let key = (target_file.clone(), symbol.clone());
802 reverse.entry(key).or_default().push(CallerSite {
803 caller_file: canon_caller.clone(),
804 caller_symbol: symbol_name.clone(),
805 line: call_site.line,
806 col: 0,
807 resolved: true,
808 });
809 }
810 EdgeResolution::Unresolved { ref callee_name } => {
811 let key = (canon_caller.clone(), callee_name.clone());
814 reverse.entry(key).or_default().push(CallerSite {
815 caller_file: canon_caller.clone(),
816 caller_symbol: symbol_name.clone(),
817 line: call_site.line,
818 col: 0,
819 resolved: false,
820 });
821 }
822 }
823 }
824 }
825 }
826
827 self.reverse_index = Some(reverse);
828 }
829
830 pub fn callers_of(
836 &mut self,
837 file: &Path,
838 symbol: &str,
839 depth: usize,
840 ) -> Result<CallersResult, AftError> {
841 let canon = self.canonicalize(file)?;
842
843 self.build_file(&canon)?;
845
846 if self.reverse_index.is_none() {
848 self.build_reverse_index();
849 }
850
851 let scanned_files = self.project_files.as_ref().map(|f| f.len()).unwrap_or(0);
852 let effective_depth = if depth == 0 { 1 } else { depth };
853
854 let mut visited = HashSet::new();
855 let mut all_sites: Vec<CallerSite> = Vec::new();
856 self.collect_callers_recursive(
857 &canon,
858 symbol,
859 effective_depth,
860 0,
861 &mut visited,
862 &mut all_sites,
863 );
864
865 let mut groups_map: HashMap<PathBuf, Vec<CallerEntry>> = HashMap::new();
867 for site in &all_sites {
868 groups_map
869 .entry(site.caller_file.clone())
870 .or_default()
871 .push(CallerEntry {
872 symbol: site.caller_symbol.clone(),
873 line: site.line,
874 });
875 }
876
877 let total_callers = all_sites.len();
878 let mut callers: Vec<CallerGroup> = groups_map
879 .into_iter()
880 .map(|(file_path, entries)| CallerGroup {
881 file: self.relative_path(&file_path),
882 callers: entries,
883 })
884 .collect();
885
886 callers.sort_by(|a, b| a.file.cmp(&b.file));
888
889 Ok(CallersResult {
890 symbol: symbol.to_string(),
891 file: self.relative_path(&canon),
892 callers,
893 total_callers,
894 scanned_files,
895 })
896 }
897
898 pub fn trace_to(
904 &mut self,
905 file: &Path,
906 symbol: &str,
907 max_depth: usize,
908 ) -> Result<TraceToResult, AftError> {
909 let canon = self.canonicalize(file)?;
910
911 self.build_file(&canon)?;
913
914 if self.reverse_index.is_none() {
916 self.build_reverse_index();
917 }
918
919 let target_rel = self.relative_path(&canon);
920 let effective_max = if max_depth == 0 { 10 } else { max_depth };
921 let reverse_index = self
922 .reverse_index
923 .as_ref()
924 .ok_or_else(|| AftError::ParseError {
925 message: format!(
926 "reverse index unavailable after building callers for {}",
927 canon.display()
928 ),
929 })?;
930
931 let (target_line, target_sig) = get_symbol_meta(&canon, symbol);
933
934 let target_is_entry = self
936 .lookup_file_data(&canon)
937 .and_then(|fd| {
938 let meta = fd.symbol_metadata.get(symbol)?;
939 Some(is_entry_point(symbol, &meta.kind, meta.exported, fd.lang))
940 })
941 .unwrap_or(false);
942
943 type PathElem = (PathBuf, String, u32, Option<String>);
946 let mut complete_paths: Vec<Vec<PathElem>> = Vec::new();
947 let mut max_depth_reached = false;
948 let mut truncated_paths: usize = 0;
949
950 let initial: Vec<PathElem> =
952 vec![(canon.clone(), symbol.to_string(), target_line, target_sig)];
953
954 if target_is_entry {
956 complete_paths.push(initial.clone());
957 }
958
959 let mut queue: Vec<(Vec<PathElem>, usize)> = vec![(initial, 0)];
961
962 while let Some((path, depth)) = queue.pop() {
963 if depth >= effective_max {
964 max_depth_reached = true;
965 continue;
966 }
967
968 let Some((current_file, current_symbol, _, _)) = path.last().cloned() else {
969 continue;
970 };
971
972 let path_visited: HashSet<(PathBuf, String)> = path
974 .iter()
975 .map(|(f, s, _, _)| (f.clone(), s.clone()))
976 .collect();
977
978 let lookup_key = (current_file.clone(), current_symbol.clone());
980 let callers = match reverse_index.get(&lookup_key) {
981 Some(sites) => sites.clone(),
982 None => {
983 if path.len() > 1 {
986 truncated_paths += 1;
989 }
990 continue;
991 }
992 };
993
994 let mut has_new_path = false;
995 for site in &callers {
996 let caller_key = (site.caller_file.clone(), site.caller_symbol.clone());
997
998 if path_visited.contains(&caller_key) {
1000 continue;
1001 }
1002
1003 has_new_path = true;
1004
1005 let (caller_line, caller_sig) =
1007 get_symbol_meta(&site.caller_file, &site.caller_symbol);
1008
1009 let mut new_path = path.clone();
1010 new_path.push((
1011 site.caller_file.clone(),
1012 site.caller_symbol.clone(),
1013 caller_line,
1014 caller_sig,
1015 ));
1016
1017 let caller_is_entry = self
1021 .lookup_file_data(&site.caller_file)
1022 .and_then(|fd| {
1023 let meta = fd.symbol_metadata.get(&site.caller_symbol)?;
1024 Some(is_entry_point(
1025 &site.caller_symbol,
1026 &meta.kind,
1027 meta.exported,
1028 fd.lang,
1029 ))
1030 })
1031 .unwrap_or(false);
1032
1033 if caller_is_entry {
1034 complete_paths.push(new_path.clone());
1035 }
1036 queue.push((new_path, depth + 1));
1039 }
1040
1041 if !has_new_path && path.len() > 1 {
1043 truncated_paths += 1;
1044 }
1045 }
1046
1047 let mut paths: Vec<TracePath> = complete_paths
1050 .into_iter()
1051 .map(|mut elems| {
1052 elems.reverse();
1053 let hops: Vec<TraceHop> = elems
1054 .iter()
1055 .enumerate()
1056 .map(|(i, (file_path, sym, line, sig))| {
1057 let is_ep = if i == 0 {
1058 self.lookup_file_data(file_path)
1060 .and_then(|fd| {
1061 let meta = fd.symbol_metadata.get(sym)?;
1062 Some(is_entry_point(sym, &meta.kind, meta.exported, fd.lang))
1063 })
1064 .unwrap_or(false)
1065 } else {
1066 false
1067 };
1068 TraceHop {
1069 symbol: sym.clone(),
1070 file: self.relative_path(file_path),
1071 line: *line,
1072 signature: sig.clone(),
1073 is_entry_point: is_ep,
1074 }
1075 })
1076 .collect();
1077 TracePath { hops }
1078 })
1079 .collect();
1080
1081 paths.sort_by(|a, b| {
1083 let a_entry = a.hops.first().map(|h| h.symbol.as_str()).unwrap_or("");
1084 let b_entry = b.hops.first().map(|h| h.symbol.as_str()).unwrap_or("");
1085 a_entry.cmp(b_entry).then(a.hops.len().cmp(&b.hops.len()))
1086 });
1087
1088 let mut entry_point_names: HashSet<String> = HashSet::new();
1090 for p in &paths {
1091 if let Some(first) = p.hops.first() {
1092 if first.is_entry_point {
1093 entry_point_names.insert(first.symbol.clone());
1094 }
1095 }
1096 }
1097
1098 let total_paths = paths.len();
1099 let entry_points_found = entry_point_names.len();
1100
1101 Ok(TraceToResult {
1102 target_symbol: symbol.to_string(),
1103 target_file: target_rel,
1104 paths,
1105 total_paths,
1106 entry_points_found,
1107 max_depth_reached,
1108 truncated_paths,
1109 })
1110 }
1111
1112 pub fn impact(
1118 &mut self,
1119 file: &Path,
1120 symbol: &str,
1121 depth: usize,
1122 ) -> Result<ImpactResult, AftError> {
1123 let canon = self.canonicalize(file)?;
1124
1125 self.build_file(&canon)?;
1127
1128 if self.reverse_index.is_none() {
1130 self.build_reverse_index();
1131 }
1132
1133 let effective_depth = if depth == 0 { 1 } else { depth };
1134
1135 let (target_signature, target_parameters, target_lang) = {
1137 let file_data = match self.data.get(&canon) {
1138 Some(d) => d,
1139 None => return Err(AftError::InvalidRequest { message: "file data missing after build".to_string() }),
1140 };
1141 let meta = file_data.symbol_metadata.get(symbol);
1142 let sig = meta.and_then(|m| m.signature.clone());
1143 let lang = file_data.lang;
1144 let params = sig
1145 .as_deref()
1146 .map(|s| extract_parameters(s, lang))
1147 .unwrap_or_default();
1148 (sig, params, lang)
1149 };
1150
1151 let mut visited = HashSet::new();
1153 let mut all_sites: Vec<CallerSite> = Vec::new();
1154 self.collect_callers_recursive(
1155 &canon,
1156 symbol,
1157 effective_depth,
1158 0,
1159 &mut visited,
1160 &mut all_sites,
1161 );
1162
1163 let mut seen = HashSet::new();
1165 all_sites.retain(|s| seen.insert((s.caller_file.clone(), s.caller_symbol.clone(), s.line)));
1166
1167 let mut callers = Vec::new();
1169 let mut affected_file_set = HashSet::new();
1170
1171 for site in &all_sites {
1172 let caller_canon = std::fs::canonicalize(&site.caller_file)
1174 .unwrap_or_else(|_| site.caller_file.clone());
1175 let _ = self.build_file(&caller_canon);
1176
1177 let (sig, is_ep, params, _lang) = {
1178 if let Some(fd) = self.data.get(&caller_canon) {
1179 let meta = fd.symbol_metadata.get(&site.caller_symbol);
1180 let sig = meta.and_then(|m| m.signature.clone());
1181 let kind = meta.map(|m| m.kind.clone()).unwrap_or(SymbolKind::Function);
1182 let exported = meta.map(|m| m.exported).unwrap_or(false);
1183 let is_ep = is_entry_point(&site.caller_symbol, &kind, exported, fd.lang);
1184 let lang = fd.lang;
1185 let params = sig
1186 .as_deref()
1187 .map(|s| extract_parameters(s, lang))
1188 .unwrap_or_default();
1189 (sig, is_ep, params, lang)
1190 } else {
1191 (None, false, Vec::new(), target_lang)
1192 }
1193 };
1194
1195 let call_expression = self.read_source_line(&caller_canon, site.line);
1197
1198 let rel_file = self.relative_path(&site.caller_file);
1199 affected_file_set.insert(rel_file.clone());
1200
1201 callers.push(ImpactCaller {
1202 caller_symbol: site.caller_symbol.clone(),
1203 caller_file: rel_file,
1204 line: site.line,
1205 signature: sig,
1206 is_entry_point: is_ep,
1207 call_expression,
1208 parameters: params,
1209 });
1210 }
1211
1212 callers.sort_by(|a, b| a.caller_file.cmp(&b.caller_file).then(a.line.cmp(&b.line)));
1214
1215 let total_affected = callers.len();
1216 let affected_files = affected_file_set.len();
1217
1218 Ok(ImpactResult {
1219 symbol: symbol.to_string(),
1220 file: self.relative_path(&canon),
1221 signature: target_signature,
1222 parameters: target_parameters,
1223 total_affected,
1224 affected_files,
1225 callers,
1226 })
1227 }
1228
1229 pub fn trace_data(
1240 &mut self,
1241 file: &Path,
1242 symbol: &str,
1243 expression: &str,
1244 max_depth: usize,
1245 ) -> Result<TraceDataResult, AftError> {
1246 let canon = self.canonicalize(file)?;
1247 let rel_file = self.relative_path(&canon);
1248
1249 self.build_file(&canon)?;
1251
1252 {
1254 let fd = match self.data.get(&canon) {
1255 Some(d) => d,
1256 None => return Err(AftError::InvalidRequest { message: "file data missing after build".to_string() }),
1257 };
1258 let has_symbol = fd.calls_by_symbol.contains_key(symbol)
1259 || fd.exported_symbols.contains(&symbol.to_string())
1260 || fd.symbol_metadata.contains_key(symbol);
1261 if !has_symbol {
1262 return Err(AftError::InvalidRequest {
1263 message: format!(
1264 "trace_data: symbol '{}' not found in {}",
1265 symbol,
1266 file.display()
1267 ),
1268 });
1269 }
1270 }
1271
1272 let mut hops = Vec::new();
1273 let mut depth_limited = false;
1274
1275 self.trace_data_inner(
1276 &canon,
1277 symbol,
1278 expression,
1279 max_depth,
1280 0,
1281 &mut hops,
1282 &mut depth_limited,
1283 &mut HashSet::new(),
1284 );
1285
1286 Ok(TraceDataResult {
1287 expression: expression.to_string(),
1288 origin_file: rel_file,
1289 origin_symbol: symbol.to_string(),
1290 hops,
1291 depth_limited,
1292 })
1293 }
1294
1295 fn trace_data_inner(
1297 &mut self,
1298 file: &Path,
1299 symbol: &str,
1300 tracking_name: &str,
1301 max_depth: usize,
1302 current_depth: usize,
1303 hops: &mut Vec<DataFlowHop>,
1304 depth_limited: &mut bool,
1305 visited: &mut HashSet<(PathBuf, String, String)>,
1306 ) {
1307 let visit_key = (
1308 file.to_path_buf(),
1309 symbol.to_string(),
1310 tracking_name.to_string(),
1311 );
1312 if visited.contains(&visit_key) {
1313 return; }
1315 visited.insert(visit_key);
1316
1317 let source = match std::fs::read_to_string(file) {
1319 Ok(s) => s,
1320 Err(_) => return,
1321 };
1322
1323 let lang = match detect_language(file) {
1324 Some(l) => l,
1325 None => return,
1326 };
1327
1328 let grammar = grammar_for(lang);
1329 let mut parser = Parser::new();
1330 if parser.set_language(&grammar).is_err() {
1331 return;
1332 }
1333 let tree = match parser.parse(&source, None) {
1334 Some(t) => t,
1335 None => return,
1336 };
1337
1338 let symbols = list_symbols_from_tree(&source, &tree, lang, file);
1340 let sym_info = match symbols.iter().find(|s| s.name == symbol) {
1341 Some(s) => s,
1342 None => return,
1343 };
1344
1345 let body_start = line_col_to_byte(&source, sym_info.start_line, sym_info.start_col);
1346 let body_end = line_col_to_byte(&source, sym_info.end_line, sym_info.end_col);
1347
1348 let root = tree.root_node();
1349
1350 let body_node = match find_node_covering_range(root, body_start, body_end) {
1352 Some(n) => n,
1353 None => return,
1354 };
1355
1356 let mut tracked_names: Vec<String> = vec![tracking_name.to_string()];
1358 let rel_file = self.relative_path(file);
1359
1360 self.walk_for_data_flow(
1362 body_node,
1363 &source,
1364 &mut tracked_names,
1365 file,
1366 symbol,
1367 &rel_file,
1368 lang,
1369 max_depth,
1370 current_depth,
1371 hops,
1372 depth_limited,
1373 visited,
1374 );
1375 }
1376
1377 #[allow(clippy::too_many_arguments)]
1380 fn walk_for_data_flow(
1381 &mut self,
1382 node: tree_sitter::Node,
1383 source: &str,
1384 tracked_names: &mut Vec<String>,
1385 file: &Path,
1386 symbol: &str,
1387 rel_file: &str,
1388 lang: LangId,
1389 max_depth: usize,
1390 current_depth: usize,
1391 hops: &mut Vec<DataFlowHop>,
1392 depth_limited: &mut bool,
1393 visited: &mut HashSet<(PathBuf, String, String)>,
1394 ) {
1395 let kind = node.kind();
1396
1397 let is_var_decl = matches!(
1399 kind,
1400 "variable_declarator"
1401 | "assignment_expression"
1402 | "augmented_assignment_expression"
1403 | "assignment"
1404 | "let_declaration"
1405 | "short_var_declaration"
1406 );
1407
1408 if is_var_decl {
1409 if let Some((new_name, init_text, line, is_approx)) =
1410 self.extract_assignment_info(node, source, lang, tracked_names)
1411 {
1412 if !is_approx {
1414 hops.push(DataFlowHop {
1415 file: rel_file.to_string(),
1416 symbol: symbol.to_string(),
1417 variable: new_name.clone(),
1418 line,
1419 flow_type: "assignment".to_string(),
1420 approximate: false,
1421 });
1422 tracked_names.push(new_name);
1423 } else {
1424 hops.push(DataFlowHop {
1426 file: rel_file.to_string(),
1427 symbol: symbol.to_string(),
1428 variable: init_text,
1429 line,
1430 flow_type: "assignment".to_string(),
1431 approximate: true,
1432 });
1433 return;
1435 }
1436 }
1437 }
1438
1439 if kind == "call_expression" || kind == "call" || kind == "macro_invocation" {
1441 self.check_call_for_data_flow(
1442 node,
1443 source,
1444 tracked_names,
1445 file,
1446 symbol,
1447 rel_file,
1448 lang,
1449 max_depth,
1450 current_depth,
1451 hops,
1452 depth_limited,
1453 visited,
1454 );
1455 }
1456
1457 let mut cursor = node.walk();
1459 if cursor.goto_first_child() {
1460 loop {
1461 let child = cursor.node();
1462 self.walk_for_data_flow(
1464 child,
1465 source,
1466 tracked_names,
1467 file,
1468 symbol,
1469 rel_file,
1470 lang,
1471 max_depth,
1472 current_depth,
1473 hops,
1474 depth_limited,
1475 visited,
1476 );
1477 if !cursor.goto_next_sibling() {
1478 break;
1479 }
1480 }
1481 }
1482 }
1483
1484 fn extract_assignment_info(
1487 &self,
1488 node: tree_sitter::Node,
1489 source: &str,
1490 _lang: LangId,
1491 tracked_names: &[String],
1492 ) -> Option<(String, String, u32, bool)> {
1493 let kind = node.kind();
1494 let line = node.start_position().row as u32 + 1;
1495
1496 match kind {
1497 "variable_declarator" => {
1498 let name_node = node.child_by_field_name("name")?;
1500 let value_node = node.child_by_field_name("value")?;
1501 let name_text = node_text(name_node, source);
1502 let value_text = node_text(value_node, source);
1503
1504 if name_node.kind() == "object_pattern" || name_node.kind() == "array_pattern" {
1506 if tracked_names.iter().any(|t| value_text.contains(t)) {
1508 return Some((name_text.clone(), name_text, line, true));
1509 }
1510 return None;
1511 }
1512
1513 if tracked_names.iter().any(|t| {
1515 value_text == *t
1516 || value_text.starts_with(&format!("{}.", t))
1517 || value_text.starts_with(&format!("{}[", t))
1518 }) {
1519 return Some((name_text, value_text, line, false));
1520 }
1521 None
1522 }
1523 "assignment_expression" | "augmented_assignment_expression" => {
1524 let left = node.child_by_field_name("left")?;
1526 let right = node.child_by_field_name("right")?;
1527 let left_text = node_text(left, source);
1528 let right_text = node_text(right, source);
1529
1530 if tracked_names.iter().any(|t| right_text == *t) {
1531 return Some((left_text, right_text, line, false));
1532 }
1533 None
1534 }
1535 "assignment" => {
1536 let left = node.child_by_field_name("left")?;
1538 let right = node.child_by_field_name("right")?;
1539 let left_text = node_text(left, source);
1540 let right_text = node_text(right, source);
1541
1542 if tracked_names.iter().any(|t| right_text == *t) {
1543 return Some((left_text, right_text, line, false));
1544 }
1545 None
1546 }
1547 "let_declaration" | "short_var_declaration" => {
1548 let left = node
1550 .child_by_field_name("pattern")
1551 .or_else(|| node.child_by_field_name("left"))?;
1552 let right = node
1553 .child_by_field_name("value")
1554 .or_else(|| node.child_by_field_name("right"))?;
1555 let left_text = node_text(left, source);
1556 let right_text = node_text(right, source);
1557
1558 if tracked_names.iter().any(|t| right_text == *t) {
1559 return Some((left_text, right_text, line, false));
1560 }
1561 None
1562 }
1563 _ => None,
1564 }
1565 }
1566
1567 #[allow(clippy::too_many_arguments)]
1570 fn check_call_for_data_flow(
1571 &mut self,
1572 node: tree_sitter::Node,
1573 source: &str,
1574 tracked_names: &[String],
1575 file: &Path,
1576 _symbol: &str,
1577 rel_file: &str,
1578 _lang: LangId,
1579 max_depth: usize,
1580 current_depth: usize,
1581 hops: &mut Vec<DataFlowHop>,
1582 depth_limited: &mut bool,
1583 visited: &mut HashSet<(PathBuf, String, String)>,
1584 ) {
1585 let args_node = find_child_by_kind(node, "arguments")
1587 .or_else(|| find_child_by_kind(node, "argument_list"));
1588
1589 let args_node = match args_node {
1590 Some(n) => n,
1591 None => return,
1592 };
1593
1594 let mut arg_positions: Vec<(usize, String)> = Vec::new(); let mut arg_idx = 0;
1597
1598 let mut cursor = args_node.walk();
1599 if cursor.goto_first_child() {
1600 loop {
1601 let child = cursor.node();
1602 let child_kind = child.kind();
1603
1604 if child_kind == "(" || child_kind == ")" || child_kind == "," {
1606 if !cursor.goto_next_sibling() {
1607 break;
1608 }
1609 continue;
1610 }
1611
1612 let arg_text = node_text(child, source);
1613
1614 if child_kind == "spread_element" || child_kind == "dictionary_splat" {
1616 if tracked_names.iter().any(|t| arg_text.contains(t)) {
1617 hops.push(DataFlowHop {
1618 file: rel_file.to_string(),
1619 symbol: _symbol.to_string(),
1620 variable: arg_text,
1621 line: child.start_position().row as u32 + 1,
1622 flow_type: "parameter".to_string(),
1623 approximate: true,
1624 });
1625 }
1626 if !cursor.goto_next_sibling() {
1627 break;
1628 }
1629 arg_idx += 1;
1630 continue;
1631 }
1632
1633 if tracked_names.iter().any(|t| arg_text == *t) {
1634 arg_positions.push((arg_idx, arg_text));
1635 }
1636
1637 arg_idx += 1;
1638 if !cursor.goto_next_sibling() {
1639 break;
1640 }
1641 }
1642 }
1643
1644 if arg_positions.is_empty() {
1645 return;
1646 }
1647
1648 let (full_callee, short_callee) = extract_callee_names(node, source);
1650 let full_callee = match full_callee {
1651 Some(f) => f,
1652 None => return,
1653 };
1654 let short_callee = match short_callee {
1655 Some(s) => s,
1656 None => return,
1657 };
1658
1659 let import_block = {
1661 match self.data.get(file) {
1662 Some(fd) => fd.import_block.clone(),
1663 None => return,
1664 }
1665 };
1666
1667 let edge = self.resolve_cross_file_edge(&full_callee, &short_callee, file, &import_block);
1668
1669 match edge {
1670 EdgeResolution::Resolved {
1671 file: target_file,
1672 symbol: target_symbol,
1673 } => {
1674 if current_depth + 1 > max_depth {
1675 *depth_limited = true;
1676 return;
1677 }
1678
1679 let _ = self.build_file(&target_file);
1681 let (params, _target_lang) = {
1682 match self.data.get(&target_file) {
1683 Some(fd) => {
1684 let meta = fd.symbol_metadata.get(&target_symbol);
1685 let sig = meta.and_then(|m| m.signature.clone());
1686 let params = sig
1687 .as_deref()
1688 .map(|s| extract_parameters(s, fd.lang))
1689 .unwrap_or_default();
1690 (params, fd.lang)
1691 }
1692 None => return,
1693 }
1694 };
1695
1696 let target_rel = self.relative_path(&target_file);
1697
1698 for (pos, _tracked) in &arg_positions {
1699 if let Some(param_name) = params.get(*pos) {
1700 hops.push(DataFlowHop {
1702 file: target_rel.clone(),
1703 symbol: target_symbol.clone(),
1704 variable: param_name.clone(),
1705 line: get_symbol_meta(&target_file, &target_symbol).0,
1706 flow_type: "parameter".to_string(),
1707 approximate: false,
1708 });
1709
1710 self.trace_data_inner(
1712 &target_file.clone(),
1713 &target_symbol.clone(),
1714 param_name,
1715 max_depth,
1716 current_depth + 1,
1717 hops,
1718 depth_limited,
1719 visited,
1720 );
1721 }
1722 }
1723 }
1724 EdgeResolution::Unresolved { callee_name } => {
1725 let has_local = self
1727 .data
1728 .get(file)
1729 .map(|fd| {
1730 fd.calls_by_symbol.contains_key(&callee_name)
1731 || fd.symbol_metadata.contains_key(&callee_name)
1732 })
1733 .unwrap_or(false);
1734
1735 if has_local {
1736 let (params, _target_lang) = {
1738 let Some(fd) = self.data.get(file) else {
1739 return;
1740 };
1741 let meta = fd.symbol_metadata.get(&callee_name);
1742 let sig = meta.and_then(|m| m.signature.clone());
1743 let params = sig
1744 .as_deref()
1745 .map(|s| extract_parameters(s, fd.lang))
1746 .unwrap_or_default();
1747 (params, fd.lang)
1748 };
1749
1750 let file_rel = self.relative_path(file);
1751
1752 for (pos, _tracked) in &arg_positions {
1753 if let Some(param_name) = params.get(*pos) {
1754 hops.push(DataFlowHop {
1755 file: file_rel.clone(),
1756 symbol: callee_name.clone(),
1757 variable: param_name.clone(),
1758 line: get_symbol_meta(file, &callee_name).0,
1759 flow_type: "parameter".to_string(),
1760 approximate: false,
1761 });
1762
1763 self.trace_data_inner(
1765 file,
1766 &callee_name.clone(),
1767 param_name,
1768 max_depth,
1769 current_depth + 1,
1770 hops,
1771 depth_limited,
1772 visited,
1773 );
1774 }
1775 }
1776 } else {
1777 for (_pos, tracked) in &arg_positions {
1779 hops.push(DataFlowHop {
1780 file: self.relative_path(file),
1781 symbol: callee_name.clone(),
1782 variable: tracked.clone(),
1783 line: node.start_position().row as u32 + 1,
1784 flow_type: "parameter".to_string(),
1785 approximate: true,
1786 });
1787 }
1788 }
1789 }
1790 }
1791 }
1792
1793 fn read_source_line(&self, path: &Path, line: u32) -> Option<String> {
1795 let content = std::fs::read_to_string(path).ok()?;
1796 content
1797 .lines()
1798 .nth(line.saturating_sub(1) as usize)
1799 .map(|l| l.trim().to_string())
1800 }
1801
1802 fn collect_callers_recursive(
1804 &self,
1805 file: &Path,
1806 symbol: &str,
1807 max_depth: usize,
1808 current_depth: usize,
1809 visited: &mut HashSet<(PathBuf, String)>,
1810 result: &mut Vec<CallerSite>,
1811 ) {
1812 if current_depth >= max_depth {
1813 return;
1814 }
1815
1816 let canon = std::fs::canonicalize(file).unwrap_or_else(|_| file.to_path_buf());
1818 let key = (canon.clone(), symbol.to_string());
1819 if visited.contains(&key) {
1820 return; }
1822 visited.insert(key);
1823
1824 let reverse_index = match &self.reverse_index {
1825 Some(ri) => ri,
1826 None => return,
1827 };
1828
1829 let lookup_key = (canon, symbol.to_string());
1830 if let Some(sites) = reverse_index.get(&lookup_key) {
1831 for site in sites {
1832 result.push(site.clone());
1833 if current_depth + 1 < max_depth {
1835 self.collect_callers_recursive(
1836 &site.caller_file,
1837 &site.caller_symbol,
1838 max_depth,
1839 current_depth + 1,
1840 visited,
1841 result,
1842 );
1843 }
1844 }
1845 }
1846 }
1847
1848 pub fn invalidate_file(&mut self, path: &Path) {
1853 self.data.remove(path);
1855 if let Ok(canon) = self.canonicalize(path) {
1856 self.data.remove(&canon);
1857 }
1858 self.reverse_index = None;
1860 self.project_files = None;
1862 }
1863
1864 fn relative_path(&self, path: &Path) -> String {
1867 path.strip_prefix(&self.project_root)
1868 .unwrap_or(path)
1869 .display()
1870 .to_string()
1871 }
1872
1873 fn canonicalize(&self, path: &Path) -> Result<PathBuf, AftError> {
1875 let full_path = if path.is_relative() {
1877 self.project_root.join(path)
1878 } else {
1879 path.to_path_buf()
1880 };
1881
1882 Ok(std::fs::canonicalize(&full_path).unwrap_or(full_path))
1884 }
1885
1886 fn lookup_file_data(&self, path: &Path) -> Option<&FileCallData> {
1890 if let Some(fd) = self.data.get(path) {
1891 return Some(fd);
1892 }
1893 let canon = std::fs::canonicalize(path).ok()?;
1895 self.data.get(&canon).or_else(|| {
1896 self.data.iter().find_map(|(k, v)| {
1898 if std::fs::canonicalize(k).ok().as_ref() == Some(&canon) {
1899 Some(v)
1900 } else {
1901 None
1902 }
1903 })
1904 })
1905 }
1906}
1907
1908fn build_file_data(path: &Path) -> Result<FileCallData, AftError> {
1914 let lang = detect_language(path).ok_or_else(|| AftError::InvalidRequest {
1915 message: format!("unsupported file for call graph: {}", path.display()),
1916 })?;
1917
1918 let source = std::fs::read_to_string(path).map_err(|e| AftError::FileNotFound {
1919 path: format!("{}: {}", path.display(), e),
1920 })?;
1921
1922 let grammar = grammar_for(lang);
1923 let mut parser = Parser::new();
1924 parser
1925 .set_language(&grammar)
1926 .map_err(|e| AftError::ParseError {
1927 message: format!("grammar init failed for {:?}: {}", lang, e),
1928 })?;
1929
1930 let tree = parser
1931 .parse(&source, None)
1932 .ok_or_else(|| AftError::ParseError {
1933 message: format!("parse failed for {}", path.display()),
1934 })?;
1935
1936 let import_block = imports::parse_imports(&source, &tree, lang);
1938
1939 let symbols = list_symbols_from_tree(&source, &tree, lang, path);
1941
1942 let mut calls_by_symbol: HashMap<String, Vec<CallSite>> = HashMap::new();
1944 let root = tree.root_node();
1945
1946 for sym in &symbols {
1947 let byte_start = line_col_to_byte(&source, sym.start_line, sym.start_col);
1948 let byte_end = line_col_to_byte(&source, sym.end_line, sym.end_col);
1949
1950 let raw_calls = extract_calls_full(&source, root, byte_start, byte_end, lang);
1951
1952 let sites: Vec<CallSite> = raw_calls
1953 .into_iter()
1954 .filter(|(_, short, _)| *short != sym.name) .map(|(full, short, line)| CallSite {
1956 callee_name: short,
1957 full_callee: full,
1958 line,
1959 byte_start,
1960 byte_end,
1961 })
1962 .collect();
1963
1964 if !sites.is_empty() {
1965 calls_by_symbol.insert(sym.name.clone(), sites);
1966 }
1967 }
1968
1969 let exported_symbols: Vec<String> = symbols
1971 .iter()
1972 .filter(|s| s.exported)
1973 .map(|s| s.name.clone())
1974 .collect();
1975
1976 let symbol_metadata: HashMap<String, SymbolMeta> = symbols
1978 .iter()
1979 .map(|s| {
1980 (
1981 s.name.clone(),
1982 SymbolMeta {
1983 kind: s.kind.clone(),
1984 exported: s.exported,
1985 signature: s.signature.clone(),
1986 },
1987 )
1988 })
1989 .collect();
1990
1991 Ok(FileCallData {
1992 calls_by_symbol,
1993 exported_symbols,
1994 symbol_metadata,
1995 import_block,
1996 lang,
1997 })
1998}
1999
2000#[derive(Debug)]
2002#[allow(dead_code)]
2003struct SymbolInfo {
2004 name: String,
2005 kind: SymbolKind,
2006 start_line: u32,
2007 start_col: u32,
2008 end_line: u32,
2009 end_col: u32,
2010 exported: bool,
2011 signature: Option<String>,
2012}
2013
2014fn list_symbols_from_tree(
2017 _source: &str,
2018 _tree: &Tree,
2019 _lang: LangId,
2020 path: &Path,
2021) -> Vec<SymbolInfo> {
2022 let mut file_parser = crate::parser::FileParser::new();
2024 match file_parser.parse(path) {
2025 Ok(_) => {}
2026 Err(_) => return vec![],
2027 }
2028
2029 let provider = crate::parser::TreeSitterProvider::new();
2031 match provider.list_symbols(path) {
2032 Ok(symbols) => symbols
2033 .into_iter()
2034 .map(|s| SymbolInfo {
2035 name: s.name,
2036 kind: s.kind,
2037 start_line: s.range.start_line,
2038 start_col: s.range.start_col,
2039 end_line: s.range.end_line,
2040 end_col: s.range.end_col,
2041 exported: s.exported,
2042 signature: s.signature,
2043 })
2044 .collect(),
2045 Err(_) => vec![],
2046 }
2047}
2048
2049fn line_col_to_byte(source: &str, line: u32, col: u32) -> usize {
2051 let mut byte = 0;
2052 for (i, l) in source.lines().enumerate() {
2053 if i == line as usize {
2054 return byte + (col as usize).min(l.len());
2055 }
2056 byte += l.len() + 1;
2057 }
2058 source.len()
2059}
2060
2061fn get_symbol_meta(path: &Path, symbol_name: &str) -> (u32, Option<String>) {
2063 let provider = crate::parser::TreeSitterProvider::new();
2064 match provider.list_symbols(path) {
2065 Ok(symbols) => {
2066 for s in &symbols {
2067 if s.name == symbol_name {
2068 return (s.range.start_line + 1, s.signature.clone());
2069 }
2070 }
2071 (1, None)
2072 }
2073 Err(_) => (1, None),
2074 }
2075}
2076
2077fn node_text(node: tree_sitter::Node, source: &str) -> String {
2083 source[node.start_byte()..node.end_byte()].to_string()
2084}
2085
2086fn find_node_covering_range(
2088 root: tree_sitter::Node,
2089 start: usize,
2090 end: usize,
2091) -> Option<tree_sitter::Node> {
2092 let mut best = None;
2093 let mut cursor = root.walk();
2094
2095 fn walk_covering<'a>(
2096 cursor: &mut tree_sitter::TreeCursor<'a>,
2097 start: usize,
2098 end: usize,
2099 best: &mut Option<tree_sitter::Node<'a>>,
2100 ) {
2101 let node = cursor.node();
2102 if node.start_byte() <= start && node.end_byte() >= end {
2103 *best = Some(node);
2104 if cursor.goto_first_child() {
2105 loop {
2106 walk_covering(cursor, start, end, best);
2107 if !cursor.goto_next_sibling() {
2108 break;
2109 }
2110 }
2111 cursor.goto_parent();
2112 }
2113 }
2114 }
2115
2116 walk_covering(&mut cursor, start, end, &mut best);
2117 best
2118}
2119
2120fn find_child_by_kind<'a>(
2122 node: tree_sitter::Node<'a>,
2123 kind: &str,
2124) -> Option<tree_sitter::Node<'a>> {
2125 let mut cursor = node.walk();
2126 if cursor.goto_first_child() {
2127 loop {
2128 if cursor.node().kind() == kind {
2129 return Some(cursor.node());
2130 }
2131 if !cursor.goto_next_sibling() {
2132 break;
2133 }
2134 }
2135 }
2136 None
2137}
2138
2139fn extract_callee_names(node: tree_sitter::Node, source: &str) -> (Option<String>, Option<String>) {
2141 let callee = match node.child_by_field_name("function") {
2143 Some(c) => c,
2144 None => return (None, None),
2145 };
2146
2147 let full = node_text(callee, source);
2148 let short = if full.contains('.') {
2149 full.rsplit('.').next().unwrap_or(&full).to_string()
2150 } else {
2151 full.clone()
2152 };
2153
2154 (Some(full), Some(short))
2155}
2156
2157pub(crate) fn resolve_module_path(from_dir: &Path, module_path: &str) -> Option<PathBuf> {
2165 if !module_path.starts_with('.') {
2167 return None;
2168 }
2169
2170 let base = from_dir.join(module_path);
2171
2172 if base.is_file() {
2174 return Some(std::fs::canonicalize(&base).unwrap_or(base));
2175 }
2176
2177 let extensions = [".ts", ".tsx", ".js", ".jsx"];
2179 for ext in &extensions {
2180 let with_ext = base.with_extension(ext.trim_start_matches('.'));
2181 if with_ext.is_file() {
2182 return Some(std::fs::canonicalize(&with_ext).unwrap_or(with_ext));
2183 }
2184 }
2185
2186 if base.is_dir() {
2188 if let Some(index) = find_index_file(&base) {
2189 return Some(index);
2190 }
2191 }
2192
2193 None
2194}
2195
2196fn find_index_file(dir: &Path) -> Option<PathBuf> {
2198 let candidates = ["index.ts", "index.tsx", "index.js", "index.jsx"];
2199 for name in &candidates {
2200 let p = dir.join(name);
2201 if p.is_file() {
2202 return Some(std::fs::canonicalize(&p).unwrap_or(p));
2203 }
2204 }
2205 None
2206}
2207
2208fn resolve_aliased_import(
2211 local_name: &str,
2212 import_block: &ImportBlock,
2213 caller_dir: &Path,
2214) -> Option<(String, PathBuf)> {
2215 for imp in &import_block.imports {
2216 if let Some(original) = find_alias_original(&imp.raw_text, local_name) {
2219 if let Some(resolved_path) = resolve_module_path(caller_dir, &imp.module_path) {
2220 return Some((original, resolved_path));
2221 }
2222 }
2223 }
2224 None
2225}
2226
2227fn find_alias_original(raw_import: &str, local_name: &str) -> Option<String> {
2231 let search = format!(" as {}", local_name);
2234 if let Some(pos) = raw_import.find(&search) {
2235 let before = &raw_import[..pos];
2237 let original = before
2239 .rsplit(|c: char| c == '{' || c == ',' || c.is_whitespace())
2240 .find(|s| !s.is_empty())?;
2241 return Some(original.to_string());
2242 }
2243 None
2244}
2245
2246pub fn walk_project_files(root: &Path) -> impl Iterator<Item = PathBuf> {
2254 use ignore::WalkBuilder;
2255
2256 let walker = WalkBuilder::new(root)
2257 .hidden(true) .git_ignore(true) .git_global(true) .git_exclude(true) .filter_entry(|entry| {
2262 let name = entry.file_name().to_string_lossy();
2263 if entry.file_type().map_or(false, |ft| ft.is_dir()) {
2265 return !matches!(
2266 name.as_ref(),
2267 "node_modules" | "target" | "venv" | ".venv" | ".git" | "__pycache__"
2268 | ".tox" | "dist" | "build"
2269 );
2270 }
2271 true
2272 })
2273 .build();
2274
2275 walker
2276 .filter_map(|entry| entry.ok())
2277 .filter(|entry| entry.file_type().map_or(false, |ft| ft.is_file()))
2278 .filter(|entry| detect_language(entry.path()).is_some())
2279 .map(|entry| entry.into_path())
2280}
2281
2282#[cfg(test)]
2287mod tests {
2288 use super::*;
2289 use std::fs;
2290 use tempfile::TempDir;
2291
2292 fn setup_ts_project() -> TempDir {
2294 let dir = TempDir::new().unwrap();
2295
2296 fs::write(
2298 dir.path().join("main.ts"),
2299 r#"import { helper, compute } from './utils';
2300import * as math from './math';
2301
2302export function main() {
2303 const a = helper(1);
2304 const b = compute(a, 2);
2305 const c = math.add(a, b);
2306 return c;
2307}
2308"#,
2309 )
2310 .unwrap();
2311
2312 fs::write(
2314 dir.path().join("utils.ts"),
2315 r#"import { double } from './helpers';
2316
2317export function helper(x: number): number {
2318 return double(x);
2319}
2320
2321export function compute(a: number, b: number): number {
2322 return a + b;
2323}
2324"#,
2325 )
2326 .unwrap();
2327
2328 fs::write(
2330 dir.path().join("helpers.ts"),
2331 r#"export function double(x: number): number {
2332 return x * 2;
2333}
2334
2335export function triple(x: number): number {
2336 return x * 3;
2337}
2338"#,
2339 )
2340 .unwrap();
2341
2342 fs::write(
2344 dir.path().join("math.ts"),
2345 r#"export function add(a: number, b: number): number {
2346 return a + b;
2347}
2348
2349export function subtract(a: number, b: number): number {
2350 return a - b;
2351}
2352"#,
2353 )
2354 .unwrap();
2355
2356 dir
2357 }
2358
2359 fn setup_alias_project() -> TempDir {
2361 let dir = TempDir::new().unwrap();
2362
2363 fs::write(
2364 dir.path().join("main.ts"),
2365 r#"import { helper as h } from './utils';
2366
2367export function main() {
2368 return h(42);
2369}
2370"#,
2371 )
2372 .unwrap();
2373
2374 fs::write(
2375 dir.path().join("utils.ts"),
2376 r#"export function helper(x: number): number {
2377 return x + 1;
2378}
2379"#,
2380 )
2381 .unwrap();
2382
2383 dir
2384 }
2385
2386 fn setup_cycle_project() -> TempDir {
2388 let dir = TempDir::new().unwrap();
2389
2390 fs::write(
2391 dir.path().join("a.ts"),
2392 r#"import { funcB } from './b';
2393
2394export function funcA() {
2395 return funcB();
2396}
2397"#,
2398 )
2399 .unwrap();
2400
2401 fs::write(
2402 dir.path().join("b.ts"),
2403 r#"import { funcA } from './a';
2404
2405export function funcB() {
2406 return funcA();
2407}
2408"#,
2409 )
2410 .unwrap();
2411
2412 dir
2413 }
2414
2415 #[test]
2418 fn callgraph_single_file_call_extraction() {
2419 let dir = setup_ts_project();
2420 let mut graph = CallGraph::new(dir.path().to_path_buf());
2421
2422 let file_data = graph.build_file(&dir.path().join("main.ts")).unwrap();
2423 let main_calls = &file_data.calls_by_symbol["main"];
2424
2425 let callee_names: Vec<&str> = main_calls.iter().map(|c| c.callee_name.as_str()).collect();
2426 assert!(
2427 callee_names.contains(&"helper"),
2428 "main should call helper, got: {:?}",
2429 callee_names
2430 );
2431 assert!(
2432 callee_names.contains(&"compute"),
2433 "main should call compute, got: {:?}",
2434 callee_names
2435 );
2436 assert!(
2437 callee_names.contains(&"add"),
2438 "main should call math.add (short name: add), got: {:?}",
2439 callee_names
2440 );
2441 }
2442
2443 #[test]
2444 fn callgraph_file_data_has_exports() {
2445 let dir = setup_ts_project();
2446 let mut graph = CallGraph::new(dir.path().to_path_buf());
2447
2448 let file_data = graph.build_file(&dir.path().join("utils.ts")).unwrap();
2449 assert!(
2450 file_data.exported_symbols.contains(&"helper".to_string()),
2451 "utils.ts should export helper, got: {:?}",
2452 file_data.exported_symbols
2453 );
2454 assert!(
2455 file_data.exported_symbols.contains(&"compute".to_string()),
2456 "utils.ts should export compute, got: {:?}",
2457 file_data.exported_symbols
2458 );
2459 }
2460
2461 #[test]
2464 fn callgraph_resolve_direct_import() {
2465 let dir = setup_ts_project();
2466 let mut graph = CallGraph::new(dir.path().to_path_buf());
2467
2468 let main_path = dir.path().join("main.ts");
2469 let file_data = graph.build_file(&main_path).unwrap();
2470 let import_block = file_data.import_block.clone();
2471
2472 let edge = graph.resolve_cross_file_edge("helper", "helper", &main_path, &import_block);
2473 match edge {
2474 EdgeResolution::Resolved { file, symbol } => {
2475 assert!(
2476 file.ends_with("utils.ts"),
2477 "helper should resolve to utils.ts, got: {:?}",
2478 file
2479 );
2480 assert_eq!(symbol, "helper");
2481 }
2482 EdgeResolution::Unresolved { callee_name } => {
2483 panic!("Expected resolved, got unresolved: {}", callee_name);
2484 }
2485 }
2486 }
2487
2488 #[test]
2489 fn callgraph_resolve_namespace_import() {
2490 let dir = setup_ts_project();
2491 let mut graph = CallGraph::new(dir.path().to_path_buf());
2492
2493 let main_path = dir.path().join("main.ts");
2494 let file_data = graph.build_file(&main_path).unwrap();
2495 let import_block = file_data.import_block.clone();
2496
2497 let edge = graph.resolve_cross_file_edge("math.add", "add", &main_path, &import_block);
2498 match edge {
2499 EdgeResolution::Resolved { file, symbol } => {
2500 assert!(
2501 file.ends_with("math.ts"),
2502 "math.add should resolve to math.ts, got: {:?}",
2503 file
2504 );
2505 assert_eq!(symbol, "add");
2506 }
2507 EdgeResolution::Unresolved { callee_name } => {
2508 panic!("Expected resolved, got unresolved: {}", callee_name);
2509 }
2510 }
2511 }
2512
2513 #[test]
2514 fn callgraph_resolve_aliased_import() {
2515 let dir = setup_alias_project();
2516 let mut graph = CallGraph::new(dir.path().to_path_buf());
2517
2518 let main_path = dir.path().join("main.ts");
2519 let file_data = graph.build_file(&main_path).unwrap();
2520 let import_block = file_data.import_block.clone();
2521
2522 let edge = graph.resolve_cross_file_edge("h", "h", &main_path, &import_block);
2523 match edge {
2524 EdgeResolution::Resolved { file, symbol } => {
2525 assert!(
2526 file.ends_with("utils.ts"),
2527 "h (alias for helper) should resolve to utils.ts, got: {:?}",
2528 file
2529 );
2530 assert_eq!(symbol, "helper");
2531 }
2532 EdgeResolution::Unresolved { callee_name } => {
2533 panic!("Expected resolved, got unresolved: {}", callee_name);
2534 }
2535 }
2536 }
2537
2538 #[test]
2539 fn callgraph_unresolved_edge_marked() {
2540 let dir = setup_ts_project();
2541 let mut graph = CallGraph::new(dir.path().to_path_buf());
2542
2543 let main_path = dir.path().join("main.ts");
2544 let file_data = graph.build_file(&main_path).unwrap();
2545 let import_block = file_data.import_block.clone();
2546
2547 let edge =
2548 graph.resolve_cross_file_edge("unknownFunc", "unknownFunc", &main_path, &import_block);
2549 assert_eq!(
2550 edge,
2551 EdgeResolution::Unresolved {
2552 callee_name: "unknownFunc".to_string()
2553 },
2554 "Unknown callee should be unresolved"
2555 );
2556 }
2557
2558 #[test]
2561 fn callgraph_cycle_detection_stops() {
2562 let dir = setup_cycle_project();
2563 let mut graph = CallGraph::new(dir.path().to_path_buf());
2564
2565 let tree = graph
2567 .forward_tree(&dir.path().join("a.ts"), "funcA", 10)
2568 .unwrap();
2569
2570 assert_eq!(tree.name, "funcA");
2571 assert!(tree.resolved);
2572
2573 fn count_depth(node: &CallTreeNode) -> usize {
2576 if node.children.is_empty() {
2577 1
2578 } else {
2579 1 + node
2580 .children
2581 .iter()
2582 .map(|c| count_depth(c))
2583 .max()
2584 .unwrap_or(0)
2585 }
2586 }
2587
2588 let depth = count_depth(&tree);
2589 assert!(
2590 depth <= 4,
2591 "Cycle should be detected and bounded, depth was: {}",
2592 depth
2593 );
2594 }
2595
2596 #[test]
2599 fn callgraph_depth_limit_truncates() {
2600 let dir = setup_ts_project();
2601 let mut graph = CallGraph::new(dir.path().to_path_buf());
2602
2603 let tree = graph
2606 .forward_tree(&dir.path().join("main.ts"), "main", 1)
2607 .unwrap();
2608
2609 assert_eq!(tree.name, "main");
2610
2611 for child in &tree.children {
2613 assert!(
2614 child.children.is_empty(),
2615 "At depth 1, child '{}' should have no children, got {:?}",
2616 child.name,
2617 child.children.len()
2618 );
2619 }
2620 }
2621
2622 #[test]
2623 fn callgraph_depth_zero_no_children() {
2624 let dir = setup_ts_project();
2625 let mut graph = CallGraph::new(dir.path().to_path_buf());
2626
2627 let tree = graph
2628 .forward_tree(&dir.path().join("main.ts"), "main", 0)
2629 .unwrap();
2630
2631 assert_eq!(tree.name, "main");
2632 assert!(
2633 tree.children.is_empty(),
2634 "At depth 0, should have no children"
2635 );
2636 }
2637
2638 #[test]
2641 fn callgraph_forward_tree_cross_file() {
2642 let dir = setup_ts_project();
2643 let mut graph = CallGraph::new(dir.path().to_path_buf());
2644
2645 let tree = graph
2647 .forward_tree(&dir.path().join("main.ts"), "main", 5)
2648 .unwrap();
2649
2650 assert_eq!(tree.name, "main");
2651 assert!(tree.resolved);
2652
2653 let helper_child = tree.children.iter().find(|c| c.name == "helper");
2655 assert!(
2656 helper_child.is_some(),
2657 "main should have helper as child, children: {:?}",
2658 tree.children.iter().map(|c| &c.name).collect::<Vec<_>>()
2659 );
2660
2661 let helper = helper_child.unwrap();
2662 assert!(
2663 helper.file.ends_with("utils.ts") || helper.file == "utils.ts",
2664 "helper should be in utils.ts, got: {}",
2665 helper.file
2666 );
2667
2668 let double_child = helper.children.iter().find(|c| c.name == "double");
2670 assert!(
2671 double_child.is_some(),
2672 "helper should call double, children: {:?}",
2673 helper.children.iter().map(|c| &c.name).collect::<Vec<_>>()
2674 );
2675
2676 let double = double_child.unwrap();
2677 assert!(
2678 double.file.ends_with("helpers.ts") || double.file == "helpers.ts",
2679 "double should be in helpers.ts, got: {}",
2680 double.file
2681 );
2682 }
2683
2684 #[test]
2687 fn callgraph_walker_excludes_gitignored() {
2688 let dir = TempDir::new().unwrap();
2689
2690 fs::write(dir.path().join(".gitignore"), "ignored_dir/\n").unwrap();
2692
2693 fs::write(dir.path().join("main.ts"), "export function main() {}").unwrap();
2695 fs::create_dir(dir.path().join("ignored_dir")).unwrap();
2696 fs::write(
2697 dir.path().join("ignored_dir").join("secret.ts"),
2698 "export function secret() {}",
2699 )
2700 .unwrap();
2701
2702 fs::create_dir(dir.path().join("node_modules")).unwrap();
2704 fs::write(
2705 dir.path().join("node_modules").join("dep.ts"),
2706 "export function dep() {}",
2707 )
2708 .unwrap();
2709
2710 std::process::Command::new("git")
2712 .args(["init"])
2713 .current_dir(dir.path())
2714 .output()
2715 .unwrap();
2716
2717 let files: Vec<PathBuf> = walk_project_files(dir.path()).collect();
2718 let file_names: Vec<String> = files
2719 .iter()
2720 .map(|f| f.file_name().unwrap().to_string_lossy().to_string())
2721 .collect();
2722
2723 assert!(
2724 file_names.contains(&"main.ts".to_string()),
2725 "Should include main.ts, got: {:?}",
2726 file_names
2727 );
2728 assert!(
2729 !file_names.contains(&"secret.ts".to_string()),
2730 "Should exclude gitignored secret.ts, got: {:?}",
2731 file_names
2732 );
2733 assert!(
2734 !file_names.contains(&"dep.ts".to_string()),
2735 "Should exclude node_modules, got: {:?}",
2736 file_names
2737 );
2738 }
2739
2740 #[test]
2741 fn callgraph_walker_only_source_files() {
2742 let dir = TempDir::new().unwrap();
2743
2744 fs::write(dir.path().join("main.ts"), "export function main() {}").unwrap();
2745 fs::write(dir.path().join("readme.md"), "# Hello").unwrap();
2746 fs::write(dir.path().join("data.json"), "{}").unwrap();
2747
2748 let files: Vec<PathBuf> = walk_project_files(dir.path()).collect();
2749 let file_names: Vec<String> = files
2750 .iter()
2751 .map(|f| f.file_name().unwrap().to_string_lossy().to_string())
2752 .collect();
2753
2754 assert!(file_names.contains(&"main.ts".to_string()));
2755 assert!(
2756 file_names.contains(&"readme.md".to_string()),
2757 "Markdown is now a supported source language"
2758 );
2759 assert!(
2760 !file_names.contains(&"data.json".to_string()),
2761 "Should not include non-source files"
2762 );
2763 }
2764
2765 #[test]
2768 fn callgraph_find_alias_original_simple() {
2769 let raw = "import { foo as bar } from './utils';";
2770 assert_eq!(find_alias_original(raw, "bar"), Some("foo".to_string()));
2771 }
2772
2773 #[test]
2774 fn callgraph_find_alias_original_multiple() {
2775 let raw = "import { foo as bar, baz as qux } from './utils';";
2776 assert_eq!(find_alias_original(raw, "bar"), Some("foo".to_string()));
2777 assert_eq!(find_alias_original(raw, "qux"), Some("baz".to_string()));
2778 }
2779
2780 #[test]
2781 fn callgraph_find_alias_no_match() {
2782 let raw = "import { foo } from './utils';";
2783 assert_eq!(find_alias_original(raw, "foo"), None);
2784 }
2785
2786 #[test]
2789 fn callgraph_callers_of_direct() {
2790 let dir = setup_ts_project();
2791 let mut graph = CallGraph::new(dir.path().to_path_buf());
2792
2793 let result = graph
2795 .callers_of(&dir.path().join("helpers.ts"), "double", 1)
2796 .unwrap();
2797
2798 assert_eq!(result.symbol, "double");
2799 assert!(result.total_callers > 0, "double should have callers");
2800 assert!(result.scanned_files > 0, "should have scanned files");
2801
2802 let utils_group = result.callers.iter().find(|g| g.file.contains("utils.ts"));
2804 assert!(
2805 utils_group.is_some(),
2806 "double should be called from utils.ts, groups: {:?}",
2807 result.callers.iter().map(|g| &g.file).collect::<Vec<_>>()
2808 );
2809
2810 let group = utils_group.unwrap();
2811 let helper_caller = group.callers.iter().find(|c| c.symbol == "helper");
2812 assert!(
2813 helper_caller.is_some(),
2814 "double should be called by helper, callers: {:?}",
2815 group.callers.iter().map(|c| &c.symbol).collect::<Vec<_>>()
2816 );
2817 }
2818
2819 #[test]
2820 fn callgraph_callers_of_no_callers() {
2821 let dir = setup_ts_project();
2822 let mut graph = CallGraph::new(dir.path().to_path_buf());
2823
2824 let result = graph
2826 .callers_of(&dir.path().join("main.ts"), "main", 1)
2827 .unwrap();
2828
2829 assert_eq!(result.symbol, "main");
2830 assert_eq!(result.total_callers, 0, "main should have no callers");
2831 assert!(result.callers.is_empty());
2832 }
2833
2834 #[test]
2835 fn callgraph_callers_recursive_depth() {
2836 let dir = setup_ts_project();
2837 let mut graph = CallGraph::new(dir.path().to_path_buf());
2838
2839 let result = graph
2843 .callers_of(&dir.path().join("helpers.ts"), "double", 2)
2844 .unwrap();
2845
2846 assert!(
2847 result.total_callers >= 2,
2848 "with depth 2, double should have >= 2 callers (direct + transitive), got {}",
2849 result.total_callers
2850 );
2851
2852 let main_group = result.callers.iter().find(|g| g.file.contains("main.ts"));
2854 assert!(
2855 main_group.is_some(),
2856 "recursive callers should include main.ts, groups: {:?}",
2857 result.callers.iter().map(|g| &g.file).collect::<Vec<_>>()
2858 );
2859 }
2860
2861 #[test]
2862 fn callgraph_invalidate_file_clears_reverse_index() {
2863 let dir = setup_ts_project();
2864 let mut graph = CallGraph::new(dir.path().to_path_buf());
2865
2866 let _ = graph
2868 .callers_of(&dir.path().join("helpers.ts"), "double", 1)
2869 .unwrap();
2870 assert!(
2871 graph.reverse_index.is_some(),
2872 "reverse index should be built"
2873 );
2874
2875 graph.invalidate_file(&dir.path().join("utils.ts"));
2877
2878 assert!(
2880 graph.reverse_index.is_none(),
2881 "invalidate_file should clear reverse index"
2882 );
2883 let canon = std::fs::canonicalize(dir.path().join("utils.ts")).unwrap();
2885 assert!(
2886 !graph.data.contains_key(&canon),
2887 "invalidate_file should remove file from data cache"
2888 );
2889 assert!(
2891 graph.project_files.is_none(),
2892 "invalidate_file should clear project_files"
2893 );
2894 }
2895
2896 #[test]
2899 fn is_entry_point_exported_function() {
2900 assert!(is_entry_point(
2901 "handleRequest",
2902 &SymbolKind::Function,
2903 true,
2904 LangId::TypeScript
2905 ));
2906 }
2907
2908 #[test]
2909 fn is_entry_point_exported_method_is_not_entry() {
2910 assert!(!is_entry_point(
2912 "handleRequest",
2913 &SymbolKind::Method,
2914 true,
2915 LangId::TypeScript
2916 ));
2917 }
2918
2919 #[test]
2920 fn is_entry_point_main_init_patterns() {
2921 for name in &["main", "Main", "MAIN", "init", "setup", "bootstrap", "run"] {
2922 assert!(
2923 is_entry_point(name, &SymbolKind::Function, false, LangId::TypeScript),
2924 "{} should be an entry point",
2925 name
2926 );
2927 }
2928 }
2929
2930 #[test]
2931 fn is_entry_point_test_patterns_ts() {
2932 assert!(is_entry_point(
2933 "describe",
2934 &SymbolKind::Function,
2935 false,
2936 LangId::TypeScript
2937 ));
2938 assert!(is_entry_point(
2939 "it",
2940 &SymbolKind::Function,
2941 false,
2942 LangId::TypeScript
2943 ));
2944 assert!(is_entry_point(
2945 "test",
2946 &SymbolKind::Function,
2947 false,
2948 LangId::TypeScript
2949 ));
2950 assert!(is_entry_point(
2951 "testValidation",
2952 &SymbolKind::Function,
2953 false,
2954 LangId::TypeScript
2955 ));
2956 assert!(is_entry_point(
2957 "specHelper",
2958 &SymbolKind::Function,
2959 false,
2960 LangId::TypeScript
2961 ));
2962 }
2963
2964 #[test]
2965 fn is_entry_point_test_patterns_python() {
2966 assert!(is_entry_point(
2967 "test_login",
2968 &SymbolKind::Function,
2969 false,
2970 LangId::Python
2971 ));
2972 assert!(is_entry_point(
2973 "setUp",
2974 &SymbolKind::Function,
2975 false,
2976 LangId::Python
2977 ));
2978 assert!(is_entry_point(
2979 "tearDown",
2980 &SymbolKind::Function,
2981 false,
2982 LangId::Python
2983 ));
2984 assert!(!is_entry_point(
2986 "testSomething",
2987 &SymbolKind::Function,
2988 false,
2989 LangId::Python
2990 ));
2991 }
2992
2993 #[test]
2994 fn is_entry_point_test_patterns_rust() {
2995 assert!(is_entry_point(
2996 "test_parse",
2997 &SymbolKind::Function,
2998 false,
2999 LangId::Rust
3000 ));
3001 assert!(!is_entry_point(
3002 "TestSomething",
3003 &SymbolKind::Function,
3004 false,
3005 LangId::Rust
3006 ));
3007 }
3008
3009 #[test]
3010 fn is_entry_point_test_patterns_go() {
3011 assert!(is_entry_point(
3012 "TestParsing",
3013 &SymbolKind::Function,
3014 false,
3015 LangId::Go
3016 ));
3017 assert!(!is_entry_point(
3019 "testParsing",
3020 &SymbolKind::Function,
3021 false,
3022 LangId::Go
3023 ));
3024 }
3025
3026 #[test]
3027 fn is_entry_point_non_exported_non_main_is_not_entry() {
3028 assert!(!is_entry_point(
3029 "helperUtil",
3030 &SymbolKind::Function,
3031 false,
3032 LangId::TypeScript
3033 ));
3034 }
3035
3036 #[test]
3039 fn callgraph_symbol_metadata_populated() {
3040 let dir = setup_ts_project();
3041 let mut graph = CallGraph::new(dir.path().to_path_buf());
3042
3043 let file_data = graph.build_file(&dir.path().join("utils.ts")).unwrap();
3044 assert!(
3045 file_data.symbol_metadata.contains_key("helper"),
3046 "symbol_metadata should contain helper"
3047 );
3048 let meta = &file_data.symbol_metadata["helper"];
3049 assert_eq!(meta.kind, SymbolKind::Function);
3050 assert!(meta.exported, "helper should be exported");
3051 }
3052
3053 fn setup_trace_project() -> TempDir {
3069 let dir = TempDir::new().unwrap();
3070
3071 fs::write(
3072 dir.path().join("main.ts"),
3073 r#"import { processData } from './utils';
3074
3075export function main() {
3076 const result = processData("hello");
3077 return result;
3078}
3079"#,
3080 )
3081 .unwrap();
3082
3083 fs::write(
3084 dir.path().join("service.ts"),
3085 r#"import { processData } from './utils';
3086
3087export function handleRequest(input: string): string {
3088 return processData(input);
3089}
3090"#,
3091 )
3092 .unwrap();
3093
3094 fs::write(
3095 dir.path().join("utils.ts"),
3096 r#"import { validate } from './helpers';
3097
3098export function processData(input: string): string {
3099 const valid = validate(input);
3100 if (!valid) {
3101 throw new Error("invalid input");
3102 }
3103 return input.toUpperCase();
3104}
3105"#,
3106 )
3107 .unwrap();
3108
3109 fs::write(
3110 dir.path().join("helpers.ts"),
3111 r#"export function validate(input: string): boolean {
3112 return checkFormat(input);
3113}
3114
3115function checkFormat(input: string): boolean {
3116 return input.length > 0 && /^[a-zA-Z]+$/.test(input);
3117}
3118"#,
3119 )
3120 .unwrap();
3121
3122 fs::write(
3123 dir.path().join("test_helpers.ts"),
3124 r#"import { validate } from './helpers';
3125
3126function testValidation() {
3127 const result = validate("hello");
3128 console.log(result);
3129}
3130"#,
3131 )
3132 .unwrap();
3133
3134 std::process::Command::new("git")
3136 .args(["init"])
3137 .current_dir(dir.path())
3138 .output()
3139 .unwrap();
3140
3141 dir
3142 }
3143
3144 #[test]
3145 fn trace_to_multi_path() {
3146 let dir = setup_trace_project();
3147 let mut graph = CallGraph::new(dir.path().to_path_buf());
3148
3149 let result = graph
3150 .trace_to(&dir.path().join("helpers.ts"), "checkFormat", 10)
3151 .unwrap();
3152
3153 assert_eq!(result.target_symbol, "checkFormat");
3154 assert!(
3155 result.total_paths >= 2,
3156 "checkFormat should have at least 2 paths, got {} (paths: {:?})",
3157 result.total_paths,
3158 result
3159 .paths
3160 .iter()
3161 .map(|p| p.hops.iter().map(|h| h.symbol.as_str()).collect::<Vec<_>>())
3162 .collect::<Vec<_>>()
3163 );
3164
3165 for path in &result.paths {
3167 assert!(
3168 path.hops.first().unwrap().is_entry_point,
3169 "First hop should be an entry point, got: {}",
3170 path.hops.first().unwrap().symbol
3171 );
3172 assert_eq!(
3173 path.hops.last().unwrap().symbol,
3174 "checkFormat",
3175 "Last hop should be checkFormat"
3176 );
3177 }
3178
3179 assert!(
3181 result.entry_points_found >= 2,
3182 "should find at least 2 entry points, got {}",
3183 result.entry_points_found
3184 );
3185 }
3186
3187 #[test]
3188 fn trace_to_single_path() {
3189 let dir = setup_trace_project();
3190 let mut graph = CallGraph::new(dir.path().to_path_buf());
3191
3192 let result = graph
3196 .trace_to(&dir.path().join("helpers.ts"), "validate", 10)
3197 .unwrap();
3198
3199 assert_eq!(result.target_symbol, "validate");
3200 assert!(
3201 result.total_paths >= 2,
3202 "validate should have at least 2 paths, got {}",
3203 result.total_paths
3204 );
3205 }
3206
3207 #[test]
3208 fn trace_to_cycle_detection() {
3209 let dir = setup_cycle_project();
3210 let mut graph = CallGraph::new(dir.path().to_path_buf());
3211
3212 let result = graph
3214 .trace_to(&dir.path().join("a.ts"), "funcA", 10)
3215 .unwrap();
3216
3217 assert_eq!(result.target_symbol, "funcA");
3219 }
3220
3221 #[test]
3222 fn trace_to_depth_limit() {
3223 let dir = setup_trace_project();
3224 let mut graph = CallGraph::new(dir.path().to_path_buf());
3225
3226 let result = graph
3228 .trace_to(&dir.path().join("helpers.ts"), "checkFormat", 1)
3229 .unwrap();
3230
3231 assert_eq!(result.target_symbol, "checkFormat");
3235
3236 let deep_result = graph
3238 .trace_to(&dir.path().join("helpers.ts"), "checkFormat", 10)
3239 .unwrap();
3240
3241 assert!(
3242 result.total_paths <= deep_result.total_paths,
3243 "shallow trace should find <= paths compared to deep: {} vs {}",
3244 result.total_paths,
3245 deep_result.total_paths
3246 );
3247 }
3248
3249 #[test]
3250 fn trace_to_entry_point_target() {
3251 let dir = setup_trace_project();
3252 let mut graph = CallGraph::new(dir.path().to_path_buf());
3253
3254 let result = graph
3256 .trace_to(&dir.path().join("main.ts"), "main", 10)
3257 .unwrap();
3258
3259 assert_eq!(result.target_symbol, "main");
3260 assert!(
3261 result.total_paths >= 1,
3262 "main should have at least 1 path (itself), got {}",
3263 result.total_paths
3264 );
3265 let trivial = result.paths.iter().find(|p| p.hops.len() == 1);
3267 assert!(
3268 trivial.is_some(),
3269 "should have a trivial path with just the entry point itself"
3270 );
3271 }
3272
3273 #[test]
3276 fn extract_parameters_typescript() {
3277 let params = extract_parameters(
3278 "function processData(input: string, count: number): void",
3279 LangId::TypeScript,
3280 );
3281 assert_eq!(params, vec!["input", "count"]);
3282 }
3283
3284 #[test]
3285 fn extract_parameters_typescript_optional() {
3286 let params = extract_parameters(
3287 "function fetch(url: string, options?: RequestInit): Promise<Response>",
3288 LangId::TypeScript,
3289 );
3290 assert_eq!(params, vec!["url", "options"]);
3291 }
3292
3293 #[test]
3294 fn extract_parameters_typescript_defaults() {
3295 let params = extract_parameters(
3296 "function greet(name: string, greeting: string = \"hello\"): string",
3297 LangId::TypeScript,
3298 );
3299 assert_eq!(params, vec!["name", "greeting"]);
3300 }
3301
3302 #[test]
3303 fn extract_parameters_typescript_rest() {
3304 let params = extract_parameters(
3305 "function sum(...numbers: number[]): number",
3306 LangId::TypeScript,
3307 );
3308 assert_eq!(params, vec!["numbers"]);
3309 }
3310
3311 #[test]
3312 fn extract_parameters_python_self_skipped() {
3313 let params = extract_parameters(
3314 "def process(self, data: str, count: int) -> bool",
3315 LangId::Python,
3316 );
3317 assert_eq!(params, vec!["data", "count"]);
3318 }
3319
3320 #[test]
3321 fn extract_parameters_python_no_self() {
3322 let params = extract_parameters("def validate(input: str) -> bool", LangId::Python);
3323 assert_eq!(params, vec!["input"]);
3324 }
3325
3326 #[test]
3327 fn extract_parameters_python_star_args() {
3328 let params = extract_parameters("def func(*args, **kwargs)", LangId::Python);
3329 assert_eq!(params, vec!["args", "kwargs"]);
3330 }
3331
3332 #[test]
3333 fn extract_parameters_rust_self_skipped() {
3334 let params = extract_parameters(
3335 "fn process(&self, data: &str, count: usize) -> bool",
3336 LangId::Rust,
3337 );
3338 assert_eq!(params, vec!["data", "count"]);
3339 }
3340
3341 #[test]
3342 fn extract_parameters_rust_mut_self_skipped() {
3343 let params = extract_parameters("fn update(&mut self, value: i32)", LangId::Rust);
3344 assert_eq!(params, vec!["value"]);
3345 }
3346
3347 #[test]
3348 fn extract_parameters_rust_no_self() {
3349 let params = extract_parameters("fn validate(input: &str) -> bool", LangId::Rust);
3350 assert_eq!(params, vec!["input"]);
3351 }
3352
3353 #[test]
3354 fn extract_parameters_rust_mut_param() {
3355 let params = extract_parameters("fn process(mut buf: Vec<u8>, len: usize)", LangId::Rust);
3356 assert_eq!(params, vec!["buf", "len"]);
3357 }
3358
3359 #[test]
3360 fn extract_parameters_go() {
3361 let params = extract_parameters(
3362 "func ProcessData(input string, count int) error",
3363 LangId::Go,
3364 );
3365 assert_eq!(params, vec!["input", "count"]);
3366 }
3367
3368 #[test]
3369 fn extract_parameters_empty() {
3370 let params = extract_parameters("function noArgs(): void", LangId::TypeScript);
3371 assert!(
3372 params.is_empty(),
3373 "no-arg function should return empty params"
3374 );
3375 }
3376
3377 #[test]
3378 fn extract_parameters_no_parens() {
3379 let params = extract_parameters("const x = 42", LangId::TypeScript);
3380 assert!(params.is_empty(), "no parens should return empty params");
3381 }
3382
3383 #[test]
3384 fn extract_parameters_javascript() {
3385 let params = extract_parameters("function handleClick(event, target)", LangId::JavaScript);
3386 assert_eq!(params, vec!["event", "target"]);
3387 }
3388}