Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guard module names #347

Merged
merged 2 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ impl FileAnalyzer for NpmPackageAnalyzer {
pkg_internal_name,
"pkg_internal_name_pop",
);

// reach package internals via root
//
// [push pkg_internal_name] -> [push "GUARD:PKG_INTERNAL"] -> [root]
Expand All @@ -83,13 +82,21 @@ impl FileAnalyzer for NpmPackageAnalyzer {

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

let module_guard = add_push(
graph,
file,
pkg_internal_name_push,
MODULE_GUARD,
"main_guard",
);

// reach package internals via package name
//
// [root] -> [pop "GUARD:PKG"] -> [pop pkg_name]* -> [push pkg_internal_name]
Expand All @@ -102,11 +109,11 @@ impl FileAnalyzer for NpmPackageAnalyzer {
pkg_guard_pop,
"pkg_name_pop",
);
add_edge(graph, pkg_name_pop, pkg_internal_name_push, 0);
add_edge(graph, pkg_name_pop, module_guard, 0);

// reach main exports directly via package name (with precedence)
//
// [pop pkg_name] -1-> [pop "GUARD:EXPORTS"] -> [push "GUARD:EXPORTS"] -> [push main]* -> [push pkg_internal_name]
// [pop pkg_name] -1-> [pop "GUARD:EXPORTS"] -> [push "GUARD:EXPORTS"] -> [push main]* -> [push "GUARD:MODULE"] -> [push pkg_internal_name]
//
let exports_guard_pop = add_pop(
graph,
Expand All @@ -121,8 +128,7 @@ impl FileAnalyzer for NpmPackageAnalyzer {
.map(|p| p.into_path_buf())
.unwrap_or(PathBuf::from("index"))
.with_extension("");
let main_push =
add_module_pushes(graph, file, &main, pkg_internal_name_push, "main_push");
let main_push = add_module_pushes(graph, file, &main, module_guard, "main_push");
let exports_guard_push =
add_push(graph, file, main_push, EXPORTS_GUARD, "exports_guard_push");
add_edge(graph, exports_guard_pop, exports_guard_push, 0);
Expand Down
4 changes: 4 additions & 0 deletions languages/tree-sitter-stack-graphs-javascript/rust/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@ use stack_graphs::graph::File;
use stack_graphs::graph::StackGraph;

pub const EXPORTS_GUARD: &str = "GUARD:EXPORTS";
pub const MODULE_GUARD: &str = "GUARD:MODULE";
pub const PKG_GUARD: &str = "GUARD:PKG";
pub const PKG_INTERNAL_GUARD: &str = "GUARD:PKG_INTERNAL";

pub fn add_debug_name(graph: &mut StackGraph, node: Handle<Node>, name: &str) {
if name.is_empty() {
return;
}
let key = graph.add_string("name");
let value = graph.add_string(name);
graph.node_debug_info_mut(node).add(key, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ attribute node_symbol = node => symbol = (source-text node), source_n
;; a connected subgraph that is never the less never traversed.
;; - `GUARD:LABEL` - used for the names of labels
;; - `GUARD:MEMBER` - used for the members/fields/properties of objects
;; - `GUARD:MODULE` - used for the final scope of the module
;; - `GUARD:MODULE` - used for module names
;; - `GUARD:RETURN` - used for the AST nodes for values returned by a function
;; in the function body
;; - `GUARD:THIS` - used for the implicit `this` argument of a function inside
Expand Down Expand Up @@ -289,7 +289,6 @@ inherit .return_or_yield
(program)@prog {

node prog_module_pop
node prog_module_scope
node prog_exports_pop
node prog_pkg_pop_guard
node prog_pkg_push_guard
Expand All @@ -309,7 +308,12 @@ inherit .return_or_yield
edge @prog.pkg_push -> prog_pkg_push_guard
edge prog_pkg_push_guard -> ROOT_NODE

node module_guard_pop
attr (module_guard_pop) pop_symbol = "GUARD:MODULE"
edge @prog.pkg_pop -> module_guard_pop

node prog_module_pop_start
edge module_guard_pop -> prog_module_pop_start
var module_pop_end = prog_module_pop_start
let module_name = (replace FILE_PATH "\.js$" "")
scan module_name {
Expand All @@ -328,17 +332,12 @@ inherit .return_or_yield
edge @prog.before_scope -> @prog.pkg_push
edge @prog.before_scope -> @prog.hoist_point

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

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

attr (prog_module_pop) empty_source_span
attr (prog_module_scope) empty_source_span
attr (@prog.exports) empty_source_span
attr (prog_exports_pop) empty_source_span
attr (@prog.before_scope) empty_source_span
Expand Down Expand Up @@ -832,7 +831,12 @@ inherit .return_or_yield
; relative import
let name = (replace (path-normalize (path-join (path-dir FILE_PATH) $1)) "\.js$" "")

node module_guard_push
attr (module_guard_push) push_symbol = "GUARD:MODULE"
edge module_guard_push -> @source.pkg_push

node source_push_end
edge source_push_end -> module_guard_push
var push_start = source_push_end
scan name {
"([^/]+)/" {
Expand All @@ -856,7 +860,6 @@ inherit .return_or_yield
attr (push_start) is_reference, source_node = @source

edge source_push_guard_exports -> push_start
edge source_push_end -> @source.pkg_push
}
"^[\"']([^\./].*)[\"']$" {
; package import
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* --- path: debug.js --- */

/**/ debug(42);
// ^ defined: