#![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],
ancestors: Ancestors<'a, '_>,
stats: &mut Stats,
nesting_map: &mut NestingMap,
) {
use Kotlin::*;
let mut nesting = get_nesting_from_map(node, nesting_map);
match node.kind_id().into() {
IfExpression if !Self::is_else_if(node, ancestors) => {
increase_nesting(stats, &mut nesting);
}
ForStatement | WhileStatement | DoWhileStatement | WhenExpression | CatchBlock => {
increase_nesting(stats, &mut nesting);
}
Else => {
let in_when = ancestors
.parent(node)
.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 | Getter | Setter | AnonymousInitializer => {
enter_function_boundary(
&mut nesting,
node,
ancestors,
&[
FunctionDeclaration,
SecondaryConstructor,
Getter,
Setter,
AnonymousInitializer,
],
);
}
LambdaLiteral | AnonymousFunction => {
nesting.lambda += 1;
}
_ => {}
}
nesting_map.insert(node.id(), nesting);
}
}