Skip to content

Commit 80b5d61

Browse files
Guard module names
1 parent 33883c4 commit 80b5d61

File tree

4 files changed

+31
-8
lines changed

4 files changed

+31
-8
lines changed

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ impl FileAnalyzer for NpmPackageAnalyzer {
6161
pkg_internal_name,
6262
"pkg_internal_name_pop",
6363
);
64-
6564
// reach package internals via root
6665
//
6766
// [push pkg_internal_name] -> [push "GUARD:PKG_INTERNAL"] -> [root]
@@ -83,13 +82,21 @@ impl FileAnalyzer for NpmPackageAnalyzer {
8382

8483
// reach exports via package name
8584
//
86-
// [root] -> [pop "GUARD:PKG"] -> [pop PKG_NAME]* -> [push PKG_INTERNAL_NAME] -> [push "GUARD:PKG_INTERNAL"] -> [root]
85+
// [root] -> [pop "GUARD:PKG"] -> [pop PKG_NAME]* -> [push "GUARD:MODULE"] -> [push PKG_INTERNAL_NAME] -> [push "GUARD:PKG_INTERNAL"] -> [root]
8786
//
8887
if !npm_pkg.name.is_empty() {
8988
// NOTE Because all modules expose their exports at the top-level, both paths created below are equivalent for
9089
// exports of the main module. This means multiple equivalent paths to those exports, which is bad for
9190
// performance. At the moment, we have no mechanism to prevent this from happening.
9291

92+
let module_guard = add_push(
93+
graph,
94+
file,
95+
pkg_internal_name_push,
96+
MODULE_GUARD,
97+
"main_guard",
98+
);
99+
93100
// reach package internals via package name
94101
//
95102
// [root] -> [pop "GUARD:PKG"] -> [pop pkg_name]* -> [push pkg_internal_name]
@@ -102,11 +109,11 @@ impl FileAnalyzer for NpmPackageAnalyzer {
102109
pkg_guard_pop,
103110
"pkg_name_pop",
104111
);
105-
add_edge(graph, pkg_name_pop, pkg_internal_name_push, 0);
112+
add_edge(graph, pkg_name_pop, module_guard, 0);
106113

107114
// reach main exports directly via package name (with precedence)
108115
//
109-
// [pop pkg_name] -1-> [pop "GUARD:EXPORTS"] -> [push "GUARD:EXPORTS"] -> [push main]* -> [push pkg_internal_name]
116+
// [pop pkg_name] -1-> [pop "GUARD:EXPORTS"] -> [push "GUARD:EXPORTS"] -> [push main]* -> [push "GUARD:MODULE"] -> [push pkg_internal_name]
110117
//
111118
let exports_guard_pop = add_pop(
112119
graph,
@@ -121,8 +128,7 @@ impl FileAnalyzer for NpmPackageAnalyzer {
121128
.map(|p| p.into_path_buf())
122129
.unwrap_or(PathBuf::from("index"))
123130
.with_extension("");
124-
let main_push =
125-
add_module_pushes(graph, file, &main, pkg_internal_name_push, "main_push");
131+
let main_push = add_module_pushes(graph, file, &main, module_guard, "main_push");
126132
let exports_guard_push =
127133
add_push(graph, file, main_push, EXPORTS_GUARD, "exports_guard_push");
128134
add_edge(graph, exports_guard_pop, exports_guard_push, 0);

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@ use stack_graphs::graph::File;
1515
use stack_graphs::graph::StackGraph;
1616

1717
pub const EXPORTS_GUARD: &str = "GUARD:EXPORTS";
18+
pub const MODULE_GUARD: &str = "GUARD:MODULE";
1819
pub const PKG_GUARD: &str = "GUARD:PKG";
1920
pub const PKG_INTERNAL_GUARD: &str = "GUARD:PKG_INTERNAL";
2021

2122
pub fn add_debug_name(graph: &mut StackGraph, node: Handle<Node>, name: &str) {
23+
if name.is_empty() {
24+
return;
25+
}
2226
let key = graph.add_string("name");
2327
let value = graph.add_string(name);
2428
graph.node_debug_info_mut(node).add(key, value);

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ attribute node_symbol = node => symbol = (source-text node), source_n
230230
;; a connected subgraph that is never the less never traversed.
231231
;; - `GUARD:LABEL` - used for the names of labels
232232
;; - `GUARD:MEMBER` - used for the members/fields/properties of objects
233+
;; - `GUARD:MODULE` - used for module names
233234
;; - `GUARD:RETURN` - used for the AST nodes for values returned by a function
234235
;; in the function body
235236
;; - `GUARD:THIS` - used for the implicit `this` argument of a function inside
@@ -307,7 +308,12 @@ inherit .return_or_yield
307308
edge @prog.pkg_push -> prog_pkg_push_guard
308309
edge prog_pkg_push_guard -> ROOT_NODE
309310

311+
node module_guard_pop
312+
attr (module_guard_pop) pop_symbol = "GUARD:MODULE"
313+
edge @prog.pkg_pop -> module_guard_pop
314+
310315
node prog_module_pop_start
316+
edge module_guard_pop -> prog_module_pop_start
311317
var module_pop_end = prog_module_pop_start
312318
let module_name = (replace FILE_PATH "\.js$" "")
313319
scan module_name {
@@ -326,7 +332,6 @@ inherit .return_or_yield
326332
edge @prog.before_scope -> @prog.pkg_push
327333
edge @prog.before_scope -> @prog.hoist_point
328334

329-
edge @prog.pkg_pop -> prog_module_pop_start
330335

331336
attr (prog_legacy_qname_guard) push_symbol = "GUARD:LEGACY_QNAME"
332337
edge @prog.before_scope -> prog_legacy_qname_guard
@@ -826,7 +831,12 @@ inherit .return_or_yield
826831
; relative import
827832
let name = (replace (path-normalize (path-join (path-dir FILE_PATH) $1)) "\.js$" "")
828833

834+
node module_guard_push
835+
attr (module_guard_push) push_symbol = "GUARD:MODULE"
836+
edge module_guard_push -> @source.pkg_push
837+
829838
node source_push_end
839+
edge source_push_end -> module_guard_push
830840
var push_start = source_push_end
831841
scan name {
832842
"([^/]+)/" {
@@ -850,7 +860,6 @@ inherit .return_or_yield
850860
attr (push_start) is_reference, source_node = @source
851861

852862
edge source_push_guard_exports -> push_start
853-
edge source_push_end -> @source.pkg_push
854863
}
855864
"^[\"']([^\./].*)[\"']$" {
856865
; package import
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/* --- path: debug.js --- */
2+
3+
/**/ debug(42);
4+
// ^ defined:

0 commit comments

Comments
 (0)