Skip to content

Commit 728cc58

Browse files
Merge pull request #347 from github/module-guard
Guard module names
2 parents c50bbd8 + 80b5d61 commit 728cc58

File tree

4 files changed

+31
-14
lines changed

4 files changed

+31
-14
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 & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +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 the final scope of the module
233+
;; - `GUARD:MODULE` - used for module names
234234
;; - `GUARD:RETURN` - used for the AST nodes for values returned by a function
235235
;; in the function body
236236
;; - `GUARD:THIS` - used for the implicit `this` argument of a function inside
@@ -289,7 +289,6 @@ inherit .return_or_yield
289289
(program)@prog {
290290

291291
node prog_module_pop
292-
node prog_module_scope
293292
node prog_exports_pop
294293
node prog_pkg_pop_guard
295294
node prog_pkg_push_guard
@@ -309,7 +308,12 @@ inherit .return_or_yield
309308
edge @prog.pkg_push -> prog_pkg_push_guard
310309
edge prog_pkg_push_guard -> ROOT_NODE
311310

311+
node module_guard_pop
312+
attr (module_guard_pop) pop_symbol = "GUARD:MODULE"
313+
edge @prog.pkg_pop -> module_guard_pop
314+
312315
node prog_module_pop_start
316+
edge module_guard_pop -> prog_module_pop_start
313317
var module_pop_end = prog_module_pop_start
314318
let module_name = (replace FILE_PATH "\.js$" "")
315319
scan module_name {
@@ -328,17 +332,12 @@ inherit .return_or_yield
328332
edge @prog.before_scope -> @prog.pkg_push
329333
edge @prog.before_scope -> @prog.hoist_point
330334

331-
attr (prog_module_scope) pop_symbol = "GUARD:MODULE"
332-
edge @prog.pkg_pop -> prog_module_pop_start
333-
edge module_pop_end -> prog_module_scope
334-
edge prog_module_scope -> @prog.after_scope
335335

336336
attr (prog_legacy_qname_guard) push_symbol = "GUARD:LEGACY_QNAME"
337337
edge @prog.before_scope -> prog_legacy_qname_guard
338338
edge prog_legacy_qname_guard -> ROOT_NODE
339339

340340
attr (prog_module_pop) empty_source_span
341-
attr (prog_module_scope) empty_source_span
342341
attr (@prog.exports) empty_source_span
343342
attr (prog_exports_pop) empty_source_span
344343
attr (@prog.before_scope) empty_source_span
@@ -832,7 +831,12 @@ inherit .return_or_yield
832831
; relative import
833832
let name = (replace (path-normalize (path-join (path-dir FILE_PATH) $1)) "\.js$" "")
834833

834+
node module_guard_push
835+
attr (module_guard_push) push_symbol = "GUARD:MODULE"
836+
edge module_guard_push -> @source.pkg_push
837+
835838
node source_push_end
839+
edge source_push_end -> module_guard_push
836840
var push_start = source_push_end
837841
scan name {
838842
"([^/]+)/" {
@@ -856,7 +860,6 @@ inherit .return_or_yield
856860
attr (push_start) is_reference, source_node = @source
857861

858862
edge source_push_guard_exports -> push_start
859-
edge source_push_end -> @source.pkg_push
860863
}
861864
"^[\"']([^\./].*)[\"']$" {
862865
; 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)