Inspect full recorded UTF-8 content with the registry's configured grammar. Errors are explicit coverage gaps, never empty successful inspections.
(
registry: &mut ParserRegistry,
path: &str,
source: &str,
)
| 153 | /// Inspect full recorded UTF-8 content with the registry's configured grammar. |
| 154 | /// Errors are explicit coverage gaps, never empty successful inspections. |
| 155 | pub fn inspect( |
| 156 | registry: &mut ParserRegistry, |
| 157 | path: &str, |
| 158 | source: &str, |
| 159 | ) -> Result<StructuralInspection, &'static str> { |
| 160 | let language = Language::detect(path).ok_or("Unsupported language")?; |
| 161 | if source.len() > MAX_SOURCE_BYTES { |
| 162 | return Err("Source exceeds the 1 MiB structural inspection budget"); |
| 163 | } |
| 164 | let tree = registry |
| 165 | .parse_tree(path, source) |
| 166 | .ok_or("Parser returned no syntax tree")?; |
| 167 | let has_syntax_errors = tree.root_node().has_error(); |
| 168 | let mut issues = Vec::new(); |
| 169 | let mut nodes = Vec::new(); |
| 170 | let mut stack = vec![(tree.root_node(), 0)]; |
| 171 | while let Some((node, depth)) = stack.pop() { |
| 172 | if depth > 256 { |
| 173 | return Err("Audit AST nesting budget exceeded; inspection is incomplete"); |
| 174 | } |
| 175 | nodes.push(node); |
| 176 | if nodes.len() > 300_000 { |
| 177 | return Err("Audit AST node budget exceeded"); |
| 178 | } |
| 179 | let rule = if has_syntax_errors { |
| 180 | (node.is_error() || node.is_missing()).then_some(Rule::Syntax) |
| 181 | } else if language == Language::Rust && node.kind() == "macro_invocation" { |
| 182 | node.child_by_field_name("macro") |
| 183 | .and_then(|name| name.utf8_text(source.as_bytes()).ok()) |
| 184 | .filter(|name| { |
| 185 | matches!( |
| 186 | name.rsplit("::").next().map(str::trim), |
| 187 | Some("todo" | "unimplemented") |
| 188 | ) |
| 189 | }) |
| 190 | .map(|_| Rule::RustPlaceholder) |
| 191 | } else if matches!(language, Language::TypeScript | Language::Tsx) |
| 192 | && node.kind() == "catch_clause" |
| 193 | { |
| 194 | // A comment may explain intentional suppression. Leave that case to |
| 195 | // substantive review rather than calling it an undocumented discard. |
| 196 | node.child_by_field_name("body") |
| 197 | .filter(|body| body.named_child_count() == 0) |
| 198 | .map(|_| Rule::EmptyCatch) |
| 199 | } else { |
| 200 | None |
| 201 | }; |
| 202 | if let Some(rule) = rule { |
| 203 | let text = node |
| 204 | .utf8_text(source.as_bytes()) |
| 205 | .map_err(|_| "Invalid AST source range")?; |
| 206 | issues.push(StructuralIssue { |
| 207 | rule, |
| 208 | start_line: node.start_position().row as u32 + 1, |
| 209 | end_line: node.end_position().row as u32 |
| 210 | + u32::from( |
| 211 | node.end_position().column > 0 |
| 212 | || node.end_position().row == node.start_position().row, |