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

Method flush_value_refs

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

Source from the content-addressed store, hash-verified

440 }
441
442 fn flush_value_refs(&mut self, root: Node<'t>) {
443 let scopes = std::mem::take(&mut self.value_scopes);
444 let mut targets = std::mem::take(&mut self.fs_values);
445 let counts = std::mem::take(&mut self.fs_value_counts);
446 if !self.variant.value_refs() || std::env::var("CODEGRAPH_VALUE_REFS").as_deref() == Ok("0") {
447 return;
448 }
449 if targets.is_empty() || scopes.is_empty() || util::is_generated_file(self.file_path) {
450 return;
451 }
452
453 // Shadow prune: count declarators of each target name across the whole
454 // tree; more declarators than file-scope nodes ⇒ an inner re-binding
455 // shadows the target. (TS/JS declarators are `variable_declarator`;
456 // the other kinds in the TS switch belong to other grammars.)
457 let mut decl_counts: HashMap<&str, u32> = HashMap::new();
458 let mut dstack: Vec<Node> = vec![root];
459 let mut dvisited = 0usize;
460 while let Some(n) = dstack.pop() {
461 if dvisited >= MAX_VALUE_REF_NODES {
462 break;
463 }
464 dvisited += 1;
465 if n.kind() == "variable_declarator" {
466 if let Some(first) = n.named_child(0) {
467 if first.kind() == "identifier" {
468 let nm = self.text(first);
469 if targets.contains_key(nm) {
470 *decl_counts.entry(nm).or_insert(0) += 1;
471 }
472 }
473 }
474 }
475 for i in 0..n.named_child_count() {
476 if let Some(c) = n.named_child(i) {
477 dstack.push(c);
478 }
479 }
480 }
481 let shadowed: Vec<String> = decl_counts
482 .iter()
483 .filter(|(nm, c)| **c > counts.get(**nm).copied().unwrap_or(1))
484 .map(|(nm, _)| nm.to_string())
485 .collect();
486 for nm in shadowed {
487 targets.remove(&nm);
488 }
489 if targets.is_empty() {
490 return;
491 }
492
493 let refs_kind = edge_kind_index("references").unwrap();
494 for scope in &scopes {
495 // Self-skip and per-scope dedupe compare node ID STRINGS (which
496 // collide for same-(kind, name, line) nodes), matching the TS side.
497 let mut seen: HashSet<&str> = HashSet::new();
498 let mut stack: Vec<Node> = vec![scope.node];
499 let mut visited = 0usize;

Callers 1

extractFunction · 0.45

Calls 9

is_generated_fileFunction · 0.85
edge_kind_indexFunction · 0.85
value_refsMethod · 0.80
putMethod · 0.80
push_edgeMethod · 0.80
getMethod · 0.65
unwrapMethod · 0.60
textMethod · 0.45
collectMethod · 0.45

Tested by

no test coverage detected