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

Method flush_value_refs

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

Source from the content-addressed store, hash-verified

530 }
531
532 fn flush_value_refs(&mut self, root: Node<'t>) {
533 let scopes = std::mem::take(&mut self.value_scopes);
534 let mut targets = std::mem::take(&mut self.fs_values);
535 let counts = std::mem::take(&mut self.fs_value_counts);
536 if std::env::var("CODEGRAPH_VALUE_REFS").as_deref() == Ok("0") {
537 return;
538 }
539 if targets.is_empty() || scopes.is_empty() || util::is_generated_file(self.file_path) {
540 return;
541 }
542
543 // Shadow prune: count declarators of each target name across the whole
544 // tree; more declarators than file-scope nodes ⇒ an inner re-binding
545 // shadows the target. (TS/JS declarators are `variable_declarator`;
546 // the other kinds in the TS switch belong to other grammars.)
547 let mut decl_counts: HashMap<&str, u32> = HashMap::new();
548 let mut dstack: Vec<Node> = vec![root];
549 let mut dvisited = 0usize;
550 while let Some(n) = dstack.pop() {
551 if dvisited >= MAX_VALUE_REF_NODES {
552 break;
553 }
554 dvisited += 1;
555 if n.kind() == "variable_declarator" {
556 if let Some(first) = n.named_child(0) {
557 if first.kind() == "identifier" {
558 let nm = self.text(first);
559 if targets.contains_key(nm) {
560 *decl_counts.entry(nm).or_insert(0) += 1;
561 }
562 }
563 }
564 }
565 for c in n.named_children(&mut n.walk()) {
566 dstack.push(c);
567 }
568 }
569 let shadowed: Vec<String> = decl_counts
570 .iter()
571 .filter(|(nm, c)| **c > counts.get(**nm).copied().unwrap_or(1))
572 .map(|(nm, _)| nm.to_string())
573 .collect();
574 for nm in shadowed {
575 targets.remove(&nm);
576 }
577 if targets.is_empty() {
578 return;
579 }
580
581 let refs_kind = edge_kind_index("references").unwrap();
582 for scope in &scopes {
583 // Self-skip and per-scope dedupe compare node ID STRINGS (which
584 // collide for same-(kind, name, line) nodes), matching the TS side.
585 let mut seen: HashSet<&str> = HashSet::new();
586 let mut stack: Vec<Node> = vec![scope.node];
587 let mut visited = 0usize;
588 while let Some(n) = stack.pop() {
589 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