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

Commit dcec17e

Browse files
author
Hendrik van Antwerpen
committed
Make TsConfigAnalzyer more robust for preexisting node ids when creating stack graph
1 parent baadf19 commit dcec17e

File tree

1 file changed

+30
-34
lines changed
  • languages/tree-sitter-stack-graphs-typescript/rust

1 file changed

+30
-34
lines changed

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

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -42,51 +42,49 @@ impl FileAnalyzer for TsConfigAnalyzer {
4242
) -> Result<(), tree_sitter_stack_graphs::LoadError> {
4343
let tsc = TsConfig::parse_str(path, source).map_err(|_| LoadError::ParseError)?;
4444

45-
// local id generator
46-
let mut ids = (1..).map(|id| NodeID::new_in_file(file, id));
47-
4845
// root node
4946
let root = graph.node_for_id(NodeID::root()).unwrap();
5047

5148
// project scope
52-
let proj_scope = graph.add_scope_node(ids.next().unwrap(), false).unwrap();
49+
let proj_scope_id = graph.new_node_id(file);
50+
let proj_scope = graph.add_scope_node(proj_scope_id, false).unwrap();
5351
self.add_debug_name(graph, proj_scope, "proj_scope");
5452

5553
// project definition
56-
let proj_def = self.add_ns_pop(graph, root, PROJ_NS, PROJECT_NAME, &mut ids);
54+
let proj_def = self.add_ns_pop(graph, file, root, PROJ_NS, PROJECT_NAME);
5755
self.add_debug_name(graph, proj_def, "proj_def");
5856
self.add_edge(graph, proj_def, proj_scope, 0);
5957

6058
// project reference
61-
let proj_ref = self.add_ns_push(graph, root, PROJ_NS, PROJECT_NAME, &mut ids);
59+
let proj_ref = self.add_ns_push(graph, file, root, PROJ_NS, PROJECT_NAME);
6260
self.add_debug_name(graph, proj_ref, "proj_ref");
6361
self.add_edge(graph, proj_scope, proj_ref, 0);
6462

6563
// root directory
6664
let root_dir_ref =
67-
self.add_module_pushes(graph, M_NS, &tsc.root_dir(all_paths), proj_scope, &mut ids);
65+
self.add_module_pushes(graph, file, M_NS, &tsc.root_dir(all_paths), proj_scope);
6866
self.add_debug_name(graph, root_dir_ref, "root_dir.ref");
6967

7068
// package definition
7169
let pkg_def =
72-
self.add_module_pops(graph, NON_REL_M_NS, Path::new(PACKAGE_NAME), root, &mut ids);
70+
self.add_module_pops(graph, file, NON_REL_M_NS, Path::new(PACKAGE_NAME), root);
7371
self.add_debug_name(graph, pkg_def, "pkg_def");
7472
self.add_edge(graph, pkg_def, root_dir_ref, 0);
7573

7674
// auxiliary root directories, map relative imports to module paths
7775
for (idx, root_dir) in tsc.root_dirs().iter().enumerate() {
78-
let root_dir_def = self.add_pop(graph, proj_scope, REL_M_NS, &mut ids);
76+
let root_dir_def = self.add_pop(graph, file, proj_scope, REL_M_NS);
7977
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);
78+
let root_dir_ref = self.add_module_pushes(graph, file, M_NS, root_dir, proj_scope);
8179
self.add_debug_name(graph, root_dir_ref, &format!("root_dirs[{}].ref", idx));
8280
self.add_edge(graph, root_dir_def, root_dir_ref, 0);
8381
}
8482

8583
// base URL
8684
let base_url = tsc.base_url();
87-
let base_url_def = self.add_pop(graph, proj_scope, NON_REL_M_NS, &mut ids);
85+
let base_url_def = self.add_pop(graph, file, proj_scope, NON_REL_M_NS);
8886
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);
87+
let base_url_ref = self.add_module_pushes(graph, file, M_NS, &base_url, proj_scope);
9088
self.add_debug_name(graph, base_url_ref, "base_url.ref");
9189
self.add_edge(graph, base_url_def, base_url_ref, 0);
9290

