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

Commit 649a0d2

Browse files
author
Hendrik van Antwerpen
committed
Cleanup code and ensure all components of module names have a debug name attached
1 parent 90e2e11 commit 649a0d2

File tree

3 files changed

+100
-43
lines changed

3 files changed

+100
-43
lines changed

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

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,37 @@ impl FileAnalyzer for NpmPackageAnalyzer {
4949
add_debug_name(graph, proj_scope, "npm_package.proj_scope");
5050

5151
// project definition
52-
let proj_def = add_ns_pop(graph, file, root, PROJ_NS, proj_name);
53-
add_debug_name(graph, proj_def, "npm_package.proj_def");
52+
let proj_def = add_ns_pop(
53+
graph,
54+
file,
55+
root,
56+
PROJ_NS,
57+
proj_name,
58+
"npm_package.proj_def",
59+
);
5460
add_edge(graph, proj_def, proj_scope, 0);
5561

5662
// project reference
57-
let proj_ref = add_ns_push(graph, file, root, PROJ_NS, proj_name);
58-
add_debug_name(graph, proj_ref, "npm_package.proj_ref");
63+
let proj_ref = add_ns_push(
64+
graph,
65+
file,
66+
root,
67+
PROJ_NS,
68+
proj_name,
69+
"npm_package.proj_ref",
70+
);
5971
add_edge(graph, proj_scope, proj_ref, 0);
6072

6173
// package definition
62-
let pkg_def = add_module_pops(graph, file, NON_REL_M_NS, Path::new(&npm_pkg.name), root);
63-
add_debug_name(graph, pkg_def, "npm_package.pkg_def");
64-
let pkg_ref = add_push(graph, file, proj_scope, PKG_M_NS);
65-
add_debug_name(graph, pkg_ref, "npm_package.pkg_ref");
74+
let pkg_def = add_module_pops(
75+
graph,
76+
file,
77+
NON_REL_M_NS,
78+
Path::new(&npm_pkg.name),
79+
root,
80+
"npm_package.pkg_def",
81+
);
82+
let pkg_ref = add_push(graph, file, proj_scope, PKG_M_NS, "npm_package.pkg_ref");
6683
add_edge(graph, pkg_def, pkg_ref, 0);
6784

6885
Ok(())

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

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -50,46 +50,62 @@ impl FileAnalyzer for TsConfigAnalyzer {
5050
add_debug_name(graph, proj_scope, "tsconfig.proj_scope");
5151

5252
// project definition
53-
let proj_def = add_ns_pop(graph, file, root, PROJ_NS, proj_name);
54-
add_debug_name(graph, proj_def, "tsconfig.proj_def");
53+
let proj_def = add_ns_pop(graph, file, root, PROJ_NS, proj_name, "tsconfig.proj_def");
5554
add_edge(graph, proj_def, proj_scope, 0);
5655

5756
// project reference
58-
let proj_ref = add_ns_push(graph, file, root, PROJ_NS, proj_name);
59-
add_debug_name(graph, proj_ref, "tsconfig.proj_ref");
57+
let proj_ref = add_ns_push(graph, file, root, PROJ_NS, proj_name, "tsconfig.proj_ref");
6058
add_edge(graph, proj_scope, proj_ref, 0);
6159

6260
// root directory
63-
let pkg_def = add_pop(graph, file, proj_scope, PKG_M_NS);
64-
add_debug_name(graph, pkg_def, "tsconfig.pkg_def");
65-
let root_dir_ref =
66-
add_module_pushes(graph, file, M_NS, &tsc.root_dir(all_paths), proj_scope);
67-
add_debug_name(graph, root_dir_ref, "tsconfig.root_dir.ref");
61+
let pkg_def = add_pop(graph, file, proj_scope, PKG_M_NS, "tsconfig.pkg_def");
62+
let root_dir_ref = add_module_pushes(
63+
graph,
64+
file,
65+
M_NS,
66+
&tsc.root_dir(all_paths),
67+
proj_scope,
68+
"tsconfig.root_dir.ref",
69+
);
6870
add_edge(graph, pkg_def, root_dir_ref, 0);
6971

7072
// auxiliary root directories, map relative imports to module paths
7173
for (idx, root_dir) in tsc.root_dirs().iter().enumerate() {
72-
let root_dir_def = add_pop(graph, file, proj_scope, REL_M_NS);
73-
add_debug_name(
74+
let root_dir_def = add_pop(
7475
graph,
75-
root_dir_def,
76+
file,
77+
proj_scope,
78+
REL_M_NS,
7679
&format!("tsconfig.root_dirs[{}].def", idx),
7780
);
78-
let root_dir_ref = add_module_pushes(graph, file, M_NS, root_dir, proj_scope);
79-
add_debug_name(
81+
let root_dir_ref = add_module_pushes(
8082
graph,
81-
root_dir_ref,
83+
file,
84+
M_NS,
85+
root_dir,
86+
proj_scope,
8287
&format!("tsconfig.root_dirs[{}].ref", idx),
8388
);
8489
add_edge(graph, root_dir_def, root_dir_ref, 0);
8590
}
8691

8792
// base URL
8893
let base_url = tsc.base_url();
89-
let base_url_def = add_pop(graph, file, proj_scope, NON_REL_M_NS);
90-
add_debug_name(graph, base_url_def, "tsconfig.base_url.def");
91-
let base_url_ref = add_module_pushes(graph, file, M_NS, &base_url, proj_scope);
92-
add_debug_name(graph, base_url_ref, "tsconfig.base_url.ref");
94+
let base_url_def = add_pop(
95+
graph,
96+
file,
97+
proj_scope,
98+
NON_REL_M_NS,
99+
"tsconfig.base_url.def",
100+
);
101+
let base_url_ref = add_module_pushes(
102+
graph,
103+
file,
104+
M_NS,
105+
&base_url,
106+
proj_scope,
107+
"tsconfig.base_url.ref",
108+
);
93109
add_edge(graph, base_url_def, base_url_ref, 0);
94110

95111
// path mappings
@@ -100,18 +116,22 @@ impl FileAnalyzer for TsConfigAnalyzer {
100116
} else {
101117
&from
102118
};
103-
let from_def = add_module_pops(graph, file, NON_REL_M_NS, from, proj_scope);
104-
add_debug_name(
119+
let from_def = add_module_pops(
105120
graph,
106-
from_def,
121+
file,
122+
NON_REL_M_NS,
123+
from,
124+
proj_scope,
107125
&format!("tsconfig.paths[{}].from_def", from_idx),
108126
);
109127
for (to_idx, to) in tos.iter().enumerate() {
110128
let to = if is_prefix { to.parent().unwrap() } else { &to };
111-
let to_ref = add_module_pushes(graph, file, M_NS, to, proj_scope);
112-
add_debug_name(
129+
let to_ref = add_module_pushes(
113130
graph,
114-
to_ref,
131+
file,
132+
M_NS,
133+
to,
134+
proj_scope,
115135
&format!("tsconfig.paths[{}][{}].to_ref", from_idx, to_idx),
116136
);
117137
add_edge(graph, from_def, to_ref, 0);

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

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@ pub fn add_pop(
3030
file: Handle<File>,
3131
from: Handle<Node>,
3232
name: &str,
33+
debug_name: &str,
3334
) -> Handle<Node> {
3435
let id = graph.new_node_id(file);
3536
let sym = graph.add_symbol(name);
3637
let node = graph.add_pop_symbol_node(id, sym, false).unwrap();
3738
graph.add_edge(from, node, 0);
39+
add_debug_name(graph, node, debug_name);
3840
node
3941
}
4042

@@ -43,11 +45,13 @@ pub fn add_push(
4345
file: Handle<File>,
4446
to: Handle<Node>,
4547
name: &str,
48+
debug_name: &str,
4649
) -> Handle<Node> {
4750
let id = graph.new_node_id(file);
4851
let sym = graph.add_symbol(name);
4952
let node = graph.add_push_symbol_node(id, sym, false).unwrap();
5053
graph.add_edge(node, to, 0);
54+
add_debug_name(graph, node, debug_name);
5155
node
5256
}
5357

@@ -57,9 +61,10 @@ pub fn add_ns_pop(
5761
from: Handle<Node>,
5862
ns: &str,
5963
name: &str,
64+
debug_prefix: &str,
6065
) -> Handle<Node> {
61-
let ns_node = add_pop(graph, file, from, ns);
62-
let pop_node = add_pop(graph, file, ns_node, name);
66+
let ns_node = add_pop(graph, file, from, ns, &format!("{}.ns", debug_prefix));
67+
let pop_node = add_pop(graph, file, ns_node, name, debug_prefix);
6368
pop_node
6469
}
6570

@@ -69,9 +74,10 @@ pub fn add_ns_push(
6974
to: Handle<Node>,
7075
ns: &str,
7176
name: &str,
77+
debug_prefix: &str,
7278
) -> Handle<Node> {
73-
let ns_node = add_push(graph, file, to, ns);
74-
let push_node = add_push(graph, file, ns_node, name);
79+
let ns_node = add_push(graph, file, to, ns, &format!("{}.ns", debug_prefix));
80+
let push_node = add_push(graph, file, ns_node, name, debug_prefix);
7581
push_node
7682
}
7783

@@ -88,13 +94,20 @@ pub fn add_module_pops(
8894
ns: &str,
8995
path: &Path,
9096
from: Handle<Node>,
97+
debug_prefix: &str,
9198
) -> Handle<Node> {
92-
let ns_node = add_pop(graph, file, from, ns);
99+
let ns_node = add_pop(graph, file, from, ns, &format!("{}.ns", debug_prefix));
93100
let mut node = ns_node;
94-
for c in path.components() {
101+
for (i, c) in path.components().enumerate() {
95102
match c {
96103
Component::Normal(name) => {
97-
node = add_pop(graph, file, node, &name.to_string_lossy());
104+
node = add_pop(
105+
graph,
106+
file,
107+
node,
108+
&name.to_string_lossy(),
109+
&format!("{}[{}]", debug_prefix, i),
110+
);
98111
}
99112
_ => {
100113
eprintln!(
@@ -113,13 +126,20 @@ pub fn add_module_pushes(
113126
ns: &str,
114127
path: &Path,
115128
to: Handle<Node>,
129+
debug_prefix: &str,
116130
) -> Handle<Node> {
117-
let ns_node = add_push(graph, file, to, ns);
131+
let ns_node = add_push(graph, file, to, ns, &format!("{}.ns", debug_prefix));
118132
let mut node = ns_node;
119-
for c in path.components() {
133+
for (i, c) in path.components().enumerate() {
120134
match c {
121135
Component::Normal(name) => {
122-
node = add_push(graph, file, node, &name.to_string_lossy());
136+
node = add_push(
137+
graph,
138+
file,
139+
node,
140+
&name.to_string_lossy(),
141+
&format!("{}[{}]", debug_prefix, i),
142+
);
123143
}
124144
_ => {
125145
eprintln!(

0 commit comments

Comments
 (0)