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