no-mistakes-postgres-engine 0.109.130

PostgreSQL embedded SQL extraction, schema catalog, and replay-safety analysis for no-mistakes
Documentation
use super::{Parameter, ScopeVisitor};
use oxc_ast::ast::{Argument, CallExpression};
use oxc_ast_visit::{walk, Visit};
use oxc_span::GetSpan;
use std::ops::Range;

type ProjectedCall = (usize, Vec<Parameter>, Option<(usize, String)>);

#[derive(Default)]
pub(in crate::embedded::walk) struct ProjectedBinds {
    pub(super) confirmed: Vec<ProjectedCall>,
    pub(super) pending: Vec<ProjectedCall>,
}

pub(in crate::embedded::walk) struct Capture {
    first_argument: u32,
    values: Vec<Option<String>>,
}

impl<'a> ScopeVisitor<'a> {
    pub(in crate::embedded::walk) fn walk_with_lock_helper_arguments(
        &mut self,
        call: &CallExpression<'a>,
        binds: ProjectedBinds,
        imported: Range<usize>,
    ) {
        if binds.confirmed.is_empty() && binds.pending.is_empty() && imported.is_empty() {
            walk::walk_call_expression(self, call);
            return;
        }
        let Some(first) = call.arguments.first() else {
            walk::walk_call_expression(self, call);
            return;
        };
        let outer = self.helper_argument_capture.replace(Capture {
            first_argument: first.span().start,
            values: Vec::new(),
        });
        walk::walk_call_expression(self, call);
        let captured = std::mem::replace(&mut self.helper_argument_capture, outer);
        let values = captured.map_or_else(Vec::new, |capture| capture.values);
        for (index, parameters, receiver) in binds.confirmed {
            let call = &mut self.calls[index];
            if receiver_changed(receiver, &values) {
                call.forget_transaction();
            }
            call.bound_parameters = resolve(parameters, &values);
        }
        for (index, parameters, receiver) in binds.pending {
            let call = &mut self.pending_calls[index].call;
            if receiver_changed(receiver, &values) {
                call.forget_transaction();
            }
            call.bound_parameters = resolve(parameters, &values);
        }
        for invocation in &mut self.imported_lock_helpers[imported] {
            invocation.arguments.clone_from(&values);
        }
    }

    pub(in crate::embedded::walk) fn capture_lock_helper_arguments(
        &mut self,
        arguments: &[Argument<'a>],
    ) -> bool {
        if !self
            .helper_argument_capture
            .as_ref()
            .is_some_and(|capture| {
                arguments
                    .first()
                    .is_some_and(|argument| argument.span().start == capture.first_argument)
            })
        {
            return false;
        }
        let mut values = Vec::with_capacity(arguments.len());
        for argument in arguments {
            values.push(
                argument
                    .as_expression()
                    .and_then(|expression| super::super::transaction::identity(self, expression)),
            );
            self.visit_argument(argument);
        }
        if let Some(capture) = &mut self.helper_argument_capture {
            capture.values = values;
        }
        true
    }
}

fn resolve(parameters: Vec<Parameter>, values: &[Option<String>]) -> Vec<Option<String>> {
    parameters
        .into_iter()
        .map(|parameter| match parameter {
            Parameter::Argument(index) => values.get(index).cloned().flatten(),
            Parameter::Literal(value) => Some(value),
            Parameter::Unknown => None,
        })
        .collect()
}

fn receiver_changed(receiver: Option<(usize, String)>, values: &[Option<String>]) -> bool {
    receiver.is_some_and(|(index, initial)| {
        values.get(index).and_then(Option::as_deref) != Some(initial.as_str())
    })
}