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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
use std::collections::{HashMap, HashSet};
use std::path::Path;
use crate::parser::{EdgeDef, EdgeKind, NodeDef, NodeKind};
/// Returns true if the given relative file path looks like a test file.
pub fn is_test_path(path: &str) -> bool {
let lower = path.to_lowercase();
lower.contains("/test/")
|| lower.contains("/tests/")
|| lower.contains("/__tests__/")
|| lower.contains("/spec/")
|| lower.ends_with(".test.ts")
|| lower.ends_with(".spec.ts")
|| lower.ends_with(".test.js")
|| lower.ends_with(".spec.js")
|| lower.ends_with(".test.tsx")
|| lower.ends_with(".spec.tsx")
|| lower.ends_with("_test.py")
|| lower.ends_with("_test.rs")
}
/// Resolve raw parser edges into fully-qualified node IDs.
///
/// Import paths and call targets are matched against the known node set.
/// `CALLS` edges originating from test files are reclassified as `TESTS`
/// when the destination is a production symbol. Unresolvable edges are
/// kept so later analysis phases can still use them.
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();
// Build a set of test-file paths for CALLS→TESTS reclassification
let test_paths: HashSet<&str> = nodes
.iter()
.filter(|n| is_test_path(&n.path))
.map(|n| n.path.as_str())
.collect();
// Also test source IDs (fn:/cls: nodes whose path is a test file)
let test_node_ids: HashSet<&str> = nodes
.iter()
.filter(|n| is_test_path(&n.path))
.map(|n| n.id.as_str())
.collect();
let _ = test_paths; // used via test_node_ids
// 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 source node is from a test file and destination is not, use TESTS edge
let src_is_test = test_node_ids.contains(edge.src.as_str());
// If dst is a valid node ID, keep it. Otherwise try to resolve by name.
if node_ids.contains(edge.dst.as_str()) {
let dst_is_test = test_node_ids.contains(edge.dst.as_str());
let kind = if src_is_test && !dst_is_test {
EdgeKind::Tests
} else {
edge.kind.clone()
};
resolved_edges.push(EdgeDef {
kind,
..edge.clone()
});
} else if let Some(targets) = export_index.get(&edge.dst) {
// Found matching names - create CALLS (or TESTS) edges with lower confidence
for target_id in targets {
let dst_is_test = test_node_ids.contains(target_id.as_str());
let kind = if src_is_test && !dst_is_test {
EdgeKind::Tests
} else {
EdgeKind::Calls
};
resolved_edges.push(EdgeDef {
src: edge.src.clone(),
dst: target_id.clone(),
kind,
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)
}
/// Create `File` [`NodeDef`]s for every path in `file_paths`.
///
/// These synthetic nodes are added to the graph so that `IMPORTS` edges
/// always have a valid destination, even for files that contain no parseable symbols.
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
}
/// Build a `file_path → language` lookup from a slice of parsed nodes.
///
/// Function and class nodes take priority over file nodes so the inferred
/// language is as accurate as possible.
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
}