use tree_sitter::{Node, Query, QueryCursor};
#[must_use]
pub fn calculate_max_depth(node: Node) -> usize {
walk_depth(node, 0)
}
fn walk_depth(node: Node, current: usize) -> usize {
let mut max = current;
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
let kind = child.kind();
if matches!(
kind,
"if_expression"
| "match_expression"
| "for_expression"
| "while_expression"
| "loop_expression"
| "if_statement"
| "for_statement"
| "for_in_statement"
| "while_statement"
| "do_statement"
| "switch_case"
| "catch_clause"
| "try_statement"
| "closure_expression"
| "arrow_function"
| "function_expression"
| "lambda"
) {
max = std::cmp::max(max, walk_depth(child, current + 1));
} else {
max = std::cmp::max(max, walk_depth(child, current));
}
}
max
}
#[must_use]
pub fn calculate_complexity(node: Node, source: &str, query: &Query) -> usize {
let mut cursor = QueryCursor::new();
let mut complexity = 1;
for _ in cursor.matches(query, node, source.as_bytes()) {
complexity += 1;
}
complexity
}
#[must_use]
pub fn count_arguments(node: Node) -> usize {
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
if child.kind() == "parameters" || child.kind() == "formal_parameters" {
return child.named_child_count();
}
}
0
}