use super::super::super::ScopeVisitor;
use super::super::Recovered;
use super::branch_paths;
use no_mistakes_ts_facts::syntax_helpers::unwrap_ts_wrappers;
use oxc_ast::ast::{Expression, LogicalExpression, LogicalOperator, UnaryOperator};
type Paths = Vec<Vec<(u64, u32)>>;
pub(super) fn branches(
visitor: &ScopeVisitor<'_>,
test: &Expression<'_>,
paths: &[Vec<(u64, u32)>],
id: u64,
truth_arm: u32,
) -> (Paths, Paths) {
let mut expression = unwrap_ts_wrappers(test);
let mut invert = false;
while let Expression::UnaryExpression(unary) = expression {
if unary.operator != UnaryOperator::LogicalNot {
break;
}
invert = !invert;
expression = unwrap_ts_wrappers(&unary.argument);
}
recovered_paths(visitor, expression, paths, id, truth_arm, |value| {
Some(value.truth() != invert)
})
}
pub(super) fn logical_branches(
visitor: &ScopeVisitor<'_>,
expression: &LogicalExpression<'_>,
paths: &[Vec<(u64, u32)>],
id: u64,
right_arm: u32,
) -> (Paths, Paths) {
match expression.operator {
LogicalOperator::Coalesce => recovered_paths(
visitor,
&expression.left,
paths,
id,
right_arm,
Recovered::nullish,
),
LogicalOperator::And => branches(visitor, &expression.left, paths, id, right_arm),
LogicalOperator::Or => {
let (retained, replaced) =
branches(visitor, &expression.left, paths, id, 1 - right_arm);
(replaced, retained)
}
}
}
fn recovered_paths(
visitor: &ScopeVisitor<'_>,
test: &Expression<'_>,
paths: &[Vec<(u64, u32)>],
id: u64,
truth_arm: u32,
truth: impl Fn(&Recovered) -> Option<bool>,
) -> (Paths, Paths) {
let Some(values) = visitor.recover_variants(test) else {
return (
branch_paths(paths, id, truth_arm),
branch_paths(paths, id, 1 - truth_arm),
);
};
value_paths(values, paths, id, truth_arm, truth)
}
pub(super) fn finite_branches(
visitor: &ScopeVisitor<'_>,
test: &Expression<'_>,
paths: &[Vec<(u64, u32)>],
id: u64,
truth_arm: u32,
) -> Option<(Paths, Paths)> {
let mut values = Vec::new();
for path in paths {
for value in super::super::super::closed_values::recover_predicate(visitor, test, path)? {
if !values.contains(&value) {
values.push(value);
}
if values.len() > crate::embedded::MAX_EMBEDDED_SQL_VARIANTS {
return None;
}
}
}
if !values
.iter()
.any(|value| value.choices.iter().any(|(id, _)| id & (1 << 62) != 0))
{
return None;
}
Some(value_paths(values, paths, id, truth_arm, |value| {
Some(value.truth())
}))
}
fn value_paths(
values: Vec<Recovered>,
paths: &[Vec<(u64, u32)>],
id: u64,
truth_arm: u32,
truth: impl Fn(&Recovered) -> Option<bool>,
) -> (Paths, Paths) {
let mut yes = Vec::new();
let mut no = Vec::new();
for value in values {
let taken = truth(&value);
let mut compatible = paths.to_vec();
for (choice_id, choice_arm) in value.choices {
compatible = branch_paths(&compatible, choice_id, choice_arm);
}
for (arm, output) in [(truth_arm, &mut yes), (1 - truth_arm, &mut no)] {
if taken.is_some_and(|taken| taken != (arm == truth_arm)) {
continue;
}
for path in branch_paths(&compatible, id, arm) {
if !output.contains(&path) {
output.push(path);
}
}
}
}
(yes, no)
}