Skip to content
This repository was archived by the owner on Sep 9, 2025. It is now read-only.

Commit baadf19

Browse files
author
Hendrik van Antwerpen
committed
Change module path encoding to support root dirs
1 parent b9f121f commit baadf19

File tree

5 files changed

+177
-236
lines changed

5 files changed

+177
-236
lines changed

languages/tree-sitter-stack-graphs-typescript/rust/tsconfig.rs

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ use stack_graphs::graph::StackGraph;
1919
use tree_sitter_stack_graphs::FileAnalyzer;
2020
use tree_sitter_stack_graphs::LoadError;
2121

22+
const PACKAGE_NAME: &str = ""; // FIXME should be a parameter to TsConfigAnalyzer
23+
const PROJECT_NAME: &str = ""; // FIXME should be a parameter to TsConfigAnalyzer
24+
25+
const M_NS: &str = "%M";
26+
const NON_REL_M_NS: &str = "%NonRelM";
27+
const PROJ_NS: &str = "%Proj";
28+
const REL_M_NS: &str = "%RelM";
29+
2230
pub struct TsConfigAnalyzer {}
2331

2432
impl FileAnalyzer for TsConfigAnalyzer {
@@ -45,37 +53,42 @@ impl FileAnalyzer for TsConfigAnalyzer {
4553
self.add_debug_name(graph, proj_scope, "proj_scope");
4654

4755
// project definition
48-
let proj_def = self.add_pop(graph, root, "%Proj", &mut ids);
56+
let proj_def = self.add_ns_pop(graph, root, PROJ_NS, PROJECT_NAME, &mut ids);
4957
self.add_debug_name(graph, proj_def, "proj_def");
50-
// TODO: add actual project name
5158
self.add_edge(graph, proj_def, proj_scope, 0);
5259

5360
// project reference
54-
let proj_ref = self.add_push(graph, root, "%Proj", &mut ids);
61+
let proj_ref = self.add_ns_push(graph, root, PROJ_NS, PROJECT_NAME, &mut ids);
5562
self.add_debug_name(graph, proj_ref, "proj_ref");
56-
// TODO: add actual project name
5763
self.add_edge(graph, proj_scope, proj_ref, 0);
5864

5965
// root directory
6066
let root_dir_ref =
61-
self.add_module_pushes(graph, &tsc.root_dir(all_paths), proj_scope, &mut ids);
67+
self.add_module_pushes(graph, M_NS, &tsc.root_dir(all_paths), proj_scope, &mut ids);
6268
self.add_debug_name(graph, root_dir_ref, "root_dir.ref");
63-
self.add_edge(graph, root_dir_ref, proj_scope, 0);
6469

65-
// auxiliary root directories
70+
// package definition
71+
let pkg_def =
72+
self.add_module_pops(graph, NON_REL_M_NS, Path::new(PACKAGE_NAME), root, &mut ids);
73+
self.add_debug_name(graph, pkg_def, "pkg_def");
74+
self.add_edge(graph, pkg_def, root_dir_ref, 0);
75+
76+
// auxiliary root directories, map relative imports to module paths
6677
for (idx, root_dir) in tsc.root_dirs().iter().enumerate() {
67-
let root_dir_ref = self.add_module_pushes(graph, root_dir, proj_scope, &mut ids);
78+
let root_dir_def = self.add_pop(graph, proj_scope, REL_M_NS, &mut ids);
79+
self.add_debug_name(graph, root_dir_def, &format!("root_dirs[{}].def", idx));
80+
let root_dir_ref = self.add_module_pushes(graph, M_NS, root_dir, proj_scope, &mut ids);
6881
self.add_debug_name(graph, root_dir_ref, &format!("root_dirs[{}].ref", idx));
69-
self.add_edge(graph, proj_scope, root_dir_ref, 0);
82+
self.add_edge(graph, root_dir_def, root_dir_ref, 0);
7083
}
7184

7285
// base URL
7386
let base_url = tsc.base_url();
74-
let base_url_pkg = self.add_pop(graph, proj_scope, "%Pkg", &mut ids);
75-
self.add_debug_name(graph, base_url_pkg, "base_url.pkg_def");
76-
let base_url_ref = self.add_module_pushes(graph, &base_url, proj_scope, &mut ids);
87+
let base_url_def = self.add_pop(graph, proj_scope, NON_REL_M_NS, &mut ids);
88+
self.add_debug_name(graph, base_url_def, "base_url.def");
89+
let base_url_ref = self.add_module_pushes(graph, M_NS, &base_url, proj_scope, &mut ids);
7790
self.add_debug_name(graph, base_url_ref, "base_url.ref");
78-
self.add_edge(graph, base_url_pkg, base_url_ref, 0);
91+
self.add_edge(graph, base_url_def, base_url_ref, 0);
7992

8093
// path mappings
8194
for (from_idx, (from, tos)) in tsc.paths().iter().enumerate() {
@@ -85,13 +98,11 @@ impl FileAnalyzer for TsConfigAnalyzer {
8598
} else {
8699
&from
87100
};
88-
let mut from_def = self.add_pop(graph, proj_scope, "%Pkg", &mut ids);
89-
self.add_debug_name(graph, from_def, &format!("paths[{}].pkg_def", from_idx));
90-
from_def = self.add_module_pops(graph, from, from_def, &mut ids);
101+
let from_def = self.add_module_pops(graph, NON_REL_M_NS, from, proj_scope, &mut ids);
91102
self.add_debug_name(graph, from_def, &format!("paths[{}].from_def", from_idx));
92103
for (to_idx, to) in tos.iter().enumerate() {
93104
let to = if is_prefix { to.parent().unwrap() } else { &to };
94-
let to_ref = self.add_module_pushes(graph, to, proj_scope, &mut ids);
105+
let to_ref = self.add_module_pushes(graph, M_NS, to, proj_scope, &mut ids);
95106
self.add_debug_name(
96107
graph,
97108
to_ref,
@@ -184,18 +195,20 @@ impl TsConfigAnalyzer {
184195
fn add_module_pops<'a>(
185196
&'a self,
186197
graph: &'a mut StackGraph,
198+
ns: &str,
187199
path: &Path,
188200
from: Handle<Node>,
189201
ids: &mut dyn Iterator<Item = NodeID>,
190202
) -> Handle<Node> {
191-
let mut node = from;
203+
let ns_node = self.add_pop(graph, from, ns, ids);
204+
let mut node = ns_node;
192205
for c in path.components() {
193206
match c {
194207
Component::Normal(name) => {
195-
node = self.add_ns_pop(graph, node, "%M", &name.to_string_lossy(), ids);
208+
node = self.add_pop(graph, node, &name.to_string_lossy(), ids);
196209
}
197210
_ => {
198-
eprintln!("add_module_definition: expecting normalized, non-escaping, relative paths, got {}", path.display())
211+
eprintln!("add_module_pops: expecting normalized, non-escaping, relative paths, got {}", path.display())
199212
}
200213
}
201214
}
@@ -205,18 +218,20 @@ impl TsConfigAnalyzer {
205218
fn add_module_pushes<'a>(
206219
&'a self,
207220
graph: &'a mut StackGraph,
221+
ns: &str,
208222
path: &Path,
209223
to: Handle<Node>,
210224
ids: &mut dyn Iterator<Item = NodeID>,
211225
) -> Handle<Node> {
212-
let mut node = to;
226+
let ns_node = self.add_push(graph, to, ns, ids);
227+
let mut node = ns_node;
213228
for c in path.components() {
214229
match c {
215230
Component::Normal(name) => {
216-
node = self.add_ns_push(graph, node, "%M", &name.to_string_lossy(), ids);
231+
node = self.add_push(graph, node, &name.to_string_lossy(), ids);
217232
}
218233
_ => {
219-
eprintln!("add_module_reference: expecting normalized, non-escaping, relative paths, got {}", path.display())
234+
eprintln!("add_module_pushes: expecting normalized, non-escaping, relative paths, got {}", path.display())
220235
}
221236
}
222237
}

0 commit comments

Comments
 (0)