1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
use std::collections::{HashMap, HashSet};
use std::path::Path;
use crate::parser::{EdgeDef, EdgeKind, NodeDef, NodeKind};
pub fn resolve(
nodes: &[NodeDef],
edges: &[EdgeDef],
_repo_root: &Path,
) -> anyhow::Result<Vec<EdgeDef>> {
let mut resolved_edges: Vec<EdgeDef> = Vec::new();
// Create a set of all known node IDs for validation
let node_ids: HashSet<&str> = nodes.iter().map(|n| n.id.as_str()).collect();
// Build export index: name -> Vec<node_id>
let mut export_index: HashMap<String, Vec<String>> = HashMap::new();
for node in nodes {
export_index
.entry(node.name.clone())
.or_default()
.push(node.id.clone());
}
// Build file node set from actual files
let mut file_paths: HashSet<String> = HashSet::new();
for node in nodes {
file_paths.insert(node.path.clone());
}
// Create file node IDs we know about
let known_file_ids: HashSet<String> = file_paths
.iter()
.map(|p| format!("file:{}", p))
.collect();
// Process all edges
for edge in edges {
match edge.kind {
EdgeKind::Imports => {
// src = file:<current_file>, dst = file:<imported_file>
// Check if dst is a valid file ID, or try to resolve it
let dst_is_valid = node_ids.contains(edge.dst.as_str())
|| known_file_ids.contains(&edge.dst);
if dst_is_valid {
resolved_edges.push(EdgeDef {
src: edge.src.clone(),
dst: edge.dst.clone(),
kind: EdgeKind::Imports,
..Default::default()
});
} else {
// Try with different extensions
let import_target = edge.dst.trim_start_matches("file:");
let mut found = false;
for ext in &[".ts", ".tsx", ".js", ".jsx", ".py", ".rs"] {
let alt = format!("file:{}{}", import_target, ext);
if known_file_ids.contains(&alt) {
resolved_edges.push(EdgeDef {
src: edge.src.clone(),
dst: alt,
kind: EdgeKind::Imports,
..Default::default()
});
found = true;
break;
}
}
// Try directory index files (Node.js resolution)
if !found {
for index in &["/index.js", "/index.ts", "/index.jsx", "/index.tsx"] {
let alt = format!("file:{}{}", import_target, index);
if known_file_ids.contains(&alt) {
resolved_edges.push(EdgeDef {
src: edge.src.clone(),
dst: alt,
kind: EdgeKind::Imports,
..Default::default()
});
found = true;
break;
}
}
}
if !found {
// Include unresolvable imports too (they may still be useful)
resolved_edges.push(edge.clone());
}
}
}
EdgeKind::Exports => {
// src = file:<path>, dst = fn:path:name or cls:path:name
if node_ids.contains(edge.dst.as_str()) {
resolved_edges.push(edge.clone());
} else {
// Keep the edge but log a warning — may be resolved in a later pass
tracing::debug!("Unresolved export edge: {} -> {}", edge.src, edge.dst);
resolved_edges.push(edge.clone());
}
}
EdgeKind::Calls | EdgeKind::Inherits => {
// If dst is a valid node ID, keep it. Otherwise try to resolve by name.
if node_ids.contains(edge.dst.as_str()) {
resolved_edges.push(edge.clone());
} else if let Some(targets) = export_index.get(&edge.dst) {
// Found matching names - create CALLS edges with lower confidence
for target_id in targets {
resolved_edges.push(EdgeDef {
src: edge.src.clone(),
dst: target_id.clone(),
kind: EdgeKind::Calls,
confidence: 0.8,
..Default::default()
});
}
} else {
// Keep the edge even if unresolved (maybe a future phase can handle)
resolved_edges.push(edge.clone());
}
}
_ => {
// CoChanges, Owns, DependsOn — pass through unchanged
resolved_edges.push(edge.clone());
}
}
}
Ok(resolved_edges)
}
pub fn create_file_nodes(
file_paths: &HashSet<String>,
language: &HashMap<String, &str>,
) -> Vec<NodeDef> {
let mut nodes = Vec::new();
for path in file_paths {
let id = format!("file:{}", path);
let _lang = language
.get(path.as_str())
.copied()
.unwrap_or("unknown");
nodes.push(NodeDef {
id,
kind: NodeKind::File,
name: path.clone(),
path: path.clone(),
line_start: 1,
line_end: 1,
..Default::default()
});
}
nodes
}
pub fn build_language_map(
nodes: &[NodeDef],
) -> HashMap<String, &'static str> {
let mut map = HashMap::new();
for node in nodes {
let lang = match node.id.split(':').next().unwrap_or("") {
"fn" if node.path.ends_with(".ts") || node.path.ends_with(".tsx") => "typescript",
"fn" if node.path.ends_with(".js") || node.path.ends_with(".jsx") => "javascript",
"fn" if node.path.ends_with(".py") => "python",
"fn" if node.path.ends_with(".rs") => "rust",
"cls" if node.path.ends_with(".ts") || node.path.ends_with(".tsx") => "typescript",
"cls" if node.path.ends_with(".js") || node.path.ends_with(".jsx") => "javascript",
"cls" if node.path.ends_with(".py") => "python",
"cls" if node.path.ends_with(".rs") => "rust",
"file" if node.path.ends_with(".ts") || node.path.ends_with(".tsx") => "typescript",
"file" if node.path.ends_with(".js") || node.path.ends_with(".jsx") => "javascript",
"file" if node.path.ends_with(".py") => "python",
"file" if node.path.ends_with(".rs") => "rust",
_ => "unknown",
};
// Only insert if not already present (function/class nodes take priority)
map.entry(node.path.clone()).or_insert(lang);
}
map
}