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