use crate::languages::LanguageProfile;
use tree_sitter::Node;
pub fn compute_cyclomatic(node: &Node, source: &[u8], profile: &dyn LanguageProfile) -> u64 {
let mut complexity: u64 = 1; walk_cyclomatic(node, source, profile, &mut complexity);
complexity
}
fn walk_cyclomatic(
node: &Node,
_source: &[u8],
profile: &dyn LanguageProfile,
complexity: &mut u64,
) {
let kind = node.kind();
let control_flow = profile.control_flow_nodes();
let boolean_ops = profile.boolean_operators();
let match_constructs = profile.match_construct_nodes();
let match_arms = profile.match_arm_nodes();
if control_flow.contains(&kind) && !is_any_else(kind) && !match_constructs.contains(&kind) {
*complexity += 1;
}
if match_arms.contains(&kind) {
*complexity += 1;
}
if boolean_ops.contains(&kind) {
*complexity += 1;
}
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
walk_cyclomatic(&child, _source, profile, complexity);
}
}
fn is_any_else(kind: &str) -> bool {
matches!(kind, "else_clause" | "else_statement" | "else")
}