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();
if statement.handler.is_some() {
let outer_try = visitor
.caught_try
.replace((statement.block.span.start, visitor.control_depth));
visitor.with_caught_effects(|visitor| visitor.visit_block_statement(&statement.block));
visitor.caught_try = outer_try;
} else {
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,
)
});
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 {
visitor.revoke_query_receiver(argument);
visitor.note_returned_builder_fragments(argument);
}
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();
}