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