#![allow(
clippy::enum_glob_use,
clippy::too_many_lines,
clippy::wildcard_imports
)]
#![allow(
clippy::cast_precision_loss,
clippy::cast_possible_truncation,
clippy::cast_sign_loss
)]
use super::{Abc, Stats};
use crate::macros::ruby_bool_terminal_kinds;
use crate::*;
fn ruby_inspect_container(container_node: &Node, conditions: &mut f64) {
use Ruby::*;
let mut node = *container_node;
let mut node_kind = node.kind_id().into();
let Some(parent) = node.parent() else { return };
let mut has_boolean_content = matches!(
parent.kind_id().into(),
Binary | Binary2 | Binary3 | If | Unless | While | Until
);
loop {
let is_parens = matches!(node_kind, ParenthesizedStatements);
let is_not = matches!(node_kind, Unary | Unary2 | Unary3 | Unary4 | Unary5)
&& node.child(0).is_some_and(|c| c.kind_id() == BANG as u16);
if !is_parens && !is_not {
break;
}
if !has_boolean_content && is_not {
has_boolean_content = true;
}
let next = if is_not {
node.child(1)
} else {
node.children().find(Node::is_named)
};
let Some(child) = next else { break };
node = child;
node_kind = node.kind_id().into();
if matches!(node_kind, ruby_bool_terminal_kinds!()) {
if has_boolean_content {
*conditions += 1.;
}
break;
}
}
}
fn ruby_count_unary_conditions(list_node: &Node, conditions: &mut f64) {
use Ruby::*;
let list_kind = list_node.kind_id().into();
let mut cursor = list_node.cursor();
if cursor.goto_first_child() {
loop {
let node = cursor.node();
let node_kind = node.kind_id().into();
if matches!(node_kind, ruby_bool_terminal_kinds!())
&& matches!(list_kind, Binary | Binary2 | Binary3)
{
*conditions += 1.;
} else if node.is_named() {
ruby_inspect_container(&node, conditions);
}
if !cursor.goto_next_sibling() {
break;
}
}
}
}
fn ruby_count_condition(condition: &Node, conditions: &mut f64) {
use Ruby::*;
let kind = condition.kind_id().into();
if matches!(kind, ruby_bool_terminal_kinds!()) {
*conditions += 1.;
} else if matches!(
kind,
ParenthesizedStatements | Unary | Unary2 | Unary3 | Unary4 | Unary5
) {
ruby_inspect_container(condition, conditions);
}
}
impl Abc for RubyCode {
fn compute<'a>(node: &Node<'a>, code: &'a [u8], stats: &mut Stats) {
use Ruby::*;
match node.kind_id().into() {
Assignment | Assignment2 | OperatorAssignment | OperatorAssignment2 => {
stats.assignments += 1.;
}
If | Unless | While | Until | IfModifier | UnlessModifier | WhileModifier
| UntilModifier => {
if let Some(cond) = node.child_by_field_name("condition") {
ruby_count_condition(&cond, &mut stats.conditions);
}
}
Call | Call2 | Call3 | Call4 | Super | Yield | Yield2 => {
stats.branches += 1.;
}
EQEQ | BANGEQ | EQEQEQ | LT | GT | LTEQ | GTEQ | LTEQGT | EQTILDE | BANGTILDE
| Else | Elsif | When | QMARK | Rescue | RescueModifier | RescueModifier2
| RescueModifier3 => {
stats.conditions += 1.;
}
InClause if crate::metrics::npa::ruby_in_clause_counts(node, code) => {
stats.conditions += 1.;
}
AMPAMP | PIPEPIPE | And | Or => {
if let Some(parent) = node.parent() {
ruby_count_unary_conditions(&parent, &mut stats.conditions);
}
}
_ => {}
}
}
}