mirror of
https://github.com/colbymchenry/codegraph.git
synced 2026-10-27 07:23:43 +08:00
fix(extraction): a function in an initializer is referenced once, by what the initializer declares (#2743)
PRs #693 and #1802 walk a declaration's initializer under the symbol it declares, so `const routes = { home }` references `home` from `routes`. The dispatcher's declaration-wide function-as-value scan then took the same values again for the enclosing file (or class). Flush-time dedup keys on the source, so both stayed: a second reference to the same function from the same spot. Each initializer walk now records what it walked, and the scan skips it. PR #2738 added that mechanism for component wrappers, and C#, Scala and Dart already had their own. The newly covered paths: - TS/JS module-level constants and variables, owned object literals included (#2300), and TS/JS class properties; - Python assignments, Go var/const specs, Rust const/static items; - Java fields, and Kotlin properties (walkInitializer takes the property's id, so their walk is unchanged). Mirrored in the kernel: tsjs, python, go, rustlang, java and kotlin. References no initializer walk takes are unchanged: a module-level call argument `register(fn)`, C-family tables, Rust's `static CB: fn() = handler` (the declaration's own value, which the scan still reads). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
ae0fc631e0
commit
51be28d15b
@@ -428,8 +428,10 @@ impl<'t> Walker<'t> {
|
||||
} else if matches!(kind, "var_declaration" | "short_var_declaration" | "const_declaration")
|
||||
&& !self.inside_class_like()
|
||||
{
|
||||
self.extract_variable(node);
|
||||
self.scan_fn_ref_subtree(node, 0);
|
||||
// The initializers extract_variable walked under their specs'
|
||||
// symbols took their own candidates; the scan covers the rest.
|
||||
let walked = self.extract_variable(node);
|
||||
self.scan_fn_ref_subtree(node, 0, &walked);
|
||||
skip_children = true;
|
||||
} else if kind == "import_declaration" {
|
||||
self.extract_import(node);
|
||||
@@ -678,7 +680,10 @@ impl<'t> Walker<'t> {
|
||||
}
|
||||
|
||||
/// extractVariable's Go branch: var/const specs + short_var_declaration.
|
||||
fn extract_variable(&mut self, node: Node<'t>) {
|
||||
/// Returns the initializers it walked under their specs' symbols, for the
|
||||
/// declaration scan to skip.
|
||||
fn extract_variable(&mut self, node: Node<'t>) -> Vec<usize> {
|
||||
let mut walked = Vec::new();
|
||||
let docstring = self.docstring_of(node);
|
||||
let is_const_decl = node.kind() == "const_declaration";
|
||||
|
||||
@@ -726,6 +731,7 @@ impl<'t> Walker<'t> {
|
||||
self.stack.push(Scope { row, kind: "variable", name });
|
||||
self.visit_function_body(value_field);
|
||||
self.stack.pop();
|
||||
walked.push(value_field.id());
|
||||
} else {
|
||||
self.visit_function_body(value_field);
|
||||
}
|
||||
@@ -756,6 +762,7 @@ impl<'t> Walker<'t> {
|
||||
}
|
||||
}
|
||||
}
|
||||
walked
|
||||
}
|
||||
|
||||
/// extractImport's Go branch: one import node + ref per import_spec.
|
||||
@@ -1241,11 +1248,15 @@ impl<'t> Walker<'t> {
|
||||
}
|
||||
}
|
||||
|
||||
fn scan_fn_ref_subtree(&mut self, node: Node<'t>, depth: u32) {
|
||||
fn scan_fn_ref_subtree(&mut self, node: Node<'t>, depth: u32, walked: &[usize]) {
|
||||
stack_guard!();
|
||||
if depth > 12 {
|
||||
return;
|
||||
}
|
||||
// Subtrees the body walker has already been through.
|
||||
if walked.contains(&node.id()) {
|
||||
return;
|
||||
}
|
||||
if depth > 0
|
||||
&& matches!(
|
||||
node.kind(),
|
||||
@@ -1258,7 +1269,7 @@ impl<'t> Walker<'t> {
|
||||
self.maybe_capture_fn_refs(node);
|
||||
for i in 0..node.named_child_count() {
|
||||
if let Some(c) = node.named_child(i) {
|
||||
self.scan_fn_ref_subtree(c, depth + 1);
|
||||
self.scan_fn_ref_subtree(c, depth + 1, walked);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user