#![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 python_comprehension_clause_nesting(
node: &Node,
nesting: usize,
depth: usize,
lambda: usize,
nesting_map: &mut HashMap<usize, (usize, usize, usize)>,
) -> usize {
use Python::*;
let mut for_count = 0;
let mut last_clause_is_if = false;
for child in node.children() {
let kind = child.kind_id();
if kind == ForInClause as u16 {
nesting_map.insert(child.id(), (nesting + for_count, depth, lambda));
for_count += 1;
last_clause_is_if = false;
} else if kind == IfClause as u16 {
nesting_map.insert(child.id(), (nesting + for_count, depth, lambda));
last_clause_is_if = true;
}
}
for_count + usize::from(last_clause_is_if)
}
fn python_apply_boolean_operator(node: &Node, stats: &mut Stats) {
use Python::*;
if node.count_specific_ancestors::<PythonCode>(
|node| node.kind_id() == BooleanOperator,
python_is_lambda,
) == 0
{
stats.structural += node.count_specific_ancestors::<PythonCode>(python_is_lambda, |node| {
matches!(
node.kind_id().into(),
ExpressionList | IfStatement | ForStatement | WhileStatement
)
});
}
compute_booleans(node, stats, And, Or);
}
impl Cognitive for PythonCode {
fn compute<'a>(
node: &Node<'a>,
_code: &'a [u8],
stats: &mut Stats,
nesting_map: &mut HashMap<usize, (usize, usize, usize)>,
) {
use Python::*;
let (mut nesting, mut depth, mut lambda) = get_nesting_from_map(node, nesting_map);
match node.kind_id().into() {
IfStatement if !Self::is_else_if(node) => {
increase_nesting(stats, &mut nesting, depth, lambda);
}
ForStatement | WhileStatement | ConditionalExpression | MatchStatement => {
increase_nesting(stats, &mut nesting, depth, lambda);
}
ListComprehension
| DictionaryComprehension
| SetComprehension
| GeneratorExpression => {
nesting +=
python_comprehension_clause_nesting(node, nesting, depth, lambda, nesting_map);
}
ForInClause | IfClause => {
if let Some(&(clause_nesting, _, _)) = nesting_map.get(&node.id()) {
nesting = clause_nesting;
}
stats.nesting = nesting + depth + lambda;
increment(stats);
stats.boolean_seq.reset();
}
ElifClause => {
increment_branch_extension(stats);
}
ElseClause => {
increment_by_one(stats);
}
ExceptClause => {
increase_nesting(stats, &mut nesting, depth, lambda);
}
ExpressionList | ExpressionStatement | Tuple => {
stats.boolean_seq.reset();
}
BooleanOperator => python_apply_boolean_operator(node, stats),
Lambda | Lambda2 => {
lambda += 1;
}
FunctionDefinition => {
increment_function_depth(&mut depth, node, &[FunctionDefinition]);
}
_ => {}
}
nesting_map.insert(node.id(), (nesting, depth, lambda));
}
}