in src/metrics/cognitive.rs [234:304]
fn compute(
node: &Node,
stats: &mut Stats,
nesting_map: &mut HashMap<usize, (usize, usize, usize)>,
) {
use Python::*;
// Get nesting of the parent
let (mut nesting, mut depth, mut lambda) = get_nesting_from_map(node, nesting_map);
match node.kind_id().into() {
IfStatement | ForStatement | WhileStatement | ConditionalExpression => {
increase_nesting(stats, &mut nesting, depth, lambda);
}
ElifClause => {
// No nesting increment for them because their cost has already
// been paid by the if construct
increment_by_one(stats);
// Reset the boolean sequence
stats.boolean_seq.reset();
}
ElseClause | FinallyClause => {
// No nesting increment for them because their cost has already
// been paid by the if construct
increment_by_one(stats);
}
ExceptClause => {
nesting += 1;
increment(stats);
}
ExpressionList | ExpressionStatement | Tuple => {
stats.boolean_seq.reset();
}
NotOperator => {
stats.boolean_seq.not_operator(node.kind_id());
}
BooleanOperator => {
if node.count_specific_ancestors::<PythonParser>(
|node| node.kind_id() == BooleanOperator,
|node| node.kind_id() == Lambda,
) == 0
{
stats.structural += node.count_specific_ancestors::<PythonParser>(
|node| node.kind_id() == Lambda,
|node| {
matches!(
node.kind_id().into(),
ExpressionList | IfStatement | ForStatement | WhileStatement
)
},
);
}
compute_booleans::<language_python::Python>(node, stats, And, Or);
}
Lambda => {
// Increase lambda nesting
lambda += 1;
}
FunctionDefinition => {
// Increase depth function nesting if needed
increment_function_depth::<language_python::Python>(
&mut depth,
node,
FunctionDefinition,
);
}
_ => {}
}
// Add node to nesting map
nesting_map.insert(node.id(), (nesting, depth, lambda));
}