@@ -98,11 +96,11 @@ impl FileAnalyzer for TsConfigAnalyzer {
9896
} else {
9997
&from
10098
};
101-
let from_def = self.add_module_pops(graph, NON_REL_M_NS, from, proj_scope, &mut ids);
99+
let from_def = self.add_module_pops(graph, file, NON_REL_M_NS, from, proj_scope);
102100
self.add_debug_name(graph, from_def, &format!("paths[{}].from_def", from_idx));
103101
for (to_idx, to) in tos.iter().enumerate() {
104102
let to = if is_prefix { to.parent().unwrap() } else { &to };
105-
let to_ref = self.add_module_pushes(graph, M_NS, to, proj_scope, &mut ids);
103+
let to_ref = self.add_module_pushes(graph, file, M_NS, to, proj_scope);
106104
self.add_debug_name(
107105
graph,
108106
to_ref,
@@ -126,56 +124,54 @@ impl TsConfigAnalyzer {
126124
fn add_pop<'a>(
127125
&'a self,
128126
graph: &'a mut StackGraph,
127+
file: Handle<File>,
129128
from: Handle<Node>,
130129
name: &str,
131-
ids: &mut dyn Iterator<Item = NodeID>,
132130
) -> Handle<Node> {
131+
let id = graph.new_node_id(file);
133132
let sym = graph.add_symbol(name);
134-
let node = graph
135-
.add_pop_symbol_node(ids.next().unwrap(), sym, false)
136-
.unwrap();
133+
let node = graph.add_pop_symbol_node(id, sym, false).unwrap();
137134
graph.add_edge(from, node, 0);
138135
node
139136
}
140137

141138
fn add_push<'a>(
142139
&'a self,
143140
graph: &'a mut StackGraph,
141+
file: Handle<File>,
144142
to: Handle<Node>,
145143
name: &str,
146-
ids: &mut dyn Iterator<Item = NodeID>,
147144
) -> Handle<Node> {
145+
let id = graph.new_node_id(file);
148146
let sym = graph.add_symbol(name);
149-
let node = graph
150-
.add_push_symbol_node(ids.next().unwrap(), sym, false)
151-
.unwrap();
147+
let node = graph.add_push_symbol_node(id, sym, false).unwrap();
152148
graph.add_edge(node, to, 0);
153149
node
154150
}
155151

156152
fn add_ns_pop<'a>(
157153
&'a self,
158154
graph: &'a mut StackGraph,
155+
file: Handle<File>,
159156
from: Handle<Node>,
160157
ns: &str,
161158
name: &str,
162-
ids: &mut dyn Iterator<Item = NodeID>,
163159
) -> Handle<Node> {
164-
let ns_node = self.add_pop(graph, from, ns, ids);
165-
let pop_node = self.add_pop(graph, ns_node, name, ids);
160+
let ns_node = self.add_pop(graph, file, from, ns);
161+
let pop_node = self.add_pop(graph, file, ns_node, name);
166162
pop_node
167163
}
168164

169165
fn add_ns_push<'a>(
170166
&'a self,
171167
graph: &'a mut StackGraph,
168+
file: Handle<File>,
172169
to: Handle<Node>,
173170
ns: &str,
174171
name: &str,
175-
ids: &mut dyn Iterator<Item = NodeID>,
176172
) -> Handle<Node> {
177-
let ns_node = self.add_push(graph, to, ns, ids);
178-
let push_node = self.add_push(graph, ns_node, name, ids);
173+
let ns_node = self.add_push(graph, file, to, ns);
174+
let push_node = self.add_push(graph, file, ns_node, name);
179175
push_node
180176
}
181177

@@ -195,17 +191,17 @@ impl TsConfigAnalyzer {
195191
fn add_module_pops<'a>(
196192
&'a self,
197193
graph: &'a mut StackGraph,
194+
file: Handle<File>,
198195
ns: &str,
199196
path: &Path,
200197
from: Handle<Node>,
201-
ids: &mut dyn Iterator<Item = NodeID>,
202198
) -> Handle<Node> {
203-
let ns_node = self.add_pop(graph, from, ns, ids);
199+
let ns_node = self.add_pop(graph, file, from, ns);
204200
let mut node = ns_node;
205201
for c in path.components() {
206202
match c {
207203
Component::Normal(name) => {
208-
node = self.add_pop(graph, node, &name.to_string_lossy(), ids);
204+
node = self.add_pop(graph, file, node, &name.to_string_lossy());
209205
}
210206
_ => {
211207
eprintln!("add_module_pops: expecting normalized, non-escaping, relative paths, got {}", path.display())
@@ -218,17 +214,17 @@ impl TsConfigAnalyzer {
218214
fn add_module_pushes<'a>(
219215
&'a self,
220216
graph: &'a mut StackGraph,
217+
file: Handle<File>,
221218
ns: &str,
222219
path: &Path,
223220
to: Handle<Node>,
224-
ids: &mut dyn Iterator<Item = NodeID>,
225221
) -> Handle<Node> {
226-
let ns_node = self.add_push(graph, to, ns, ids);
222+
let ns_node = self.add_push(graph, file, to, ns);
227223
let mut node = ns_node;
228224
for c in path.components() {
229225
match c {
230226
Component::Normal(name) => {
231-
node = self.add_push(graph, node, &name.to_string_lossy(), ids);
227+
node = self.add_push(graph, file, node, &name.to_string_lossy());
232228
}
233229
_ => {
234230
eprintln!("add_module_pushes: expecting normalized, non-escaping, relative paths, got {}", path.display())

0 commit comments

Comments
 (0)