MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / flush_value_refs

Method flush_value_refs

codegraph-kernel/src/tsjs/mod.rs:646–736  ·  view source on GitHub ↗
(&mut self, root: Node<'t>)

Source from the content-addressed store, hash-verified

644 }
645
646 fn flush_value_refs(&mut self, root: Node<'t>) {
647 let scopes = std::mem::take(&mut self.value_scopes);
648 let mut targets = std::mem::take(&mut self.fs_values);
649 let counts = std::mem::take(&mut self.fs_value_counts);
650 if std::env::var("CODEGRAPH_VALUE_REFS").as_deref() == Ok("0") {
651 return;
652 }
653 if targets.is_empty() || scopes.is_empty() || util::is_generated_file(self.file_path) {
654 return;
655 }
656
657 // Shadow prune: count declarators of each target name across the whole
658 // tree; more declarators than file-scope nodes ⇒ an inner re-binding
659 // shadows the target. (TS/JS declarators are `variable_declarator`;
660 // the other kinds in the TS switch belong to other grammars.)
661 let mut decl_counts: HashMap<&str, u32> = HashMap::new();
662 let mut dstack: Vec<Node> = vec![root];
663 let mut dvisited = 0usize;
664 while let Some(n) = dstack.pop() {
665 if dvisited >= MAX_VALUE_REF_NODES {
666 break;
667 }
668 dvisited += 1;
669 if n.kind() == "variable_declarator" {
670 if let Some(first) = n.named_child(0) {
671 if first.kind() == "identifier" {
672 let nm = self.text(first);
673 if targets.contains_key(nm) {
674 *decl_counts.entry(nm).or_insert(0) += 1;
675 }
676 }
677 }
678 }
679 for c in n.named_children(&mut n.walk()) {
680 dstack.push(c);
681 }
682 }
683 let shadowed: Vec<String> = decl_counts
684 .iter()
685 .filter(|(nm, c)| **c > counts.get(**nm).copied().unwrap_or(1))
686 .map(|(nm, _)| nm.to_string())
687 .collect();
688 for nm in shadowed {
689 targets.remove(&nm);
690 }
691 if targets.is_empty() {
692 return;
693 }
694
695 let refs_kind = edge_kind_index("references").unwrap();
696 for scope in &scopes {
697 // Self-skip and per-scope dedupe compare node ID STRINGS (which
698 // collide for same-(kind, name, line) nodes), matching the TS side.
699 let mut seen: HashSet<&str> = HashSet::new();
700 let mut stack: Vec<Node> = vec![scope.node];
701 let mut visited = 0usize;
702 while let Some(n) = stack.pop() {
703 if visited >= MAX_VALUE_REF_NODES {

Callers 1

extractFunction · 0.45

Calls 12

is_generated_fileFunction · 0.85
edge_kind_indexFunction · 0.85
pushMethod · 0.80
mapMethod · 0.80
push_edgeMethod · 0.80
textMethod · 0.65
getMethod · 0.65
putMethod · 0.65
unwrapMethod · 0.60
takeFunction · 0.50
walkMethod · 0.45
collectMethod · 0.45

Tested by

no test coverage detected