Skip to main content

aft/
callgraph.rs

1//! Call graph engine: cross-file call resolution and forward traversal.
2//!
3//! Builds a lazy, worktree-scoped call graph that resolves calls across files
4//! using import chains. Supports depth-limited forward traversal with cycle
5//! detection.
6
7use 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// ---------------------------------------------------------------------------
21// Core types
22// ---------------------------------------------------------------------------
23
24/// A single call site within a function body.
25#[derive(Debug, Clone)]
26pub struct CallSite {
27    /// The short callee name (last segment, e.g. "foo" for `utils.foo()`).
28    pub callee_name: String,
29    /// The full callee expression (e.g. "utils.foo" for `utils.foo()`).
30    pub full_callee: String,
31    /// 1-based line number of the call.
32    pub line: u32,
33    /// Byte range of the call expression in the source.
34    pub byte_start: usize,
35    pub byte_end: usize,
36}
37
38/// Per-symbol metadata for entry point detection (avoids re-parsing).
39#[derive(Debug, Clone, Serialize)]
40pub struct SymbolMeta {
41    /// The kind of symbol (function, class, method, etc).
42    pub kind: SymbolKind,
43    /// Whether this symbol is exported.
44    pub exported: bool,
45    /// Function/method signature if available.
46    #[serde(skip_serializing_if = "Option::is_none")]
47    pub signature: Option<String>,
48}
49
50/// Per-file call data: call sites grouped by containing symbol, plus
51/// exported symbol names and parsed imports.
52#[derive(Debug, Clone)]
53pub struct FileCallData {
54    /// Map from symbol name → list of call sites within that symbol's body.
55    pub calls_by_symbol: HashMap<String, Vec<CallSite>>,
56    /// Names of exported symbols in this file.
57    pub exported_symbols: Vec<String>,
58    /// Per-symbol metadata (kind, exported, signature).
59    pub symbol_metadata: HashMap<String, SymbolMeta>,
60    /// Parsed import block for cross-file resolution.
61    pub import_block: ImportBlock,
62    /// Language of the file.
63    pub lang: LangId,
64}
65
66/// Result of resolving a cross-file call edge.
67#[derive(Debug, Clone, PartialEq, Eq)]
68pub enum EdgeResolution {
69    /// Successfully resolved to a specific file and symbol.
70    Resolved { file: PathBuf, symbol: String },
71    /// Could not resolve — callee name preserved for diagnostics.
72    Unresolved { callee_name: String },
73}
74
75/// A single caller site: who calls a given symbol and from where.
76#[derive(Debug, Clone, Serialize)]
77pub struct CallerSite {
78    /// File containing the caller.
79    pub caller_file: PathBuf,
80    /// Symbol that makes the call.
81    pub caller_symbol: String,
82    /// 1-based line number of the call.
83    pub line: u32,
84    /// 0-based column (byte start within file, kept for future use).
85    pub col: u32,
86    /// Whether the edge was resolved via import chain.
87    pub resolved: bool,
88}
89
90/// A group of callers from a single file.
91#[derive(Debug, Clone, Serialize)]
92pub struct CallerGroup {
93    /// File path (relative to project root).
94    pub file: String,
95    /// Individual call sites in this file.
96    pub callers: Vec<CallerEntry>,
97}
98
99/// A single caller entry within a CallerGroup.
100#[derive(Debug, Clone, Serialize)]
101pub struct CallerEntry {
102    pub symbol: String,
103    /// 1-based line number of the call.
104    pub line: u32,
105}
106
107/// Result of a `callers_of` query.
108#[derive(Debug, Clone, Serialize)]
109pub struct CallersResult {
110    /// Target symbol queried.
111    pub symbol: String,
112    /// Target file queried.
113    pub file: String,
114    /// Caller groups, one per calling file.
115    pub callers: Vec<CallerGroup>,
116    /// Total number of call sites found.
117    pub total_callers: usize,
118    /// Number of files scanned to build the reverse index.
119    pub scanned_files: usize,
120}
121
122/// A node in the forward call tree.
123#[derive(Debug, Clone, Serialize)]
124pub struct CallTreeNode {
125    /// Symbol name.
126    pub name: String,
127    /// File path (relative to project root when possible).
128    pub file: String,
129    /// 1-based line number.
130    pub line: u32,
131    /// Function signature if available.
132    #[serde(skip_serializing_if = "Option::is_none")]
133    pub signature: Option<String>,
134    /// Whether this edge was resolved cross-file.
135    pub resolved: bool,
136    /// Child calls (recursive).
137    pub children: Vec<CallTreeNode>,
138}
139
140// ---------------------------------------------------------------------------
141// Entry point detection
142// ---------------------------------------------------------------------------
143
144/// Well-known main/init function names (case-insensitive exact match).
145const MAIN_INIT_NAMES: &[&str] = &["main", "init", "setup", "bootstrap", "run"];
146
147/// Determine whether a symbol is an entry point.
148///
149/// Entry points are:
150/// - Exported standalone functions (not methods — methods are class members)
151/// - Functions matching well-known main/init patterns (any language)
152/// - Test functions matching language-specific patterns
153pub fn is_entry_point(name: &str, kind: &SymbolKind, exported: bool, lang: LangId) -> bool {
154    // Exported standalone functions
155    if exported && *kind == SymbolKind::Function {
156        return true;
157    }
158
159    // Main/init patterns (case-insensitive exact match, any kind)
160    let lower = name.to_lowercase();
161    if MAIN_INIT_NAMES.contains(&lower.as_str()) {
162        return true;
163    }
164
165    // Test patterns by language
166    match lang {
167        LangId::TypeScript | LangId::JavaScript | LangId::Tsx => {
168            // describe, it, test (exact), or starts with test/spec
169            matches!(lower.as_str(), "describe" | "it" | "test")
170                || lower.starts_with("test")
171                || lower.starts_with("spec")
172        }
173        LangId::Python => {
174            // starts with test_ or matches setUp/tearDown
175            lower.starts_with("test_") || matches!(name, "setUp" | "tearDown")
176        }
177        LangId::Rust => {
178            // starts with test_
179            lower.starts_with("test_")
180        }
181        LangId::Go => {
182            // starts with Test (case-sensitive)
183            name.starts_with("Test")
184        }
185        LangId::Markdown => false,
186    }
187}
188
189// ---------------------------------------------------------------------------
190// Trace-to types
191// ---------------------------------------------------------------------------
192
193/// A single hop in a trace path.
194#[derive(Debug, Clone, Serialize)]
195pub struct TraceHop {
196    /// Symbol name at this hop.
197    pub symbol: String,
198    /// File path (relative to project root).
199    pub file: String,
200    /// 1-based line number.
201    pub line: u32,
202    /// Function signature if available.
203    #[serde(skip_serializing_if = "Option::is_none")]
204    pub signature: Option<String>,
205    /// Whether this hop is an entry point.
206    pub is_entry_point: bool,
207}
208
209/// A complete path from an entry point to the target symbol (top-down).
210#[derive(Debug, Clone, Serialize)]
211pub struct TracePath {
212    /// Hops from entry point (first) to target (last).
213    pub hops: Vec<TraceHop>,
214}
215
216/// Result of a `trace_to` query.
217#[derive(Debug, Clone, Serialize)]
218pub struct TraceToResult {
219    /// The target symbol that was traced.
220    pub target_symbol: String,
221    /// The target file (relative to project root).
222    pub target_file: String,
223    /// Complete paths from entry points to the target.
224    pub paths: Vec<TracePath>,
225    /// Total number of complete paths found.
226    pub total_paths: usize,
227    /// Number of distinct entry points found across all paths.
228    pub entry_points_found: usize,
229    /// Whether any path was cut short by the depth limit.
230    pub max_depth_reached: bool,
231    /// Number of paths that reached a dead end (no callers, not entry point).
232    pub truncated_paths: usize,
233}
234
235// ---------------------------------------------------------------------------
236// Impact analysis types
237// ---------------------------------------------------------------------------
238
239/// A single caller in an impact analysis result.
240#[derive(Debug, Clone, Serialize)]
241pub struct ImpactCaller {
242    /// Symbol that calls the target.
243    pub caller_symbol: String,
244    /// File containing the caller (relative to project root).
245    pub caller_file: String,
246    /// 1-based line number of the call site.
247    pub line: u32,
248    /// Caller's function/method signature, if available.
249    #[serde(skip_serializing_if = "Option::is_none")]
250    pub signature: Option<String>,
251    /// Whether the caller is an entry point.
252    pub is_entry_point: bool,
253    /// Source line at the call site (trimmed).
254    #[serde(skip_serializing_if = "Option::is_none")]
255    pub call_expression: Option<String>,
256    /// Parameter names extracted from the caller's signature.
257    pub parameters: Vec<String>,
258}
259
260/// Result of an `impact` query — enriched callers analysis.
261#[derive(Debug, Clone, Serialize)]
262pub struct ImpactResult {
263    /// The target symbol being analyzed.
264    pub symbol: String,
265    /// The target file (relative to project root).
266    pub file: String,
267    /// Target symbol's signature, if available.
268    #[serde(skip_serializing_if = "Option::is_none")]
269    pub signature: Option<String>,
270    /// Parameter names extracted from the target's signature.
271    pub parameters: Vec<String>,
272    /// Total number of affected call sites.
273    pub total_affected: usize,
274    /// Number of distinct files containing callers.
275    pub affected_files: usize,
276    /// Enriched caller details.
277    pub callers: Vec<ImpactCaller>,
278}
279
280// ---------------------------------------------------------------------------
281// Data flow tracking types
282// ---------------------------------------------------------------------------
283
284/// A single hop in a data flow trace.
285#[derive(Debug, Clone, Serialize)]
286pub struct DataFlowHop {
287    /// File path (relative to project root).
288    pub file: String,
289    /// Symbol (function/method) containing this hop.
290    pub symbol: String,
291    /// Variable or parameter name being tracked at this hop.
292    pub variable: String,
293    /// 1-based line number.
294    pub line: u32,
295    /// Type of data flow: "assignment", "parameter", or "return".
296    pub flow_type: String,
297    /// Whether this hop is an approximation (destructuring, spread, unresolved).
298    pub approximate: bool,
299}
300
301/// Result of a `trace_data` query — tracks how an expression flows through
302/// variable assignments and function parameters.
303#[derive(Debug, Clone, Serialize)]
304pub struct TraceDataResult {
305    /// The expression being tracked.
306    pub expression: String,
307    /// The file where tracking started.
308    pub origin_file: String,
309    /// The symbol where tracking started.
310    pub origin_symbol: String,
311    /// Hops through assignments and parameters.
312    pub hops: Vec<DataFlowHop>,
313    /// Whether tracking stopped due to depth limit.
314    pub depth_limited: bool,
315}
316
317/// Extract parameter names from a function signature string.
318///
319/// Strips language-specific receivers (`self`, `&self`, `&mut self` for Rust,
320/// `self` for Python) and type annotations / default values. Returns just
321/// the parameter names.
322pub fn extract_parameters(signature: &str, lang: LangId) -> Vec<String> {
323    // Find the parameter list between parentheses
324    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    // Split on commas, respecting nested generics/brackets
339    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        // Skip language-specific receivers
349        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        // Extract just the parameter name
369        let name = extract_param_name(trimmed, lang);
370        if !name.is_empty() {
371            result.push(name);
372        }
373    }
374
375    result
376}
377
378/// Split parameter string on commas, respecting nested brackets/generics.
379fn 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
409/// Extract the parameter name from a single parameter declaration.
410///
411/// Handles:
412/// - TS/JS: `name: Type`, `name = default`, `...name`, `name?: Type`
413/// - Python: `name: Type`, `name=default`, `*args`, `**kwargs`
414/// - Rust: `name: Type`, `mut name: Type`
415/// - Go: `name Type`, `name, name2 Type`
416fn extract_param_name(param: &str, lang: LangId) -> String {
417    let trimmed = param.trim();
418
419    // Handle rest/spread params
420    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    // Rust: `mut name: Type` → strip `mut `
431    let working = if lang == LangId::Rust && working.starts_with("mut ") {
432        &working[4..]
433    } else {
434        working
435    };
436
437    // Strip type annotation (`: Type`) and default values (`= default`)
438    // Take only the name part — everything before `:`, `=`, or `?`
439    let name = working
440        .split(|c: char| c == ':' || c == '=')
441        .next()
442        .unwrap_or("")
443        .trim();
444
445    // Strip trailing `?` (optional params in TS)
446    let name = name.trim_end_matches('?');
447
448    // For Go, the name might be just `name Type` — take the first word
449    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
459// ---------------------------------------------------------------------------
460// CallGraph
461// ---------------------------------------------------------------------------
462
463/// Worktree-scoped call graph with lazy per-file construction.
464///
465/// Files are parsed and analyzed on first access, then cached. The graph
466/// can resolve cross-file call edges using the import engine.
467pub struct CallGraph {
468    /// Cached per-file call data.
469    data: HashMap<PathBuf, FileCallData>,
470    /// Project root for relative path resolution.
471    project_root: PathBuf,
472    /// All files discovered in the worktree (lazily populated).
473    project_files: Option<Vec<PathBuf>>,
474    /// Reverse index: (target_file, target_symbol) → callers.
475    /// Built lazily on first `callers_of` call, cleared on `invalidate_file`.
476    reverse_index: Option<HashMap<(PathBuf, String), Vec<CallerSite>>>,
477}
478
479impl CallGraph {
480    /// Create a new call graph for a project.
481    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    /// Get the project root directory.
491    pub fn project_root(&self) -> &Path {
492        &self.project_root
493    }
494
495    /// Get or build the call data for a file.
496    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    /// Resolve a cross-file call edge.
508    ///
509    /// Given a callee expression and the calling file's import block,
510    /// determines which file and symbol the call targets.
511    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        // Strategy:
519        // 1. Check if the callee matches a namespace import (utils.foo → import * as utils)
520        // 2. Check if the callee matches a named import (foo → import { foo } from './mod')
521        // 3. Check if the callee matches an aliased import (bar → import { foo as bar } from './mod')
522        // 4. Check if the callee matches a default import
523
524        let caller_dir = caller_file.parent().unwrap_or(Path::new("."));
525
526        // Check namespace imports: "utils.foo" where utils is a namespace import
527        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        // Check named imports (direct and aliased)
549        for imp in &import_block.imports {
550            // Direct named import: import { foo } from './utils'
551            if imp.names.contains(&short_name.to_string()) {
552                if let Some(resolved_path) = resolve_module_path(caller_dir, &imp.module_path) {
553                    // The name in the import is the original name from the source module
554                    return EdgeResolution::Resolved {
555                        file: resolved_path,
556                        symbol: short_name.to_string(),
557                    };
558                }
559            }
560
561            // Default import: import foo from './utils'
562            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        // Check aliased imports by examining the raw import text.
573        // ImportStatement.names stores the original name (foo), but the local code
574        // uses the alias (bar). We need to parse `import { foo as bar }` to find
575        // that `bar` maps to `foo`.
576        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        // Try barrel file re-exports: if any import points to an index file,
586        // check if that file re-exports the symbol
587        for imp in &import_block.imports {
588            if let Some(resolved_path) = resolve_module_path(caller_dir, &imp.module_path) {
589                // Check if the resolved path is a directory (barrel file)
590                if resolved_path.is_dir() {
591                    if let Some(index_path) = find_index_file(&resolved_path) {
592                        // Check if the index file exports this symbol
593                        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    /// Check if a file exports a given symbol name.
615    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    /// Depth-limited forward call tree traversal.
623    ///
624    /// Starting from a (file, symbol) pair, recursively follows calls
625    /// up to `max_depth` levels. Uses a visited set for cycle detection.
626    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        // Cycle detection
648        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![], // cycle — stop recursion
657            });
658        }
659
660        visited.insert(visit_key.clone());
661
662        // Build file data
663        let file_data = build_file_data(&canon)?;
664        let import_block = file_data.import_block.clone();
665        let _lang = file_data.lang;
666
667        // Get call sites for this symbol
668        let call_sites = file_data
669            .calls_by_symbol
670            .get(symbol)
671            .cloned()
672            .unwrap_or_default();
673
674        // Get symbol metadata (line, signature)
675        let (sym_line, sym_signature) = get_symbol_meta(&canon, symbol);
676
677        // Cache file data
678        self.data.insert(canon.clone(), file_data);
679
680        // Build children
681        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                                // Target file can't be parsed — mark as unresolved leaf
707                                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    /// Get all project files (lazily discovered).
745    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    /// Build the reverse index by scanning all project files.
754    ///
755    /// For each file, builds the call data (if not cached), then for each
756    /// (symbol, call_sites) pair, resolves cross-file edges and inserts
757    /// into the reverse map: `(target_file, target_symbol) → Vec<CallerSite>`.
758    fn build_reverse_index(&mut self) {
759        // Discover all project files first
760        let all_files = self.project_files().to_vec();
761
762        // Build file data for all project files
763        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        // Now build the reverse map
772        let mut reverse: HashMap<(PathBuf, String), Vec<CallerSite>> = HashMap::new();
773
774        for caller_file in &all_files {
775            // Canonicalize the caller file path for consistent lookups
776            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                            // For unresolved edges, index by (caller_file, callee_name)
812                            // so same-file calls are still findable
813                            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    /// Get callers of a symbol in a file, grouped by calling file.
831    ///
832    /// Builds the reverse index on first call (scans all project files).
833    /// Supports recursive depth expansion: depth=1 returns direct callers,
834    /// depth=2 returns callers-of-callers, etc. depth=0 is treated as 1.
835    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        // Ensure file is built (may already be cached)
844        self.build_file(&canon)?;
845
846        // Build the reverse index if not cached
847        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        // Group by file
866        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        // Sort groups by file path for deterministic output
887        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    /// Trace backward from a symbol to all entry points.
899    ///
900    /// Returns complete paths (top-down: entry point first, target last).
901    /// Uses BFS backward through the reverse index, with per-path cycle
902    /// detection and depth limiting.
903    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        // Ensure file is built
912        self.build_file(&canon)?;
913
914        // Build the reverse index if not cached
915        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        // Get line/signature for the target symbol
932        let (target_line, target_sig) = get_symbol_meta(&canon, symbol);
933
934        // Check if target itself is an entry point
935        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        // BFS state: each item is a partial path (bottom-up, will be reversed later)
944        // Each path element: (canonicalized file, symbol name, line, signature)
945        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        // Initial path starts at the target
951        let initial: Vec<PathElem> =
952            vec![(canon.clone(), symbol.to_string(), target_line, target_sig)];
953
954        // If the target itself is an entry point, record it as a trivial path
955        if target_is_entry {
956            complete_paths.push(initial.clone());
957        }
958
959        // Queue of (current_path, depth)
960        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            // Build per-path visited set from path elements
973            let path_visited: HashSet<(PathBuf, String)> = path
974                .iter()
975                .map(|(f, s, _, _)| (f.clone(), s.clone()))
976                .collect();
977
978            // Look up callers in reverse index
979            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                    // Dead end: no callers and not an entry point
984                    // (if it were an entry point, we'd have recorded it already)
985                    if path.len() > 1 {
986                        // Only count as truncated if this isn't the target itself
987                        // (the target with no callers is just "no paths found")
988                        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                // Cycle detection: skip if this caller is already in the current path
999                if path_visited.contains(&caller_key) {
1000                    continue;
1001                }
1002
1003                has_new_path = true;
1004
1005                // Get caller's metadata
1006                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                // Check if this caller is an entry point
1018                // Try both canonical and non-canonical keys (build_reverse_index
1019                // may have stored data under the raw walker path)
1020                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                // Always continue searching backward — there may be longer
1037                // paths through other entry points beyond this one
1038                queue.push((new_path, depth + 1));
1039            }
1040
1041            // If we had callers but none were new (all cycles), count as truncated
1042            if !has_new_path && path.len() > 1 {
1043                truncated_paths += 1;
1044            }
1045        }
1046
1047        // Reverse each path so it reads top-down (entry point → ... → target)
1048        // and convert to TraceHop/TracePath
1049        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                            // First hop (after reverse) is the entry point
1059                            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        // Sort paths for deterministic output (by entry point name, then path length)
1082        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        // Count distinct entry points
1089        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    /// Impact analysis: enriched callers query.
1113    ///
1114    /// Returns all call sites affected by a change to the given symbol,
1115    /// annotated with each caller's signature, entry point status, the
1116    /// source line at the call site, and extracted parameter names.
1117    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        // Ensure file is built
1126        self.build_file(&canon)?;
1127
1128        // Build the reverse index if not cached
1129        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        // Get the target symbol's own metadata
1136        let (target_signature, target_parameters, target_lang) = {
1137            let file_data = self.data.get(&canon).expect("file just built");
1138            let meta = file_data.symbol_metadata.get(symbol);
1139            let sig = meta.and_then(|m| m.signature.clone());
1140            let lang = file_data.lang;
1141            let params = sig
1142                .as_deref()
1143                .map(|s| extract_parameters(s, lang))
1144                .unwrap_or_default();
1145            (sig, params, lang)
1146        };
1147
1148        // Collect all caller sites (transitive)
1149        let mut visited = HashSet::new();
1150        let mut all_sites: Vec<CallerSite> = Vec::new();
1151        self.collect_callers_recursive(
1152            &canon,
1153            symbol,
1154            effective_depth,
1155            0,
1156            &mut visited,
1157            &mut all_sites,
1158        );
1159
1160        // Deduplicate sites by (file, symbol, line)
1161        let mut seen = HashSet::new();
1162        all_sites.retain(|s| seen.insert((s.caller_file.clone(), s.caller_symbol.clone(), s.line)));
1163
1164        // Enrich each caller site
1165        let mut callers = Vec::new();
1166        let mut affected_file_set = HashSet::new();
1167
1168        for site in &all_sites {
1169            // Build the caller's file to get metadata
1170            let caller_canon = std::fs::canonicalize(&site.caller_file)
1171                .unwrap_or_else(|_| site.caller_file.clone());
1172            let _ = self.build_file(&caller_canon);
1173
1174            let (sig, is_ep, params, _lang) = {
1175                if let Some(fd) = self.data.get(&caller_canon) {
1176                    let meta = fd.symbol_metadata.get(&site.caller_symbol);
1177                    let sig = meta.and_then(|m| m.signature.clone());
1178                    let kind = meta.map(|m| m.kind.clone()).unwrap_or(SymbolKind::Function);
1179                    let exported = meta.map(|m| m.exported).unwrap_or(false);
1180                    let is_ep = is_entry_point(&site.caller_symbol, &kind, exported, fd.lang);
1181                    let lang = fd.lang;
1182                    let params = sig
1183                        .as_deref()
1184                        .map(|s| extract_parameters(s, lang))
1185                        .unwrap_or_default();
1186                    (sig, is_ep, params, lang)
1187                } else {
1188                    (None, false, Vec::new(), target_lang)
1189                }
1190            };
1191
1192            // Read the source line at the call site
1193            let call_expression = self.read_source_line(&caller_canon, site.line);
1194
1195            let rel_file = self.relative_path(&site.caller_file);
1196            affected_file_set.insert(rel_file.clone());
1197
1198            callers.push(ImpactCaller {
1199                caller_symbol: site.caller_symbol.clone(),
1200                caller_file: rel_file,
1201                line: site.line,
1202                signature: sig,
1203                is_entry_point: is_ep,
1204                call_expression,
1205                parameters: params,
1206            });
1207        }
1208
1209        // Sort callers by file then line for deterministic output
1210        callers.sort_by(|a, b| a.caller_file.cmp(&b.caller_file).then(a.line.cmp(&b.line)));
1211
1212        let total_affected = callers.len();
1213        let affected_files = affected_file_set.len();
1214
1215        Ok(ImpactResult {
1216            symbol: symbol.to_string(),
1217            file: self.relative_path(&canon),
1218            signature: target_signature,
1219            parameters: target_parameters,
1220            total_affected,
1221            affected_files,
1222            callers,
1223        })
1224    }
1225
1226    /// Trace how an expression flows through variable assignments within a
1227    /// function body and across function boundaries via argument-to-parameter
1228    /// matching.
1229    ///
1230    /// Algorithm:
1231    /// 1. Parse the function body, find the expression text.
1232    /// 2. Walk AST for assignments that reference the tracked name.
1233    /// 3. When the tracked name appears as a call argument, resolve the callee,
1234    ///    match argument position to parameter name, recurse.
1235    /// 4. Destructuring, spread, and unresolved calls produce approximate hops.
1236    pub fn trace_data(
1237        &mut self,
1238        file: &Path,
1239        symbol: &str,
1240        expression: &str,
1241        max_depth: usize,
1242    ) -> Result<TraceDataResult, AftError> {
1243        let canon = self.canonicalize(file)?;
1244        let rel_file = self.relative_path(&canon);
1245
1246        // Ensure file data is built
1247        self.build_file(&canon)?;
1248
1249        // Verify symbol exists
1250        {
1251            let fd = self.data.get(&canon).expect("file just built");
1252            let has_symbol = fd.calls_by_symbol.contains_key(symbol)
1253                || fd.exported_symbols.contains(&symbol.to_string())
1254                || fd.symbol_metadata.contains_key(symbol);
1255            if !has_symbol {
1256                return Err(AftError::InvalidRequest {
1257                    message: format!(
1258                        "trace_data: symbol '{}' not found in {}",
1259                        symbol,
1260                        file.display()
1261                    ),
1262                });
1263            }
1264        }
1265
1266        let mut hops = Vec::new();
1267        let mut depth_limited = false;
1268
1269        self.trace_data_inner(
1270            &canon,
1271            symbol,
1272            expression,
1273            max_depth,
1274            0,
1275            &mut hops,
1276            &mut depth_limited,
1277            &mut HashSet::new(),
1278        );
1279
1280        Ok(TraceDataResult {
1281            expression: expression.to_string(),
1282            origin_file: rel_file,
1283            origin_symbol: symbol.to_string(),
1284            hops,
1285            depth_limited,
1286        })
1287    }
1288
1289    /// Inner recursive data flow tracking.
1290    fn trace_data_inner(
1291        &mut self,
1292        file: &Path,
1293        symbol: &str,
1294        tracking_name: &str,
1295        max_depth: usize,
1296        current_depth: usize,
1297        hops: &mut Vec<DataFlowHop>,
1298        depth_limited: &mut bool,
1299        visited: &mut HashSet<(PathBuf, String, String)>,
1300    ) {
1301        let visit_key = (
1302            file.to_path_buf(),
1303            symbol.to_string(),
1304            tracking_name.to_string(),
1305        );
1306        if visited.contains(&visit_key) {
1307            return; // cycle
1308        }
1309        visited.insert(visit_key);
1310
1311        // Read and parse the file
1312        let source = match std::fs::read_to_string(file) {
1313            Ok(s) => s,
1314            Err(_) => return,
1315        };
1316
1317        let lang = match detect_language(file) {
1318            Some(l) => l,
1319            None => return,
1320        };
1321
1322        let grammar = grammar_for(lang);
1323        let mut parser = Parser::new();
1324        if parser.set_language(&grammar).is_err() {
1325            return;
1326        }
1327        let tree = match parser.parse(&source, None) {
1328            Some(t) => t,
1329            None => return,
1330        };
1331
1332        // Find the symbol's AST node range
1333        let symbols = list_symbols_from_tree(&source, &tree, lang, file);
1334        let sym_info = match symbols.iter().find(|s| s.name == symbol) {
1335            Some(s) => s,
1336            None => return,
1337        };
1338
1339        let body_start = line_col_to_byte(&source, sym_info.start_line, sym_info.start_col);
1340        let body_end = line_col_to_byte(&source, sym_info.end_line, sym_info.end_col);
1341
1342        let root = tree.root_node();
1343
1344        // Find the symbol's body node (the function/method definition node)
1345        let body_node = match find_node_covering_range(root, body_start, body_end) {
1346            Some(n) => n,
1347            None => return,
1348        };
1349
1350        // Track names through the body
1351        let mut tracked_names: Vec<String> = vec![tracking_name.to_string()];
1352        let rel_file = self.relative_path(file);
1353
1354        // Walk the body looking for assignments and calls
1355        self.walk_for_data_flow(
1356            body_node,
1357            &source,
1358            &mut tracked_names,
1359            file,
1360            symbol,
1361            &rel_file,
1362            lang,
1363            max_depth,
1364            current_depth,
1365            hops,
1366            depth_limited,
1367            visited,
1368        );
1369    }
1370
1371    /// Walk an AST subtree looking for assignments and call expressions that
1372    /// reference tracked names.
1373    #[allow(clippy::too_many_arguments)]
1374    fn walk_for_data_flow(
1375        &mut self,
1376        node: tree_sitter::Node,
1377        source: &str,
1378        tracked_names: &mut Vec<String>,
1379        file: &Path,
1380        symbol: &str,
1381        rel_file: &str,
1382        lang: LangId,
1383        max_depth: usize,
1384        current_depth: usize,
1385        hops: &mut Vec<DataFlowHop>,
1386        depth_limited: &mut bool,
1387        visited: &mut HashSet<(PathBuf, String, String)>,
1388    ) {
1389        let kind = node.kind();
1390
1391        // Check for variable declarations / assignments
1392        let is_var_decl = matches!(
1393            kind,
1394            "variable_declarator"
1395                | "assignment_expression"
1396                | "augmented_assignment_expression"
1397                | "assignment"
1398                | "let_declaration"
1399                | "short_var_declaration"
1400        );
1401
1402        if is_var_decl {
1403            if let Some((new_name, init_text, line, is_approx)) =
1404                self.extract_assignment_info(node, source, lang, tracked_names)
1405            {
1406                // The RHS references a tracked name — add assignment hop
1407                if !is_approx {
1408                    hops.push(DataFlowHop {
1409                        file: rel_file.to_string(),
1410                        symbol: symbol.to_string(),
1411                        variable: new_name.clone(),
1412                        line,
1413                        flow_type: "assignment".to_string(),
1414                        approximate: false,
1415                    });
1416                    tracked_names.push(new_name);
1417                } else {
1418                    // Destructuring or pattern — approximate
1419                    hops.push(DataFlowHop {
1420                        file: rel_file.to_string(),
1421                        symbol: symbol.to_string(),
1422                        variable: init_text,
1423                        line,
1424                        flow_type: "assignment".to_string(),
1425                        approximate: true,
1426                    });
1427                    // Don't track further through this branch
1428                    return;
1429                }
1430            }
1431        }
1432
1433        // Check for call expressions where tracked name is an argument
1434        if kind == "call_expression" || kind == "call" || kind == "macro_invocation" {
1435            self.check_call_for_data_flow(
1436                node,
1437                source,
1438                tracked_names,
1439                file,
1440                symbol,
1441                rel_file,
1442                lang,
1443                max_depth,
1444                current_depth,
1445                hops,
1446                depth_limited,
1447                visited,
1448            );
1449        }
1450
1451        // Recurse into children
1452        let mut cursor = node.walk();
1453        if cursor.goto_first_child() {
1454            loop {
1455                let child = cursor.node();
1456                // Don't re-process the current node type in recursion
1457                self.walk_for_data_flow(
1458                    child,
1459                    source,
1460                    tracked_names,
1461                    file,
1462                    symbol,
1463                    rel_file,
1464                    lang,
1465                    max_depth,
1466                    current_depth,
1467                    hops,
1468                    depth_limited,
1469                    visited,
1470                );
1471                if !cursor.goto_next_sibling() {
1472                    break;
1473                }
1474            }
1475        }
1476    }
1477
1478    /// Check if an assignment/declaration node assigns from a tracked name.
1479    /// Returns (new_name, init_text, line, is_approximate).
1480    fn extract_assignment_info(
1481        &self,
1482        node: tree_sitter::Node,
1483        source: &str,
1484        _lang: LangId,
1485        tracked_names: &[String],
1486    ) -> Option<(String, String, u32, bool)> {
1487        let kind = node.kind();
1488        let line = node.start_position().row as u32 + 1;
1489
1490        match kind {
1491            "variable_declarator" => {
1492                // TS/JS: const x = <expr>
1493                let name_node = node.child_by_field_name("name")?;
1494                let value_node = node.child_by_field_name("value")?;
1495                let name_text = node_text(name_node, source);
1496                let value_text = node_text(value_node, source);
1497
1498                // Check if name is a destructuring pattern
1499                if name_node.kind() == "object_pattern" || name_node.kind() == "array_pattern" {
1500                    // Check if value references a tracked name
1501                    if tracked_names.iter().any(|t| value_text.contains(t)) {
1502                        return Some((name_text.clone(), name_text, line, true));
1503                    }
1504                    return None;
1505                }
1506
1507                // Check if value references any tracked name
1508                if tracked_names.iter().any(|t| {
1509                    value_text == *t
1510                        || value_text.starts_with(&format!("{}.", t))
1511                        || value_text.starts_with(&format!("{}[", t))
1512                }) {
1513                    return Some((name_text, value_text, line, false));
1514                }
1515                None
1516            }
1517            "assignment_expression" | "augmented_assignment_expression" => {
1518                // TS/JS: x = <expr>
1519                let left = node.child_by_field_name("left")?;
1520                let right = node.child_by_field_name("right")?;
1521                let left_text = node_text(left, source);
1522                let right_text = node_text(right, source);
1523
1524                if tracked_names.iter().any(|t| right_text == *t) {
1525                    return Some((left_text, right_text, line, false));
1526                }
1527                None
1528            }
1529            "assignment" => {
1530                // Python: x = <expr>
1531                let left = node.child_by_field_name("left")?;
1532                let right = node.child_by_field_name("right")?;
1533                let left_text = node_text(left, source);
1534                let right_text = node_text(right, source);
1535
1536                if tracked_names.iter().any(|t| right_text == *t) {
1537                    return Some((left_text, right_text, line, false));
1538                }
1539                None
1540            }
1541            "let_declaration" | "short_var_declaration" => {
1542                // Rust / Go
1543                let left = node
1544                    .child_by_field_name("pattern")
1545                    .or_else(|| node.child_by_field_name("left"))?;
1546                let right = node
1547                    .child_by_field_name("value")
1548                    .or_else(|| node.child_by_field_name("right"))?;
1549                let left_text = node_text(left, source);
1550                let right_text = node_text(right, source);
1551
1552                if tracked_names.iter().any(|t| right_text == *t) {
1553                    return Some((left_text, right_text, line, false));
1554                }
1555                None
1556            }
1557            _ => None,
1558        }
1559    }
1560
1561    /// Check if a call expression uses a tracked name as an argument, and if so,
1562    /// resolve the callee and recurse into its body tracking the parameter name.
1563    #[allow(clippy::too_many_arguments)]
1564    fn check_call_for_data_flow(
1565        &mut self,
1566        node: tree_sitter::Node,
1567        source: &str,
1568        tracked_names: &[String],
1569        file: &Path,
1570        _symbol: &str,
1571        rel_file: &str,
1572        _lang: LangId,
1573        max_depth: usize,
1574        current_depth: usize,
1575        hops: &mut Vec<DataFlowHop>,
1576        depth_limited: &mut bool,
1577        visited: &mut HashSet<(PathBuf, String, String)>,
1578    ) {
1579        // Find the arguments node
1580        let args_node = find_child_by_kind(node, "arguments")
1581            .or_else(|| find_child_by_kind(node, "argument_list"));
1582
1583        let args_node = match args_node {
1584            Some(n) => n,
1585            None => return,
1586        };
1587
1588        // Collect argument texts and find which position a tracked name appears at
1589        let mut arg_positions: Vec<(usize, String)> = Vec::new(); // (position, tracked_name)
1590        let mut arg_idx = 0;
1591
1592        let mut cursor = args_node.walk();
1593        if cursor.goto_first_child() {
1594            loop {
1595                let child = cursor.node();
1596                let child_kind = child.kind();
1597
1598                // Skip punctuation (parentheses, commas)
1599                if child_kind == "(" || child_kind == ")" || child_kind == "," {
1600                    if !cursor.goto_next_sibling() {
1601                        break;
1602                    }
1603                    continue;
1604                }
1605
1606                let arg_text = node_text(child, source);
1607
1608                // Check for spread element — approximate
1609                if child_kind == "spread_element" || child_kind == "dictionary_splat" {
1610                    if tracked_names.iter().any(|t| arg_text.contains(t)) {
1611                        hops.push(DataFlowHop {
1612                            file: rel_file.to_string(),
1613                            symbol: _symbol.to_string(),
1614                            variable: arg_text,
1615                            line: child.start_position().row as u32 + 1,
1616                            flow_type: "parameter".to_string(),
1617                            approximate: true,
1618                        });
1619                    }
1620                    if !cursor.goto_next_sibling() {
1621                        break;
1622                    }
1623                    arg_idx += 1;
1624                    continue;
1625                }
1626
1627                if tracked_names.iter().any(|t| arg_text == *t) {
1628                    arg_positions.push((arg_idx, arg_text));
1629                }
1630
1631                arg_idx += 1;
1632                if !cursor.goto_next_sibling() {
1633                    break;
1634                }
1635            }
1636        }
1637
1638        if arg_positions.is_empty() {
1639            return;
1640        }
1641
1642        // Resolve the callee
1643        let (full_callee, short_callee) = extract_callee_names(node, source);
1644        let full_callee = match full_callee {
1645            Some(f) => f,
1646            None => return,
1647        };
1648        let short_callee = match short_callee {
1649            Some(s) => s,
1650            None => return,
1651        };
1652
1653        // Try to resolve cross-file edge
1654        let import_block = {
1655            match self.data.get(file) {
1656                Some(fd) => fd.import_block.clone(),
1657                None => return,
1658            }
1659        };
1660
1661        let edge = self.resolve_cross_file_edge(&full_callee, &short_callee, file, &import_block);
1662
1663        match edge {
1664            EdgeResolution::Resolved {
1665                file: target_file,
1666                symbol: target_symbol,
1667            } => {
1668                if current_depth + 1 > max_depth {
1669                    *depth_limited = true;
1670                    return;
1671                }
1672
1673                // Build target file to get parameter info
1674                let _ = self.build_file(&target_file);
1675                let (params, _target_lang) = {
1676                    match self.data.get(&target_file) {
1677                        Some(fd) => {
1678                            let meta = fd.symbol_metadata.get(&target_symbol);
1679                            let sig = meta.and_then(|m| m.signature.clone());
1680                            let params = sig
1681                                .as_deref()
1682                                .map(|s| extract_parameters(s, fd.lang))
1683                                .unwrap_or_default();
1684                            (params, fd.lang)
1685                        }
1686                        None => return,
1687                    }
1688                };
1689
1690                let target_rel = self.relative_path(&target_file);
1691
1692                for (pos, _tracked) in &arg_positions {
1693                    if let Some(param_name) = params.get(*pos) {
1694                        // Add parameter hop
1695                        hops.push(DataFlowHop {
1696                            file: target_rel.clone(),
1697                            symbol: target_symbol.clone(),
1698                            variable: param_name.clone(),
1699                            line: get_symbol_meta(&target_file, &target_symbol).0,
1700                            flow_type: "parameter".to_string(),
1701                            approximate: false,
1702                        });
1703
1704                        // Recurse into callee's body tracking the parameter name
1705                        self.trace_data_inner(
1706                            &target_file.clone(),
1707                            &target_symbol.clone(),
1708                            param_name,
1709                            max_depth,
1710                            current_depth + 1,
1711                            hops,
1712                            depth_limited,
1713                            visited,
1714                        );
1715                    }
1716                }
1717            }
1718            EdgeResolution::Unresolved { callee_name } => {
1719                // Check if it's a same-file call
1720                let has_local = self
1721                    .data
1722                    .get(file)
1723                    .map(|fd| {
1724                        fd.calls_by_symbol.contains_key(&callee_name)
1725                            || fd.symbol_metadata.contains_key(&callee_name)
1726                    })
1727                    .unwrap_or(false);
1728
1729                if has_local {
1730                    // Same-file call — get param info
1731                    let (params, _target_lang) = {
1732                        let Some(fd) = self.data.get(file) else {
1733                            return;
1734                        };
1735                        let meta = fd.symbol_metadata.get(&callee_name);
1736                        let sig = meta.and_then(|m| m.signature.clone());
1737                        let params = sig
1738                            .as_deref()
1739                            .map(|s| extract_parameters(s, fd.lang))
1740                            .unwrap_or_default();
1741                        (params, fd.lang)
1742                    };
1743
1744                    let file_rel = self.relative_path(file);
1745
1746                    for (pos, _tracked) in &arg_positions {
1747                        if let Some(param_name) = params.get(*pos) {
1748                            hops.push(DataFlowHop {
1749                                file: file_rel.clone(),
1750                                symbol: callee_name.clone(),
1751                                variable: param_name.clone(),
1752                                line: get_symbol_meta(file, &callee_name).0,
1753                                flow_type: "parameter".to_string(),
1754                                approximate: false,
1755                            });
1756
1757                            // Recurse into same-file function
1758                            self.trace_data_inner(
1759                                file,
1760                                &callee_name.clone(),
1761                                param_name,
1762                                max_depth,
1763                                current_depth + 1,
1764                                hops,
1765                                depth_limited,
1766                                visited,
1767                            );
1768                        }
1769                    }
1770                } else {
1771                    // Truly unresolved — approximate hop
1772                    for (_pos, tracked) in &arg_positions {
1773                        hops.push(DataFlowHop {
1774                            file: self.relative_path(file),
1775                            symbol: callee_name.clone(),
1776                            variable: tracked.clone(),
1777                            line: node.start_position().row as u32 + 1,
1778                            flow_type: "parameter".to_string(),
1779                            approximate: true,
1780                        });
1781                    }
1782                }
1783            }
1784        }
1785    }
1786
1787    /// Read a single source line (1-based) from a file, trimmed.
1788    fn read_source_line(&self, path: &Path, line: u32) -> Option<String> {
1789        let content = std::fs::read_to_string(path).ok()?;
1790        content
1791            .lines()
1792            .nth(line.saturating_sub(1) as usize)
1793            .map(|l| l.trim().to_string())
1794    }
1795
1796    /// Recursively collect callers up to the given depth.
1797    fn collect_callers_recursive(
1798        &self,
1799        file: &Path,
1800        symbol: &str,
1801        max_depth: usize,
1802        current_depth: usize,
1803        visited: &mut HashSet<(PathBuf, String)>,
1804        result: &mut Vec<CallerSite>,
1805    ) {
1806        if current_depth >= max_depth {
1807            return;
1808        }
1809
1810        // Canonicalize for consistent reverse index lookup
1811        let canon = std::fs::canonicalize(file).unwrap_or_else(|_| file.to_path_buf());
1812        let key = (canon.clone(), symbol.to_string());
1813        if visited.contains(&key) {
1814            return; // cycle detection
1815        }
1816        visited.insert(key);
1817
1818        let reverse_index = match &self.reverse_index {
1819            Some(ri) => ri,
1820            None => return,
1821        };
1822
1823        let lookup_key = (canon, symbol.to_string());
1824        if let Some(sites) = reverse_index.get(&lookup_key) {
1825            for site in sites {
1826                result.push(site.clone());
1827                // Recurse: find callers of the caller
1828                if current_depth + 1 < max_depth {
1829                    self.collect_callers_recursive(
1830                        &site.caller_file,
1831                        &site.caller_symbol,
1832                        max_depth,
1833                        current_depth + 1,
1834                        visited,
1835                        result,
1836                    );
1837                }
1838            }
1839        }
1840    }
1841
1842    /// Invalidate a file: remove its cached data and clear the reverse index.
1843    ///
1844    /// Called by the file watcher when a file changes on disk. The reverse
1845    /// index is rebuilt lazily on the next `callers_of` call.
1846    pub fn invalidate_file(&mut self, path: &Path) {
1847        // Remove from data cache (try both as-is and canonicalized)
1848        self.data.remove(path);
1849        if let Ok(canon) = self.canonicalize(path) {
1850            self.data.remove(&canon);
1851        }
1852        // Clear the reverse index — it's stale
1853        self.reverse_index = None;
1854        // Clear project_files cache for create/remove events
1855        self.project_files = None;
1856    }
1857
1858    /// Return a path relative to the project root, or the absolute path if
1859    /// it's outside the project.
1860    fn relative_path(&self, path: &Path) -> String {
1861        path.strip_prefix(&self.project_root)
1862            .unwrap_or(path)
1863            .display()
1864            .to_string()
1865    }
1866
1867    /// Canonicalize a path, falling back to the original if canonicalization fails.
1868    fn canonicalize(&self, path: &Path) -> Result<PathBuf, AftError> {
1869        // If the path is relative, resolve it against project_root
1870        let full_path = if path.is_relative() {
1871            self.project_root.join(path)
1872        } else {
1873            path.to_path_buf()
1874        };
1875
1876        // Try canonicalize, fall back to the full path
1877        Ok(std::fs::canonicalize(&full_path).unwrap_or(full_path))
1878    }
1879
1880    /// Look up cached file data, trying both the given path and its
1881    /// canonicalized form. Needed because `build_reverse_index` may store
1882    /// data under raw walker paths while CallerSite uses canonical paths.
1883    fn lookup_file_data(&self, path: &Path) -> Option<&FileCallData> {
1884        if let Some(fd) = self.data.get(path) {
1885            return Some(fd);
1886        }
1887        // Try canonical
1888        let canon = std::fs::canonicalize(path).ok()?;
1889        self.data.get(&canon).or_else(|| {
1890            // Try non-canonical forms stored by the walker
1891            self.data.iter().find_map(|(k, v)| {
1892                if std::fs::canonicalize(k).ok().as_ref() == Some(&canon) {
1893                    Some(v)
1894                } else {
1895                    None
1896                }
1897            })
1898        })
1899    }
1900}
1901
1902// ---------------------------------------------------------------------------
1903// File-level building
1904// ---------------------------------------------------------------------------
1905
1906/// Build call data for a single file.
1907fn build_file_data(path: &Path) -> Result<FileCallData, AftError> {
1908    let lang = detect_language(path).ok_or_else(|| AftError::InvalidRequest {
1909        message: format!("unsupported file for call graph: {}", path.display()),
1910    })?;
1911
1912    let source = std::fs::read_to_string(path).map_err(|e| AftError::FileNotFound {
1913        path: format!("{}: {}", path.display(), e),
1914    })?;
1915
1916    let grammar = grammar_for(lang);
1917    let mut parser = Parser::new();
1918    parser
1919        .set_language(&grammar)
1920        .map_err(|e| AftError::ParseError {
1921            message: format!("grammar init failed for {:?}: {}", lang, e),
1922        })?;
1923
1924    let tree = parser
1925        .parse(&source, None)
1926        .ok_or_else(|| AftError::ParseError {
1927            message: format!("parse failed for {}", path.display()),
1928        })?;
1929
1930    // Parse imports
1931    let import_block = imports::parse_imports(&source, &tree, lang);
1932
1933    // Get symbols (for call site extraction and export detection)
1934    let symbols = list_symbols_from_tree(&source, &tree, lang, path);
1935
1936    // Build calls_by_symbol
1937    let mut calls_by_symbol: HashMap<String, Vec<CallSite>> = HashMap::new();
1938    let root = tree.root_node();
1939
1940    for sym in &symbols {
1941        let byte_start = line_col_to_byte(&source, sym.start_line, sym.start_col);
1942        let byte_end = line_col_to_byte(&source, sym.end_line, sym.end_col);
1943
1944        let raw_calls = extract_calls_full(&source, root, byte_start, byte_end, lang);
1945
1946        let sites: Vec<CallSite> = raw_calls
1947            .into_iter()
1948            .filter(|(_, short, _)| *short != sym.name) // exclude self-references
1949            .map(|(full, short, line)| CallSite {
1950                callee_name: short,
1951                full_callee: full,
1952                line,
1953                byte_start,
1954                byte_end,
1955            })
1956            .collect();
1957
1958        if !sites.is_empty() {
1959            calls_by_symbol.insert(sym.name.clone(), sites);
1960        }
1961    }
1962
1963    // Collect exported symbol names
1964    let exported_symbols: Vec<String> = symbols
1965        .iter()
1966        .filter(|s| s.exported)
1967        .map(|s| s.name.clone())
1968        .collect();
1969
1970    // Build per-symbol metadata for entry point detection
1971    let symbol_metadata: HashMap<String, SymbolMeta> = symbols
1972        .iter()
1973        .map(|s| {
1974            (
1975                s.name.clone(),
1976                SymbolMeta {
1977                    kind: s.kind.clone(),
1978                    exported: s.exported,
1979                    signature: s.signature.clone(),
1980                },
1981            )
1982        })
1983        .collect();
1984
1985    Ok(FileCallData {
1986        calls_by_symbol,
1987        exported_symbols,
1988        symbol_metadata,
1989        import_block,
1990        lang,
1991    })
1992}
1993
1994/// Minimal symbol info needed for call graph construction.
1995#[derive(Debug)]
1996#[allow(dead_code)]
1997struct SymbolInfo {
1998    name: String,
1999    kind: SymbolKind,
2000    start_line: u32,
2001    start_col: u32,
2002    end_line: u32,
2003    end_col: u32,
2004    exported: bool,
2005    signature: Option<String>,
2006}
2007
2008/// Extract symbols from a parsed tree without going through the full
2009/// FileParser/AppContext machinery.
2010fn list_symbols_from_tree(
2011    _source: &str,
2012    _tree: &Tree,
2013    _lang: LangId,
2014    path: &Path,
2015) -> Vec<SymbolInfo> {
2016    // Use the parser module's symbol listing via a temporary FileParser
2017    let mut file_parser = crate::parser::FileParser::new();
2018    match file_parser.parse(path) {
2019        Ok(_) => {}
2020        Err(_) => return vec![],
2021    }
2022
2023    // Use the tree-sitter provider to list symbols
2024    let provider = crate::parser::TreeSitterProvider::new();
2025    match provider.list_symbols(path) {
2026        Ok(symbols) => symbols
2027            .into_iter()
2028            .map(|s| SymbolInfo {
2029                name: s.name,
2030                kind: s.kind,
2031                start_line: s.range.start_line,
2032                start_col: s.range.start_col,
2033                end_line: s.range.end_line,
2034                end_col: s.range.end_col,
2035                exported: s.exported,
2036                signature: s.signature,
2037            })
2038            .collect(),
2039        Err(_) => vec![],
2040    }
2041}
2042
2043/// Convert a 0-based line + column to a byte offset in the source.
2044fn line_col_to_byte(source: &str, line: u32, col: u32) -> usize {
2045    let mut byte = 0;
2046    for (i, l) in source.lines().enumerate() {
2047        if i == line as usize {
2048            return byte + (col as usize).min(l.len());
2049        }
2050        byte += l.len() + 1;
2051    }
2052    source.len()
2053}
2054
2055/// Get symbol metadata (line, signature) from a file.
2056fn get_symbol_meta(path: &Path, symbol_name: &str) -> (u32, Option<String>) {
2057    let provider = crate::parser::TreeSitterProvider::new();
2058    match provider.list_symbols(path) {
2059        Ok(symbols) => {
2060            for s in &symbols {
2061                if s.name == symbol_name {
2062                    return (s.range.start_line + 1, s.signature.clone());
2063                }
2064            }
2065            (1, None)
2066        }
2067        Err(_) => (1, None),
2068    }
2069}
2070
2071// ---------------------------------------------------------------------------
2072// Data flow tracking helpers
2073// ---------------------------------------------------------------------------
2074
2075/// Get the text of a tree-sitter node from the source.
2076fn node_text(node: tree_sitter::Node, source: &str) -> String {
2077    source[node.start_byte()..node.end_byte()].to_string()
2078}
2079
2080/// Find the smallest node that fully covers a byte range.
2081fn find_node_covering_range(
2082    root: tree_sitter::Node,
2083    start: usize,
2084    end: usize,
2085) -> Option<tree_sitter::Node> {
2086    let mut best = None;
2087    let mut cursor = root.walk();
2088
2089    fn walk_covering<'a>(
2090        cursor: &mut tree_sitter::TreeCursor<'a>,
2091        start: usize,
2092        end: usize,
2093        best: &mut Option<tree_sitter::Node<'a>>,
2094    ) {
2095        let node = cursor.node();
2096        if node.start_byte() <= start && node.end_byte() >= end {
2097            *best = Some(node);
2098            if cursor.goto_first_child() {
2099                loop {
2100                    walk_covering(cursor, start, end, best);
2101                    if !cursor.goto_next_sibling() {
2102                        break;
2103                    }
2104                }
2105                cursor.goto_parent();
2106            }
2107        }
2108    }
2109
2110    walk_covering(&mut cursor, start, end, &mut best);
2111    best
2112}
2113
2114/// Find a direct child node by kind name.
2115fn find_child_by_kind<'a>(
2116    node: tree_sitter::Node<'a>,
2117    kind: &str,
2118) -> Option<tree_sitter::Node<'a>> {
2119    let mut cursor = node.walk();
2120    if cursor.goto_first_child() {
2121        loop {
2122            if cursor.node().kind() == kind {
2123                return Some(cursor.node());
2124            }
2125            if !cursor.goto_next_sibling() {
2126                break;
2127            }
2128        }
2129    }
2130    None
2131}
2132
2133/// Extract full and short callee names from a call_expression node.
2134fn extract_callee_names(node: tree_sitter::Node, source: &str) -> (Option<String>, Option<String>) {
2135    // The "function" field holds the callee
2136    let callee = match node.child_by_field_name("function") {
2137        Some(c) => c,
2138        None => return (None, None),
2139    };
2140
2141    let full = node_text(callee, source);
2142    let short = if full.contains('.') {
2143        full.rsplit('.').next().unwrap_or(&full).to_string()
2144    } else {
2145        full.clone()
2146    };
2147
2148    (Some(full), Some(short))
2149}
2150
2151// ---------------------------------------------------------------------------
2152// Module path resolution
2153// ---------------------------------------------------------------------------
2154
2155/// Resolve a module path (e.g. './utils') relative to a directory.
2156///
2157/// Tries common file extensions for TypeScript/JavaScript projects.
2158pub(crate) fn resolve_module_path(from_dir: &Path, module_path: &str) -> Option<PathBuf> {
2159    // Only handle relative imports
2160    if !module_path.starts_with('.') {
2161        return None;
2162    }
2163
2164    let base = from_dir.join(module_path);
2165
2166    // Try exact path first
2167    if base.is_file() {
2168        return Some(std::fs::canonicalize(&base).unwrap_or(base));
2169    }
2170
2171    // Try common extensions
2172    let extensions = [".ts", ".tsx", ".js", ".jsx"];
2173    for ext in &extensions {
2174        let with_ext = base.with_extension(ext.trim_start_matches('.'));
2175        if with_ext.is_file() {
2176            return Some(std::fs::canonicalize(&with_ext).unwrap_or(with_ext));
2177        }
2178    }
2179
2180    // Try as directory with index file
2181    if base.is_dir() {
2182        if let Some(index) = find_index_file(&base) {
2183            return Some(index);
2184        }
2185    }
2186
2187    None
2188}
2189
2190/// Find an index file in a directory.
2191fn find_index_file(dir: &Path) -> Option<PathBuf> {
2192    let candidates = ["index.ts", "index.tsx", "index.js", "index.jsx"];
2193    for name in &candidates {
2194        let p = dir.join(name);
2195        if p.is_file() {
2196            return Some(std::fs::canonicalize(&p).unwrap_or(p));
2197        }
2198    }
2199    None
2200}
2201
2202/// Resolve an aliased import: `import { foo as bar } from './utils'`
2203/// where `local_name` is "bar". Returns `(original_name, resolved_file_path)`.
2204fn resolve_aliased_import(
2205    local_name: &str,
2206    import_block: &ImportBlock,
2207    caller_dir: &Path,
2208) -> Option<(String, PathBuf)> {
2209    for imp in &import_block.imports {
2210        // Parse the raw text to find "as <alias>" patterns
2211        // This handles: import { foo as bar, baz as qux } from './mod'
2212        if let Some(original) = find_alias_original(&imp.raw_text, local_name) {
2213            if let Some(resolved_path) = resolve_module_path(caller_dir, &imp.module_path) {
2214                return Some((original, resolved_path));
2215            }
2216        }
2217    }
2218    None
2219}
2220
2221/// Parse import raw text to find the original name for an alias.
2222/// Given raw text like `import { foo as bar, baz } from './utils'` and
2223/// local_name "bar", returns Some("foo").
2224fn find_alias_original(raw_import: &str, local_name: &str) -> Option<String> {
2225    // Look for pattern: <original> as <alias>
2226    // This is a simple text-based search; handles the common TS/JS pattern
2227    let search = format!(" as {}", local_name);
2228    if let Some(pos) = raw_import.find(&search) {
2229        // Walk backwards from `pos` to find the original name
2230        let before = &raw_import[..pos];
2231        // The original name is the last word-like token before " as "
2232        let original = before
2233            .rsplit(|c: char| c == '{' || c == ',' || c.is_whitespace())
2234            .find(|s| !s.is_empty())?;
2235        return Some(original.to_string());
2236    }
2237    None
2238}
2239
2240// ---------------------------------------------------------------------------
2241// Worktree file discovery
2242// ---------------------------------------------------------------------------
2243
2244/// Walk project files respecting .gitignore, excluding common non-source dirs.
2245///
2246/// Returns an iterator of file paths for supported source file types.
2247pub fn walk_project_files(root: &Path) -> impl Iterator<Item = PathBuf> {
2248    use ignore::WalkBuilder;
2249
2250    let walker = WalkBuilder::new(root)
2251        .hidden(true)         // skip hidden files/dirs
2252        .git_ignore(true)     // respect .gitignore
2253        .git_global(true)     // respect global gitignore
2254        .git_exclude(true)    // respect .git/info/exclude
2255        .filter_entry(|entry| {
2256            let name = entry.file_name().to_string_lossy();
2257            // Always exclude these directories regardless of .gitignore
2258            if entry.file_type().map_or(false, |ft| ft.is_dir()) {
2259                return !matches!(
2260                    name.as_ref(),
2261                    "node_modules" | "target" | "venv" | ".venv" | ".git" | "__pycache__"
2262                        | ".tox" | "dist" | "build"
2263                );
2264            }
2265            true
2266        })
2267        .build();
2268
2269    walker
2270        .filter_map(|entry| entry.ok())
2271        .filter(|entry| entry.file_type().map_or(false, |ft| ft.is_file()))
2272        .filter(|entry| detect_language(entry.path()).is_some())
2273        .map(|entry| entry.into_path())
2274}
2275
2276// ---------------------------------------------------------------------------
2277// Tests
2278// ---------------------------------------------------------------------------
2279
2280#[cfg(test)]
2281mod tests {
2282    use super::*;
2283    use std::fs;
2284    use tempfile::TempDir;
2285
2286    /// Create a temp directory with TypeScript files for testing.
2287    fn setup_ts_project() -> TempDir {
2288        let dir = TempDir::new().unwrap();
2289
2290        // main.ts: imports from utils and calls functions
2291        fs::write(
2292            dir.path().join("main.ts"),
2293            r#"import { helper, compute } from './utils';
2294import * as math from './math';
2295
2296export function main() {
2297    const a = helper(1);
2298    const b = compute(a, 2);
2299    const c = math.add(a, b);
2300    return c;
2301}
2302"#,
2303        )
2304        .unwrap();
2305
2306        // utils.ts: defines helper and compute, imports from helpers
2307        fs::write(
2308            dir.path().join("utils.ts"),
2309            r#"import { double } from './helpers';
2310
2311export function helper(x: number): number {
2312    return double(x);
2313}
2314
2315export function compute(a: number, b: number): number {
2316    return a + b;
2317}
2318"#,
2319        )
2320        .unwrap();
2321
2322        // helpers.ts: defines double
2323        fs::write(
2324            dir.path().join("helpers.ts"),
2325            r#"export function double(x: number): number {
2326    return x * 2;
2327}
2328
2329export function triple(x: number): number {
2330    return x * 3;
2331}
2332"#,
2333        )
2334        .unwrap();
2335
2336        // math.ts: defines add (for namespace import test)
2337        fs::write(
2338            dir.path().join("math.ts"),
2339            r#"export function add(a: number, b: number): number {
2340    return a + b;
2341}
2342
2343export function subtract(a: number, b: number): number {
2344    return a - b;
2345}
2346"#,
2347        )
2348        .unwrap();
2349
2350        dir
2351    }
2352
2353    /// Create a project with import aliasing.
2354    fn setup_alias_project() -> TempDir {
2355        let dir = TempDir::new().unwrap();
2356
2357        fs::write(
2358            dir.path().join("main.ts"),
2359            r#"import { helper as h } from './utils';
2360
2361export function main() {
2362    return h(42);
2363}
2364"#,
2365        )
2366        .unwrap();
2367
2368        fs::write(
2369            dir.path().join("utils.ts"),
2370            r#"export function helper(x: number): number {
2371    return x + 1;
2372}
2373"#,
2374        )
2375        .unwrap();
2376
2377        dir
2378    }
2379
2380    /// Create a project with a cycle: A → B → A.
2381    fn setup_cycle_project() -> TempDir {
2382        let dir = TempDir::new().unwrap();
2383
2384        fs::write(
2385            dir.path().join("a.ts"),
2386            r#"import { funcB } from './b';
2387
2388export function funcA() {
2389    return funcB();
2390}
2391"#,
2392        )
2393        .unwrap();
2394
2395        fs::write(
2396            dir.path().join("b.ts"),
2397            r#"import { funcA } from './a';
2398
2399export function funcB() {
2400    return funcA();
2401}
2402"#,
2403        )
2404        .unwrap();
2405
2406        dir
2407    }
2408
2409    // --- Single-file call extraction ---
2410
2411    #[test]
2412    fn callgraph_single_file_call_extraction() {
2413        let dir = setup_ts_project();
2414        let mut graph = CallGraph::new(dir.path().to_path_buf());
2415
2416        let file_data = graph.build_file(&dir.path().join("main.ts")).unwrap();
2417        let main_calls = &file_data.calls_by_symbol["main"];
2418
2419        let callee_names: Vec<&str> = main_calls.iter().map(|c| c.callee_name.as_str()).collect();
2420        assert!(
2421            callee_names.contains(&"helper"),
2422            "main should call helper, got: {:?}",
2423            callee_names
2424        );
2425        assert!(
2426            callee_names.contains(&"compute"),
2427            "main should call compute, got: {:?}",
2428            callee_names
2429        );
2430        assert!(
2431            callee_names.contains(&"add"),
2432            "main should call math.add (short name: add), got: {:?}",
2433            callee_names
2434        );
2435    }
2436
2437    #[test]
2438    fn callgraph_file_data_has_exports() {
2439        let dir = setup_ts_project();
2440        let mut graph = CallGraph::new(dir.path().to_path_buf());
2441
2442        let file_data = graph.build_file(&dir.path().join("utils.ts")).unwrap();
2443        assert!(
2444            file_data.exported_symbols.contains(&"helper".to_string()),
2445            "utils.ts should export helper, got: {:?}",
2446            file_data.exported_symbols
2447        );
2448        assert!(
2449            file_data.exported_symbols.contains(&"compute".to_string()),
2450            "utils.ts should export compute, got: {:?}",
2451            file_data.exported_symbols
2452        );
2453    }
2454
2455    // --- Cross-file resolution ---
2456
2457    #[test]
2458    fn callgraph_resolve_direct_import() {
2459        let dir = setup_ts_project();
2460        let mut graph = CallGraph::new(dir.path().to_path_buf());
2461
2462        let main_path = dir.path().join("main.ts");
2463        let file_data = graph.build_file(&main_path).unwrap();
2464        let import_block = file_data.import_block.clone();
2465
2466        let edge = graph.resolve_cross_file_edge("helper", "helper", &main_path, &import_block);
2467        match edge {
2468            EdgeResolution::Resolved { file, symbol } => {
2469                assert!(
2470                    file.ends_with("utils.ts"),
2471                    "helper should resolve to utils.ts, got: {:?}",
2472                    file
2473                );
2474                assert_eq!(symbol, "helper");
2475            }
2476            EdgeResolution::Unresolved { callee_name } => {
2477                panic!("Expected resolved, got unresolved: {}", callee_name);
2478            }
2479        }
2480    }
2481
2482    #[test]
2483    fn callgraph_resolve_namespace_import() {
2484        let dir = setup_ts_project();
2485        let mut graph = CallGraph::new(dir.path().to_path_buf());
2486
2487        let main_path = dir.path().join("main.ts");
2488        let file_data = graph.build_file(&main_path).unwrap();
2489        let import_block = file_data.import_block.clone();
2490
2491        let edge = graph.resolve_cross_file_edge("math.add", "add", &main_path, &import_block);
2492        match edge {
2493            EdgeResolution::Resolved { file, symbol } => {
2494                assert!(
2495                    file.ends_with("math.ts"),
2496                    "math.add should resolve to math.ts, got: {:?}",
2497                    file
2498                );
2499                assert_eq!(symbol, "add");
2500            }
2501            EdgeResolution::Unresolved { callee_name } => {
2502                panic!("Expected resolved, got unresolved: {}", callee_name);
2503            }
2504        }
2505    }
2506
2507    #[test]
2508    fn callgraph_resolve_aliased_import() {
2509        let dir = setup_alias_project();
2510        let mut graph = CallGraph::new(dir.path().to_path_buf());
2511
2512        let main_path = dir.path().join("main.ts");
2513        let file_data = graph.build_file(&main_path).unwrap();
2514        let import_block = file_data.import_block.clone();
2515
2516        let edge = graph.resolve_cross_file_edge("h", "h", &main_path, &import_block);
2517        match edge {
2518            EdgeResolution::Resolved { file, symbol } => {
2519                assert!(
2520                    file.ends_with("utils.ts"),
2521                    "h (alias for helper) should resolve to utils.ts, got: {:?}",
2522                    file
2523                );
2524                assert_eq!(symbol, "helper");
2525            }
2526            EdgeResolution::Unresolved { callee_name } => {
2527                panic!("Expected resolved, got unresolved: {}", callee_name);
2528            }
2529        }
2530    }
2531
2532    #[test]
2533    fn callgraph_unresolved_edge_marked() {
2534        let dir = setup_ts_project();
2535        let mut graph = CallGraph::new(dir.path().to_path_buf());
2536
2537        let main_path = dir.path().join("main.ts");
2538        let file_data = graph.build_file(&main_path).unwrap();
2539        let import_block = file_data.import_block.clone();
2540
2541        let edge =
2542            graph.resolve_cross_file_edge("unknownFunc", "unknownFunc", &main_path, &import_block);
2543        assert_eq!(
2544            edge,
2545            EdgeResolution::Unresolved {
2546                callee_name: "unknownFunc".to_string()
2547            },
2548            "Unknown callee should be unresolved"
2549        );
2550    }
2551
2552    // --- Cycle detection ---
2553
2554    #[test]
2555    fn callgraph_cycle_detection_stops() {
2556        let dir = setup_cycle_project();
2557        let mut graph = CallGraph::new(dir.path().to_path_buf());
2558
2559        // This should NOT infinite loop
2560        let tree = graph
2561            .forward_tree(&dir.path().join("a.ts"), "funcA", 10)
2562            .unwrap();
2563
2564        assert_eq!(tree.name, "funcA");
2565        assert!(tree.resolved);
2566
2567        // funcA calls funcB, funcB calls funcA (cycle), so the depth should be bounded
2568        // The tree should have children but not infinitely deep
2569        fn count_depth(node: &CallTreeNode) -> usize {
2570            if node.children.is_empty() {
2571                1
2572            } else {
2573                1 + node
2574                    .children
2575                    .iter()
2576                    .map(|c| count_depth(c))
2577                    .max()
2578                    .unwrap_or(0)
2579            }
2580        }
2581
2582        let depth = count_depth(&tree);
2583        assert!(
2584            depth <= 4,
2585            "Cycle should be detected and bounded, depth was: {}",
2586            depth
2587        );
2588    }
2589
2590    // --- Depth limiting ---
2591
2592    #[test]
2593    fn callgraph_depth_limit_truncates() {
2594        let dir = setup_ts_project();
2595        let mut graph = CallGraph::new(dir.path().to_path_buf());
2596
2597        // main → helper → double, main → compute
2598        // With depth 1, we should see direct callees but not their children
2599        let tree = graph
2600            .forward_tree(&dir.path().join("main.ts"), "main", 1)
2601            .unwrap();
2602
2603        assert_eq!(tree.name, "main");
2604
2605        // At depth 1, children should exist (direct calls) but their children should be empty
2606        for child in &tree.children {
2607            assert!(
2608                child.children.is_empty(),
2609                "At depth 1, child '{}' should have no children, got {:?}",
2610                child.name,
2611                child.children.len()
2612            );
2613        }
2614    }
2615
2616    #[test]
2617    fn callgraph_depth_zero_no_children() {
2618        let dir = setup_ts_project();
2619        let mut graph = CallGraph::new(dir.path().to_path_buf());
2620
2621        let tree = graph
2622            .forward_tree(&dir.path().join("main.ts"), "main", 0)
2623            .unwrap();
2624
2625        assert_eq!(tree.name, "main");
2626        assert!(
2627            tree.children.is_empty(),
2628            "At depth 0, should have no children"
2629        );
2630    }
2631
2632    // --- Forward tree cross-file ---
2633
2634    #[test]
2635    fn callgraph_forward_tree_cross_file() {
2636        let dir = setup_ts_project();
2637        let mut graph = CallGraph::new(dir.path().to_path_buf());
2638
2639        // main → helper (in utils.ts) → double (in helpers.ts)
2640        let tree = graph
2641            .forward_tree(&dir.path().join("main.ts"), "main", 5)
2642            .unwrap();
2643
2644        assert_eq!(tree.name, "main");
2645        assert!(tree.resolved);
2646
2647        // Find the helper child
2648        let helper_child = tree.children.iter().find(|c| c.name == "helper");
2649        assert!(
2650            helper_child.is_some(),
2651            "main should have helper as child, children: {:?}",
2652            tree.children.iter().map(|c| &c.name).collect::<Vec<_>>()
2653        );
2654
2655        let helper = helper_child.unwrap();
2656        assert!(
2657            helper.file.ends_with("utils.ts") || helper.file == "utils.ts",
2658            "helper should be in utils.ts, got: {}",
2659            helper.file
2660        );
2661
2662        // helper should call double (in helpers.ts)
2663        let double_child = helper.children.iter().find(|c| c.name == "double");
2664        assert!(
2665            double_child.is_some(),
2666            "helper should call double, children: {:?}",
2667            helper.children.iter().map(|c| &c.name).collect::<Vec<_>>()
2668        );
2669
2670        let double = double_child.unwrap();
2671        assert!(
2672            double.file.ends_with("helpers.ts") || double.file == "helpers.ts",
2673            "double should be in helpers.ts, got: {}",
2674            double.file
2675        );
2676    }
2677
2678    // --- Worktree walker ---
2679
2680    #[test]
2681    fn callgraph_walker_excludes_gitignored() {
2682        let dir = TempDir::new().unwrap();
2683
2684        // Create a .gitignore
2685        fs::write(dir.path().join(".gitignore"), "ignored_dir/\n").unwrap();
2686
2687        // Create files
2688        fs::write(dir.path().join("main.ts"), "export function main() {}").unwrap();
2689        fs::create_dir(dir.path().join("ignored_dir")).unwrap();
2690        fs::write(
2691            dir.path().join("ignored_dir").join("secret.ts"),
2692            "export function secret() {}",
2693        )
2694        .unwrap();
2695
2696        // Also create node_modules (should always be excluded)
2697        fs::create_dir(dir.path().join("node_modules")).unwrap();
2698        fs::write(
2699            dir.path().join("node_modules").join("dep.ts"),
2700            "export function dep() {}",
2701        )
2702        .unwrap();
2703
2704        // Init git repo for .gitignore to work
2705        std::process::Command::new("git")
2706            .args(["init"])
2707            .current_dir(dir.path())
2708            .output()
2709            .unwrap();
2710
2711        let files: Vec<PathBuf> = walk_project_files(dir.path()).collect();
2712        let file_names: Vec<String> = files
2713            .iter()
2714            .map(|f| f.file_name().unwrap().to_string_lossy().to_string())
2715            .collect();
2716
2717        assert!(
2718            file_names.contains(&"main.ts".to_string()),
2719            "Should include main.ts, got: {:?}",
2720            file_names
2721        );
2722        assert!(
2723            !file_names.contains(&"secret.ts".to_string()),
2724            "Should exclude gitignored secret.ts, got: {:?}",
2725            file_names
2726        );
2727        assert!(
2728            !file_names.contains(&"dep.ts".to_string()),
2729            "Should exclude node_modules, got: {:?}",
2730            file_names
2731        );
2732    }
2733
2734    #[test]
2735    fn callgraph_walker_only_source_files() {
2736        let dir = TempDir::new().unwrap();
2737
2738        fs::write(dir.path().join("main.ts"), "export function main() {}").unwrap();
2739        fs::write(dir.path().join("readme.md"), "# Hello").unwrap();
2740        fs::write(dir.path().join("data.json"), "{}").unwrap();
2741
2742        let files: Vec<PathBuf> = walk_project_files(dir.path()).collect();
2743        let file_names: Vec<String> = files
2744            .iter()
2745            .map(|f| f.file_name().unwrap().to_string_lossy().to_string())
2746            .collect();
2747
2748        assert!(file_names.contains(&"main.ts".to_string()));
2749        assert!(
2750            file_names.contains(&"readme.md".to_string()),
2751            "Markdown is now a supported source language"
2752        );
2753        assert!(
2754            !file_names.contains(&"data.json".to_string()),
2755            "Should not include non-source files"
2756        );
2757    }
2758
2759    // --- find_alias_original ---
2760
2761    #[test]
2762    fn callgraph_find_alias_original_simple() {
2763        let raw = "import { foo as bar } from './utils';";
2764        assert_eq!(find_alias_original(raw, "bar"), Some("foo".to_string()));
2765    }
2766
2767    #[test]
2768    fn callgraph_find_alias_original_multiple() {
2769        let raw = "import { foo as bar, baz as qux } from './utils';";
2770        assert_eq!(find_alias_original(raw, "bar"), Some("foo".to_string()));
2771        assert_eq!(find_alias_original(raw, "qux"), Some("baz".to_string()));
2772    }
2773
2774    #[test]
2775    fn callgraph_find_alias_no_match() {
2776        let raw = "import { foo } from './utils';";
2777        assert_eq!(find_alias_original(raw, "foo"), None);
2778    }
2779
2780    // --- Reverse callers ---
2781
2782    #[test]
2783    fn callgraph_callers_of_direct() {
2784        let dir = setup_ts_project();
2785        let mut graph = CallGraph::new(dir.path().to_path_buf());
2786
2787        // helpers.ts:double is called by utils.ts:helper
2788        let result = graph
2789            .callers_of(&dir.path().join("helpers.ts"), "double", 1)
2790            .unwrap();
2791
2792        assert_eq!(result.symbol, "double");
2793        assert!(result.total_callers > 0, "double should have callers");
2794        assert!(result.scanned_files > 0, "should have scanned files");
2795
2796        // Find the caller from utils.ts
2797        let utils_group = result.callers.iter().find(|g| g.file.contains("utils.ts"));
2798        assert!(
2799            utils_group.is_some(),
2800            "double should be called from utils.ts, groups: {:?}",
2801            result.callers.iter().map(|g| &g.file).collect::<Vec<_>>()
2802        );
2803
2804        let group = utils_group.unwrap();
2805        let helper_caller = group.callers.iter().find(|c| c.symbol == "helper");
2806        assert!(
2807            helper_caller.is_some(),
2808            "double should be called by helper, callers: {:?}",
2809            group.callers.iter().map(|c| &c.symbol).collect::<Vec<_>>()
2810        );
2811    }
2812
2813    #[test]
2814    fn callgraph_callers_of_no_callers() {
2815        let dir = setup_ts_project();
2816        let mut graph = CallGraph::new(dir.path().to_path_buf());
2817
2818        // main.ts:main is the entry point — nothing calls it
2819        let result = graph
2820            .callers_of(&dir.path().join("main.ts"), "main", 1)
2821            .unwrap();
2822
2823        assert_eq!(result.symbol, "main");
2824        assert_eq!(result.total_callers, 0, "main should have no callers");
2825        assert!(result.callers.is_empty());
2826    }
2827
2828    #[test]
2829    fn callgraph_callers_recursive_depth() {
2830        let dir = setup_ts_project();
2831        let mut graph = CallGraph::new(dir.path().to_path_buf());
2832
2833        // helpers.ts:double is called by utils.ts:helper
2834        // utils.ts:helper is called by main.ts:main
2835        // With depth=2, we should see both direct and transitive callers
2836        let result = graph
2837            .callers_of(&dir.path().join("helpers.ts"), "double", 2)
2838            .unwrap();
2839
2840        assert!(
2841            result.total_callers >= 2,
2842            "with depth 2, double should have >= 2 callers (direct + transitive), got {}",
2843            result.total_callers
2844        );
2845
2846        // Should include caller from main.ts (transitive: main → helper → double)
2847        let main_group = result.callers.iter().find(|g| g.file.contains("main.ts"));
2848        assert!(
2849            main_group.is_some(),
2850            "recursive callers should include main.ts, groups: {:?}",
2851            result.callers.iter().map(|g| &g.file).collect::<Vec<_>>()
2852        );
2853    }
2854
2855    #[test]
2856    fn callgraph_invalidate_file_clears_reverse_index() {
2857        let dir = setup_ts_project();
2858        let mut graph = CallGraph::new(dir.path().to_path_buf());
2859
2860        // Build callers to populate the reverse index
2861        let _ = graph
2862            .callers_of(&dir.path().join("helpers.ts"), "double", 1)
2863            .unwrap();
2864        assert!(
2865            graph.reverse_index.is_some(),
2866            "reverse index should be built"
2867        );
2868
2869        // Invalidate a file
2870        graph.invalidate_file(&dir.path().join("utils.ts"));
2871
2872        // Reverse index should be cleared
2873        assert!(
2874            graph.reverse_index.is_none(),
2875            "invalidate_file should clear reverse index"
2876        );
2877        // Data cache for the file should be cleared
2878        let canon = std::fs::canonicalize(dir.path().join("utils.ts")).unwrap();
2879        assert!(
2880            !graph.data.contains_key(&canon),
2881            "invalidate_file should remove file from data cache"
2882        );
2883        // Project files should be cleared
2884        assert!(
2885            graph.project_files.is_none(),
2886            "invalidate_file should clear project_files"
2887        );
2888    }
2889
2890    // --- is_entry_point ---
2891
2892    #[test]
2893    fn is_entry_point_exported_function() {
2894        assert!(is_entry_point(
2895            "handleRequest",
2896            &SymbolKind::Function,
2897            true,
2898            LangId::TypeScript
2899        ));
2900    }
2901
2902    #[test]
2903    fn is_entry_point_exported_method_is_not_entry() {
2904        // Methods are class members, not standalone entry points
2905        assert!(!is_entry_point(
2906            "handleRequest",
2907            &SymbolKind::Method,
2908            true,
2909            LangId::TypeScript
2910        ));
2911    }
2912
2913    #[test]
2914    fn is_entry_point_main_init_patterns() {
2915        for name in &["main", "Main", "MAIN", "init", "setup", "bootstrap", "run"] {
2916            assert!(
2917                is_entry_point(name, &SymbolKind::Function, false, LangId::TypeScript),
2918                "{} should be an entry point",
2919                name
2920            );
2921        }
2922    }
2923
2924    #[test]
2925    fn is_entry_point_test_patterns_ts() {
2926        assert!(is_entry_point(
2927            "describe",
2928            &SymbolKind::Function,
2929            false,
2930            LangId::TypeScript
2931        ));
2932        assert!(is_entry_point(
2933            "it",
2934            &SymbolKind::Function,
2935            false,
2936            LangId::TypeScript
2937        ));
2938        assert!(is_entry_point(
2939            "test",
2940            &SymbolKind::Function,
2941            false,
2942            LangId::TypeScript
2943        ));
2944        assert!(is_entry_point(
2945            "testValidation",
2946            &SymbolKind::Function,
2947            false,
2948            LangId::TypeScript
2949        ));
2950        assert!(is_entry_point(
2951            "specHelper",
2952            &SymbolKind::Function,
2953            false,
2954            LangId::TypeScript
2955        ));
2956    }
2957
2958    #[test]
2959    fn is_entry_point_test_patterns_python() {
2960        assert!(is_entry_point(
2961            "test_login",
2962            &SymbolKind::Function,
2963            false,
2964            LangId::Python
2965        ));
2966        assert!(is_entry_point(
2967            "setUp",
2968            &SymbolKind::Function,
2969            false,
2970            LangId::Python
2971        ));
2972        assert!(is_entry_point(
2973            "tearDown",
2974            &SymbolKind::Function,
2975            false,
2976            LangId::Python
2977        ));
2978        // "testSomething" should NOT match Python (needs test_ prefix)
2979        assert!(!is_entry_point(
2980            "testSomething",
2981            &SymbolKind::Function,
2982            false,
2983            LangId::Python
2984        ));
2985    }
2986
2987    #[test]
2988    fn is_entry_point_test_patterns_rust() {
2989        assert!(is_entry_point(
2990            "test_parse",
2991            &SymbolKind::Function,
2992            false,
2993            LangId::Rust
2994        ));
2995        assert!(!is_entry_point(
2996            "TestSomething",
2997            &SymbolKind::Function,
2998            false,
2999            LangId::Rust
3000        ));
3001    }
3002
3003    #[test]
3004    fn is_entry_point_test_patterns_go() {
3005        assert!(is_entry_point(
3006            "TestParsing",
3007            &SymbolKind::Function,
3008            false,
3009            LangId::Go
3010        ));
3011        // lowercase test should NOT match Go (needs uppercase Test prefix)
3012        assert!(!is_entry_point(
3013            "testParsing",
3014            &SymbolKind::Function,
3015            false,
3016            LangId::Go
3017        ));
3018    }
3019
3020    #[test]
3021    fn is_entry_point_non_exported_non_main_is_not_entry() {
3022        assert!(!is_entry_point(
3023            "helperUtil",
3024            &SymbolKind::Function,
3025            false,
3026            LangId::TypeScript
3027        ));
3028    }
3029
3030    // --- symbol_metadata ---
3031
3032    #[test]
3033    fn callgraph_symbol_metadata_populated() {
3034        let dir = setup_ts_project();
3035        let mut graph = CallGraph::new(dir.path().to_path_buf());
3036
3037        let file_data = graph.build_file(&dir.path().join("utils.ts")).unwrap();
3038        assert!(
3039            file_data.symbol_metadata.contains_key("helper"),
3040            "symbol_metadata should contain helper"
3041        );
3042        let meta = &file_data.symbol_metadata["helper"];
3043        assert_eq!(meta.kind, SymbolKind::Function);
3044        assert!(meta.exported, "helper should be exported");
3045    }
3046
3047    // --- trace_to ---
3048
3049    /// Setup a multi-path project for trace_to tests.
3050    ///
3051    /// Structure:
3052    ///   main.ts: exported main() → processData (from utils)
3053    ///   service.ts: exported handleRequest() → processData (from utils)
3054    ///   utils.ts: exported processData() → validate (from helpers)
3055    ///   helpers.ts: exported validate() → checkFormat (local, not exported)
3056    ///   test_helpers.ts: testValidation() → validate (from helpers)
3057    ///
3058    /// checkFormat should have 3 paths:
3059    ///   main → processData → validate → checkFormat
3060    ///   handleRequest → processData → validate → checkFormat
3061    ///   testValidation → validate → checkFormat
3062    fn setup_trace_project() -> TempDir {
3063        let dir = TempDir::new().unwrap();
3064
3065        fs::write(
3066            dir.path().join("main.ts"),
3067            r#"import { processData } from './utils';
3068
3069export function main() {
3070    const result = processData("hello");
3071    return result;
3072}
3073"#,
3074        )
3075        .unwrap();
3076
3077        fs::write(
3078            dir.path().join("service.ts"),
3079            r#"import { processData } from './utils';
3080
3081export function handleRequest(input: string): string {
3082    return processData(input);
3083}
3084"#,
3085        )
3086        .unwrap();
3087
3088        fs::write(
3089            dir.path().join("utils.ts"),
3090            r#"import { validate } from './helpers';
3091
3092export function processData(input: string): string {
3093    const valid = validate(input);
3094    if (!valid) {
3095        throw new Error("invalid input");
3096    }
3097    return input.toUpperCase();
3098}
3099"#,
3100        )
3101        .unwrap();
3102
3103        fs::write(
3104            dir.path().join("helpers.ts"),
3105            r#"export function validate(input: string): boolean {
3106    return checkFormat(input);
3107}
3108
3109function checkFormat(input: string): boolean {
3110    return input.length > 0 && /^[a-zA-Z]+$/.test(input);
3111}
3112"#,
3113        )
3114        .unwrap();
3115
3116        fs::write(
3117            dir.path().join("test_helpers.ts"),
3118            r#"import { validate } from './helpers';
3119
3120function testValidation() {
3121    const result = validate("hello");
3122    console.log(result);
3123}
3124"#,
3125        )
3126        .unwrap();
3127
3128        // git init so the walker works
3129        std::process::Command::new("git")
3130            .args(["init"])
3131            .current_dir(dir.path())
3132            .output()
3133            .unwrap();
3134
3135        dir
3136    }
3137
3138    #[test]
3139    fn trace_to_multi_path() {
3140        let dir = setup_trace_project();
3141        let mut graph = CallGraph::new(dir.path().to_path_buf());
3142
3143        let result = graph
3144            .trace_to(&dir.path().join("helpers.ts"), "checkFormat", 10)
3145            .unwrap();
3146
3147        assert_eq!(result.target_symbol, "checkFormat");
3148        assert!(
3149            result.total_paths >= 2,
3150            "checkFormat should have at least 2 paths, got {} (paths: {:?})",
3151            result.total_paths,
3152            result
3153                .paths
3154                .iter()
3155                .map(|p| p.hops.iter().map(|h| h.symbol.as_str()).collect::<Vec<_>>())
3156                .collect::<Vec<_>>()
3157        );
3158
3159        // Check that paths are top-down: entry point first, target last
3160        for path in &result.paths {
3161            assert!(
3162                path.hops.first().unwrap().is_entry_point,
3163                "First hop should be an entry point, got: {}",
3164                path.hops.first().unwrap().symbol
3165            );
3166            assert_eq!(
3167                path.hops.last().unwrap().symbol,
3168                "checkFormat",
3169                "Last hop should be checkFormat"
3170            );
3171        }
3172
3173        // Verify entry_points_found > 0
3174        assert!(
3175            result.entry_points_found >= 2,
3176            "should find at least 2 entry points, got {}",
3177            result.entry_points_found
3178        );
3179    }
3180
3181    #[test]
3182    fn trace_to_single_path() {
3183        let dir = setup_trace_project();
3184        let mut graph = CallGraph::new(dir.path().to_path_buf());
3185
3186        // validate is called from processData, testValidation
3187        // processData is called from main, handleRequest
3188        // So validate has paths: main→processData→validate, handleRequest→processData→validate, testValidation→validate
3189        let result = graph
3190            .trace_to(&dir.path().join("helpers.ts"), "validate", 10)
3191            .unwrap();
3192
3193        assert_eq!(result.target_symbol, "validate");
3194        assert!(
3195            result.total_paths >= 2,
3196            "validate should have at least 2 paths, got {}",
3197            result.total_paths
3198        );
3199    }
3200
3201    #[test]
3202    fn trace_to_cycle_detection() {
3203        let dir = setup_cycle_project();
3204        let mut graph = CallGraph::new(dir.path().to_path_buf());
3205
3206        // funcA ↔ funcB cycle — should terminate
3207        let result = graph
3208            .trace_to(&dir.path().join("a.ts"), "funcA", 10)
3209            .unwrap();
3210
3211        // Should not hang — the fact we got here means cycle detection works
3212        assert_eq!(result.target_symbol, "funcA");
3213    }
3214
3215    #[test]
3216    fn trace_to_depth_limit() {
3217        let dir = setup_trace_project();
3218        let mut graph = CallGraph::new(dir.path().to_path_buf());
3219
3220        // With max_depth=1, should not be able to reach entry points that are 3+ hops away
3221        let result = graph
3222            .trace_to(&dir.path().join("helpers.ts"), "checkFormat", 1)
3223            .unwrap();
3224
3225        // testValidation→validate→checkFormat is 2 hops, which requires depth >= 2
3226        // main→processData→validate→checkFormat is 3 hops, which requires depth >= 3
3227        // With depth=1, most paths should be truncated
3228        assert_eq!(result.target_symbol, "checkFormat");
3229
3230        // The shallow result should have fewer paths than the deep one
3231        let deep_result = graph
3232            .trace_to(&dir.path().join("helpers.ts"), "checkFormat", 10)
3233            .unwrap();
3234
3235        assert!(
3236            result.total_paths <= deep_result.total_paths,
3237            "shallow trace should find <= paths compared to deep: {} vs {}",
3238            result.total_paths,
3239            deep_result.total_paths
3240        );
3241    }
3242
3243    #[test]
3244    fn trace_to_entry_point_target() {
3245        let dir = setup_trace_project();
3246        let mut graph = CallGraph::new(dir.path().to_path_buf());
3247
3248        // main is itself an entry point — should return a single trivial path
3249        let result = graph
3250            .trace_to(&dir.path().join("main.ts"), "main", 10)
3251            .unwrap();
3252
3253        assert_eq!(result.target_symbol, "main");
3254        assert!(
3255            result.total_paths >= 1,
3256            "main should have at least 1 path (itself), got {}",
3257            result.total_paths
3258        );
3259        // Check the trivial path has just one hop
3260        let trivial = result.paths.iter().find(|p| p.hops.len() == 1);
3261        assert!(
3262            trivial.is_some(),
3263            "should have a trivial path with just the entry point itself"
3264        );
3265    }
3266
3267    // --- extract_parameters ---
3268
3269    #[test]
3270    fn extract_parameters_typescript() {
3271        let params = extract_parameters(
3272            "function processData(input: string, count: number): void",
3273            LangId::TypeScript,
3274        );
3275        assert_eq!(params, vec!["input", "count"]);
3276    }
3277
3278    #[test]
3279    fn extract_parameters_typescript_optional() {
3280        let params = extract_parameters(
3281            "function fetch(url: string, options?: RequestInit): Promise<Response>",
3282            LangId::TypeScript,
3283        );
3284        assert_eq!(params, vec!["url", "options"]);
3285    }
3286
3287    #[test]
3288    fn extract_parameters_typescript_defaults() {
3289        let params = extract_parameters(
3290            "function greet(name: string, greeting: string = \"hello\"): string",
3291            LangId::TypeScript,
3292        );
3293        assert_eq!(params, vec!["name", "greeting"]);
3294    }
3295
3296    #[test]
3297    fn extract_parameters_typescript_rest() {
3298        let params = extract_parameters(
3299            "function sum(...numbers: number[]): number",
3300            LangId::TypeScript,
3301        );
3302        assert_eq!(params, vec!["numbers"]);
3303    }
3304
3305    #[test]
3306    fn extract_parameters_python_self_skipped() {
3307        let params = extract_parameters(
3308            "def process(self, data: str, count: int) -> bool",
3309            LangId::Python,
3310        );
3311        assert_eq!(params, vec!["data", "count"]);
3312    }
3313
3314    #[test]
3315    fn extract_parameters_python_no_self() {
3316        let params = extract_parameters("def validate(input: str) -> bool", LangId::Python);
3317        assert_eq!(params, vec!["input"]);
3318    }
3319
3320    #[test]
3321    fn extract_parameters_python_star_args() {
3322        let params = extract_parameters("def func(*args, **kwargs)", LangId::Python);
3323        assert_eq!(params, vec!["args", "kwargs"]);
3324    }
3325
3326    #[test]
3327    fn extract_parameters_rust_self_skipped() {
3328        let params = extract_parameters(
3329            "fn process(&self, data: &str, count: usize) -> bool",
3330            LangId::Rust,
3331        );
3332        assert_eq!(params, vec!["data", "count"]);
3333    }
3334
3335    #[test]
3336    fn extract_parameters_rust_mut_self_skipped() {
3337        let params = extract_parameters("fn update(&mut self, value: i32)", LangId::Rust);
3338        assert_eq!(params, vec!["value"]);
3339    }
3340
3341    #[test]
3342    fn extract_parameters_rust_no_self() {
3343        let params = extract_parameters("fn validate(input: &str) -> bool", LangId::Rust);
3344        assert_eq!(params, vec!["input"]);
3345    }
3346
3347    #[test]
3348    fn extract_parameters_rust_mut_param() {
3349        let params = extract_parameters("fn process(mut buf: Vec<u8>, len: usize)", LangId::Rust);
3350        assert_eq!(params, vec!["buf", "len"]);
3351    }
3352
3353    #[test]
3354    fn extract_parameters_go() {
3355        let params = extract_parameters(
3356            "func ProcessData(input string, count int) error",
3357            LangId::Go,
3358        );
3359        assert_eq!(params, vec!["input", "count"]);
3360    }
3361
3362    #[test]
3363    fn extract_parameters_empty() {
3364        let params = extract_parameters("function noArgs(): void", LangId::TypeScript);
3365        assert!(
3366            params.is_empty(),
3367            "no-arg function should return empty params"
3368        );
3369    }
3370
3371    #[test]
3372    fn extract_parameters_no_parens() {
3373        let params = extract_parameters("const x = 42", LangId::TypeScript);
3374        assert!(params.is_empty(), "no parens should return empty params");
3375    }
3376
3377    #[test]
3378    fn extract_parameters_javascript() {
3379        let params = extract_parameters("function handleClick(event, target)", LangId::JavaScript);
3380        assert_eq!(params, vec!["event", "target"]);
3381    }
3382}