| 1042 | // --- value references (crib of python.rs — same traversal, same cases) ------ |
| 1043 | |
| 1044 | fn flush_value_refs(&mut self, root: Node<'t>) { |
| 1045 | let scopes = std::mem::take(&mut self.value_scopes); |
| 1046 | let mut targets = std::mem::take(&mut self.fs_values); |
| 1047 | let counts = std::mem::take(&mut self.fs_value_counts); |
| 1048 | if std::env::var("CODEGRAPH_VALUE_REFS").as_deref() == Ok("0") { |
| 1049 | return; |
| 1050 | } |
| 1051 | if targets.is_empty() || scopes.is_empty() || util::is_generated_file(self.file_path) { |
| 1052 | return; |
| 1053 | } |
| 1054 | |
| 1055 | // Shadow prune — ruby's declarator shape is `assignment`. A |
| 1056 | // constant-typed LHS has no named children → constants are never |
| 1057 | // counted (never pruned); identifier LHSes (and each identifier of a |
| 1058 | // multiple-assign left) bump. |
| 1059 | let mut decl_counts: HashMap<&str, u32> = HashMap::new(); |
| 1060 | let mut dstack: Vec<Node> = vec![root]; |
| 1061 | let mut dvisited = 0usize; |
| 1062 | while let Some(n) = dstack.pop() { |
| 1063 | if dvisited >= MAX_VALUE_REF_NODES { |
| 1064 | break; |
| 1065 | } |
| 1066 | dvisited += 1; |
| 1067 | if n.kind() == "assignment" { |
| 1068 | let left = n |
| 1069 | .child_by_field_name("left") |
| 1070 | .or_else(|| n.child_by_field_name("pattern")) |
| 1071 | .or_else(|| n.named_child(0)); |
| 1072 | if let Some(left) = left { |
| 1073 | if left.kind() == "identifier" { |
| 1074 | let nm = self.text(left); |
| 1075 | if targets.contains_key(nm) { |
| 1076 | *decl_counts.entry(nm).or_insert(0) += 1; |
| 1077 | } |
| 1078 | } else { |
| 1079 | for i in 0..left.named_child_count() { |
| 1080 | if let Some(c) = left.named_child(i) { |
| 1081 | if c.kind() == "identifier" { |
| 1082 | let nm = self.text(c); |
| 1083 | if targets.contains_key(nm) { |
| 1084 | *decl_counts.entry(nm).or_insert(0) += 1; |
| 1085 | } |
| 1086 | } |
| 1087 | } |
| 1088 | } |
| 1089 | } |
| 1090 | } |
| 1091 | } |
| 1092 | for i in 0..n.named_child_count() { |
| 1093 | if let Some(c) = n.named_child(i) { |
| 1094 | dstack.push(c); |
| 1095 | } |
| 1096 | } |
| 1097 | } |
| 1098 | let shadowed: Vec<String> = decl_counts |
| 1099 | .iter() |
| 1100 | .filter(|(nm, c)| **c > counts.get(**nm).copied().unwrap_or(1)) |
| 1101 | .map(|(nm, _)| nm.to_string()) |