#![allow(
clippy::enum_glob_use,
clippy::match_same_arms,
clippy::needless_pass_by_value,
clippy::wildcard_imports
)]
#![allow(
clippy::cast_precision_loss,
clippy::cast_possible_truncation,
clippy::cast_sign_loss
)]
use super::*;
fn kotlin_is_labeled_jump(node: &Node, code: &[u8]) -> bool {
node.child(0)
.filter(|c| c.kind_id() == Kotlin::Label as u16)
.and_then(|c| c.utf8_text(code))
.is_some_and(|t| t.starts_with("break@") || t.starts_with("continue@"))
}
impl Cognitive for KotlinCode {
fn compute<'a>(
node: &Node<'a>,
code: &'a [u8],
stats: &mut Stats,
nesting_map: &mut HashMap<usize, (usize, usize, usize)>,
) {
use Kotlin::*;
let (mut nesting, mut depth, mut lambda) = get_nesting_from_map(node, nesting_map);
match node.kind_id().into() {
IfExpression if !Self::is_else_if(node) => {
increase_nesting(stats, &mut nesting, depth, lambda);
}
ForStatement | WhileStatement | DoWhileStatement | WhenExpression | CatchBlock => {
increase_nesting(stats, &mut nesting, depth, lambda);
}
Else => {
let in_when = node.parent().is_some_and(|p| p.kind_id() == WhenEntry);
if !in_when {
increment_by_one(stats);
}
}
LabeledExpression if kotlin_is_labeled_jump(node, code) => {
increment_by_one(stats);
}
BinaryExpression => {
compute_booleans_with(node, stats, |id| {
matches!(id.into(), AMPAMP | PIPEPIPE | QMARKCOLON)
});
}
FunctionDeclaration | SecondaryConstructor => {
nesting = 0;
increment_function_depth(
&mut depth,
node,
&[FunctionDeclaration, SecondaryConstructor],
);
}
LambdaLiteral | AnonymousFunction => {
lambda += 1;
}
_ => {}
}
nesting_map.insert(node.id(), (nesting, depth, lambda));
}
}