no-mistakes-postgres-engine 0.109.131

PostgreSQL embedded SQL extraction, schema catalog, and replay-safety analysis for no-mistakes
Documentation
use super::expression::compatible;
use super::{expression, Arm, Node, Recovered, ScopeVisitor, MAX_EMBEDDED_SQL_VARIANTS};
use crate::embedded::walk::variants::{conditions::choice_id, ValueKind};
use no_mistakes_ts_facts::syntax_helpers::unwrap_ts_wrappers;
use oxc_ast::ast::{BinaryOperator, Expression, LogicalOperator, UnaryOperator};
use oxc_span::GetSpan;

pub(super) fn recover(
    visitor: &ScopeVisitor<'_>,
    test: &Expression<'_>,
    depth: u8,
    constraints: &[(u64, u32)],
) -> Option<Vec<Recovered>> {
    let child_depth = depth.checked_sub(1)?;
    match unwrap_ts_wrappers(test) {
        Expression::BooleanLiteral(value) => Some(vec![boolean(value.value, Vec::new())]),
        Expression::UnaryExpression(unary) if unary.operator == UnaryOperator::LogicalNot => {
            let mut values = logical_operand(visitor, &unary.argument, child_depth, constraints)?;
            for value in &mut values {
                value.value = ValueKind::Boolean(!value.truth());
            }
            Some(values)
        }
        Expression::BinaryExpression(binary)
            if matches!(
                binary.operator,
                BinaryOperator::StrictEquality | BinaryOperator::StrictInequality
            ) =>
        {
            compare(
                strings(visitor, &binary.left, depth)?,
                strings(visitor, &binary.right, depth)?,
                binary.operator == BinaryOperator::StrictInequality,
                constraints,
            )
        }
        Expression::LogicalExpression(logical) if logical.operator != LogicalOperator::Coalesce => {
            let take_right = logical.operator == LogicalOperator::And;
            let mut result = Vec::new();
            for left in logical_operand(visitor, &logical.left, child_depth, constraints)? {
                if left.truth() == take_right {
                    let path = merge_choices(constraints, &left.choices);
                    for mut right in logical_operand(visitor, &logical.right, child_depth, &path)? {
                        right.choices = merge_choices(&left.choices, &right.choices);
                        push(&mut result, right)?;
                    }
                } else {
                    push(&mut result, left)?;
                }
            }
            Some(result)
        }
        _ => None,
    }
}

type StringChoice = (String, Vec<(u64, u32)>);

fn strings(
    visitor: &ScopeVisitor<'_>,
    operand: &Expression<'_>,
    depth: u8,
) -> Option<Vec<StringChoice>> {
    let operand = unwrap_ts_wrappers(operand);
    if !matches!(
        operand,
        Expression::Identifier(_)
            | Expression::StringLiteral(_)
            | Expression::StaticMemberExpression(_)
    ) {
        return None;
    }
    expression(visitor, operand, depth)?
        .into_iter()
        .map(|arm| match arm {
            Arm {
                node: Node::Text(text),
                choices,
                root: None,
                provenance: None,
                type_annotation,
            } if choices.is_empty() || type_annotation => Some((text, choices)),
            _ => None,
        })
        .collect()
}

fn compare(
    left: Vec<StringChoice>,
    right: Vec<StringChoice>,
    negate: bool,
    constraints: &[(u64, u32)],
) -> Option<Vec<Recovered>> {
    let mut result = Vec::new();
    for (left_text, left_choices) in left
        .iter()
        .filter(|(_, choices)| compatible(choices, constraints))
    {
        for (right_text, right_choices) in &right {
            if !compatible(right_choices, constraints) || !compatible(left_choices, right_choices) {
                continue;
            }
            let choices = merge_choices(left_choices, right_choices);
            let truth = (left_text == right_text) != negate;
            push(&mut result, boolean(truth, choices))?;
        }
    }
    Some(result)
}

fn boolean(truth: bool, choices: Vec<(u64, u32)>) -> Recovered {
    let mut value = Recovered::empty(1);
    value.value = ValueKind::Boolean(truth);
    value.choices = choices;
    value
}

fn merge_choices(left: &[(u64, u32)], right: &[(u64, u32)]) -> Vec<(u64, u32)> {
    let mut choices = left.to_vec();
    choices.extend_from_slice(right);
    choices.sort_unstable();
    choices.dedup();
    choices
}

fn push(values: &mut Vec<Recovered>, value: Recovered) -> Option<()> {
    if !values.contains(&value) {
        values.push(value);
    }
    (values.len() <= MAX_EMBEDDED_SQL_VARIANTS).then_some(())
}

fn logical_operand(
    visitor: &ScopeVisitor<'_>,
    test: &Expression<'_>,
    depth: u8,
    constraints: &[(u64, u32)],
) -> Option<Vec<Recovered>> {
    if let Some(values) = recover(visitor, test, depth, constraints) {
        return Some(values);
    }
    if depth == 0 {
        return None;
    }
    let operand = unwrap_ts_wrappers(test);
    if let Some(values) = strings(visitor, operand, depth) {
        return Some(
            values
                .into_iter()
                .filter(|(_, choices)| compatible(choices, constraints))
                .map(|(text, choices)| boolean(!text.is_empty(), choices))
                .collect(),
        );
    }
    let (id, truth_arm) = match operand {
        Expression::Identifier(_) => choice_id(visitor, test, test.span().start),
        Expression::StaticMemberExpression(_) => (u64::from(test.span().start) | (1 << 60), 1),
        _ => return None,
    };
    Some(
        [(false, 1 - truth_arm), (true, truth_arm)]
            .into_iter()
            .filter_map(|(truth, arm)| {
                let choices = vec![(id, arm)];
                compatible(&choices, constraints).then(|| boolean(truth, choices))
            })
            .collect(),
    )
}