#![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::*;
use crate::lang_helpers::bash::bash_test_connective;
const RUN_COMPARISONS: [u16; 4] = [
Bash::EQ as u16,
Bash::EQEQ as u16,
Bash::BANGEQ as u16,
Bash::EQTILDE as u16,
];
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub(super) struct ComparisonRuns(Vec<RunMember>);
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct RunMember {
id: usize,
start: usize,
end: usize,
run_end: usize,
}
impl ComparisonRuns {
fn sequence_end(&mut self, node: &Node, parent: Option<Node>) -> usize {
let (start, end) = (node.start_byte(), node.end_byte());
while self
.0
.last()
.is_some_and(|member| start < member.start || member.end < end)
{
self.0.pop();
}
let run_end = match (parent, self.0.last()) {
(Some(parent), Some(member)) if member.id == parent.id() => member.run_end,
_ => end,
};
if node.wraps_any(&RUN_COMPARISONS) {
self.0.push(RunMember {
id: node.id(),
start,
end,
run_end,
});
}
run_end
}
}
impl Cognitive for BashCode {
fn compute<'a>(
node: &Node<'a>,
code: &'a [u8],
ancestors: Ancestors<'a, '_>,
stats: &mut Stats,
nesting_map: &mut NestingMap,
) {
use Bash::*;
let mut nesting = get_nesting_from_map(node, nesting_map);
match node.kind_id().into() {
IfStatement | WhileStatement | ForStatement | CStyleForStatement | CaseStatement
| TernaryExpression | TernaryExpression2 => {
increase_nesting(stats, &mut nesting);
}
ElifClause | ElseClause => {
increment_branch_extension(stats);
}
List => {
compute_booleans(node, stats, AMPAMP, PIPEPIPE);
}
BinaryExpression3 => {
let end = stats.bash_runs.sequence_end(node, ancestors.parent(node));
compute_booleans_by_node(node, end, stats, |child| match child.kind_id().into() {
AMPAMP | PIPEPIPE => Some(child.kind_id()),
_ => bash_test_connective(child, node, code).map(|op| op as u16),
});
}
FunctionDefinition => {
enter_function_boundary(&mut nesting, node, ancestors, &[FunctionDefinition]);
}
_ => {}
}
nesting_map.insert(node.id(), nesting);
}
}