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