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

Commit f13243d

Browse files
author
Hendrik van Antwerpen
committed
Implement rootDirs
1 parent 6988726 commit f13243d

File tree

4 files changed

+97
-2
lines changed

4 files changed

+97
-2
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ impl FileAnalyzer for TsConfigAnalyzer {
6262
self.add_debug_name(graph, root_dir_ref, "root_dir.ref");
6363
self.add_edge(graph, root_dir_ref, proj_scope, 0);
6464

65+
// auxiliary root directories
66+
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);
68+
self.add_debug_name(graph, root_dir_ref, &format!("root_dirs[{}].ref", idx));
69+
self.add_edge(graph, proj_scope, root_dir_ref, 0);
70+
}
71+
6572
// base URL
6673
let base_url = tsc.base_url();
6774
let base_url_pkg = self.add_pop(graph, proj_scope, "%Pkg", &mut ids);

languages/tree-sitter-stack-graphs-typescript/src/stack-graphs.tsg

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ attribute node_symbol = node => symbol = (source-text node), source_n
281281
}
282282
}
283283

284-
;; Project reference
284+
;; Project and module reference
285285
(program)@prog {
286286
; project reference
287287
node proj__ns
@@ -305,6 +305,74 @@ attribute node_symbol = node => symbol = (source-text node), source_n
305305
}
306306
}
307307

308+
; module reference nodes
309+
let mod_path = (path-normalize (path-dir FILE_PATH))
310+
;
311+
var mod_scope = proj_scope
312+
scan mod_path {
313+
"([^/]+)/" {
314+
node mod_ref
315+
node mod_ref__ns
316+
;
317+
edge mod_ref__ns -> mod_scope
318+
;
319+
attr (mod_ref__ns) push_symbol = "%M"
320+
;
321+
attr (mod_ref) push_symbol = $1
322+
edge mod_ref -> mod_ref__ns
323+
324+
node mod_node
325+
edge mod_node -> mod_ref
326+
327+
node parent_def
328+
node parent_def__ns
329+
;
330+
attr (parent_def) pop_symbol = ".."
331+
edge parent_def -> mod_scope
332+
;
333+
attr (parent_def__ns) pop_symbol = "%M"
334+
edge parent_def__ns -> parent_def
335+
;
336+
edge mod_node -> parent_def__ns
337+
338+
set mod_scope = mod_node
339+
}
340+
"index$" {
341+
; expose reference in lexical scope
342+
edge @prog.lexical_scope -> mod_scope
343+
}
344+
"([^/]+)$" {
345+
node mod_ref
346+
node mod_ref__ns
347+
;
348+
edge mod_ref__ns -> mod_scope
349+
;
350+
attr (mod_ref__ns) push_symbol = "%M"
351+
;
352+
attr (mod_ref) push_symbol = $1
353+
edge mod_ref -> mod_ref__ns
354+
355+
node mod_node
356+
edge mod_node -> mod_ref
357+
358+
node parent_def
359+
node parent_def__ns
360+
;
361+
attr (parent_def) pop_symbol = ".."
362+
edge parent_def -> mod_scope
363+
;
364+
attr (parent_def__ns) pop_symbol = "%M"
365+
edge parent_def__ns -> parent_def
366+
;
367+
edge mod_node -> parent_def__ns
368+
369+
set mod_scope = mod_node
370+
371+
; expose reference in lexical scope
372+
edge @prog.lexical_scope -> mod_scope
373+
}
374+
}
375+
308376
; compose all packages files by adding edge to the package reference
309377
edge @prog.lexical_scope -> proj_scope
310378
}
@@ -493,7 +561,7 @@ attribute node_symbol = node => symbol = (source-text node), source_n
493561
scan mod_path {
494562
"^\\..*$" { ; relative imports
495563
; normalized module path
496-
let mod_path = (path-normalize (path-join (path-dir FILE_PATH) $0))
564+
let mod_path = (path-normalize $0)
497565

498566
; module references
499567
var mod_scope = @import_stmt.lexical_scope
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* --- path: ./tsconfig.json --- */
2+
{
3+
"compilerOptions": {
4+
"rootDirs": [
5+
"src/core",
6+
"src/util"
7+
]
8+
}
9+
}
10+
11+
/* --- path: ./src/core/index.ts --- */
12+
import { bar } from "./foo/baz";
13+
// ^ defined: 16
14+
15+
/* --- path: ./src/util/foo/baz.ts --- */
16+
export const bar = 42;
17+
18+
/* --- path: ./src/util/index.ts --- */
19+
20+
/* --- path: ./src/index.ts --- */

languages/tree-sitter-stack-graphs-typescript/test/projects/nested-rootdirs.ts.skip renamed to languages/tree-sitter-stack-graphs-typescript/test/projects/import-from-rootdirs.ts

File renamed without changes.

0 commit comments

Comments
 (0)