no-mistakes-postgres-engine 0.109.125

PostgreSQL embedded SQL extraction, schema catalog, and replay-safety analysis for no-mistakes
Documentation
use super::{scope, BindingState, ScopeVisitor};
use oxc_ast::ast::{ReturnStatement, TryStatement};
use oxc_ast_visit::{walk, Visit};
use std::collections::HashMap;

impl ScopeVisitor<'_> {
    pub(super) fn invalidate_exited_execution(&mut self) {
        let owner = self
            .member_query_proof
            .function_ids
            .last()
            .copied()
            .unwrap_or(0);
        self.exited_functions.insert(owner);
        self.invalidate_execution_variants();
    }

    pub(super) fn invalidate_execution_variants(&mut self) {
        let owner = self.function_scopes.last().copied().unwrap_or(0);
        for scope in &mut self.scopes[owner..] {
            for binding in scope.values_mut() {
                binding.variants = None;
            }
        }
    }
}

pub(super) fn try_statement<'a>(visitor: &mut ScopeVisitor<'a>, statement: &TryStatement<'a>) {
    if visitor.executor_options.track_transaction_helpers {
        visitor.transaction_options.suspended += 1;
    }
    let mut before = visitor.scopes.clone();
    visitor.visit_block_statement(&statement.block);
    invalidate_exception_state(visitor, &before);
    if let Some(handler) = &statement.handler {
        before = visitor.scopes.clone();
        visitor.visit_catch_clause(handler);
        invalidate_exception_state(visitor, &before);
    }
    if let Some(finalizer) = &statement.finalizer {
        before = visitor.scopes.clone();
        visitor.visit_block_statement(finalizer);
        invalidate_exception_state(visitor, &before);
    }
    if visitor.executor_options.track_transaction_helpers {
        visitor.transaction_options.suspended -= 1;
    }
}

fn invalidate_exception_state(
    visitor: &mut ScopeVisitor<'_>,
    before: &[HashMap<String, BindingState>],
) {
    let owner = visitor.function_scopes.last().copied().unwrap_or(0);
    for (index, bindings) in visitor.scopes.iter_mut().enumerate().skip(owner) {
        for (name, binding) in bindings {
            let changed = before
                .get(index)
                .and_then(|scope| scope.get(name))
                .is_none_or(|old| {
                    (
                        &old.sql,
                        old.kind,
                        old.builder_identity,
                        &old.variants,
                        old.builder_effect_revision,
                    ) != (
                        &binding.sql,
                        binding.kind,
                        binding.builder_identity,
                        &binding.variants,
                        binding.builder_effect_revision,
                    )
                });
            // A handler can see any earlier point in the preceding phase,
            // including the value before its last successful mutation.
            if changed {
                scope::mark_binding_dynamic_keep_known_statement(binding);
            }
            binding.variants = None;
        }
    }
}
pub(super) fn returned<'a>(visitor: &mut ScopeVisitor<'a>, statement: &ReturnStatement<'a>) {
    if let Some(argument) = &statement.argument {
        // A returned executor can be mutated outside this lexical scope.
        visitor.revoke_query_receiver(argument);
        visitor.note_returned_builder_fragments(argument);
    }
    // Returning a builder is treated as an escape below; an IIFE's result
    // needs the state it had just before that.
    visitor.capture_iife(statement);
    let suppress = visitor.consume_return(statement);
    if suppress {
        visitor.suppress_nested_builder_fragments += 1;
    }
    walk::walk_return_statement(visitor, statement);
    if suppress {
        visitor.suppress_nested_builder_fragments -= 1;
    }
    visitor.invalidate_exited_execution();
}