#[macro_use]
mod scope_visits;
mod callback_transactions;
mod class_receivers;
mod delayed;
mod isolation;
mod object_params;
mod receiver_types;
mod type_shadows;
mod undeclared;
mod visitor;
use super::super::relative::PendingRelativeSpan;
use super::candidates::ImportClassification;
use super::owners::{self, NameHit};
use super::ScopedExecutors;
use oxc_ast::ast::BindingPattern;
use oxc_span::Span;
use std::collections::{HashMap, HashSet};
#[derive(Default)]
pub(super) struct ScopeCollector {
delayed: delayed::Bindings,
pub(super) track_resource_transactions: bool,
factories: HashSet<String>,
factory_shadow_names: HashSet<String>,
types: HashSet<String>,
transaction_types: HashSet<String>,
transaction_parameters: Vec<(String, String, Span)>,
factory_shadows: HashMap<String, Vec<Span>>,
callback_candidates: Vec<callback_transactions::Candidate>,
resource_isolation: Vec<(u32, isolation::Initializer)>,
undeclared: HashMap<String, super::super::UndeclaredExecutorType>,
provisional_factories: HashMap<String, Vec<u32>>,
provisional_types: HashMap<String, Vec<u32>>,
scopes: Vec<Span>,
var_scopes: Vec<Span>,
type_shadows: Vec<(String, Span)>,
classes: Vec<Span>,
class_methods: HashSet<u32>,
pending_annotations: Vec<type_shadows::PendingAnnotation>,
pub(super) found: ScopedExecutors,
pub(super) spans: Vec<PendingRelativeSpan>,
}
impl ScopeCollector {
pub(super) fn needs_walk(&self) -> bool {
!self.undeclared.is_empty()
|| !self.factories.is_empty()
|| !self.types.is_empty()
|| !self.provisional_factories.is_empty()
|| !self.provisional_types.is_empty()
}
fn with_scope(&mut self, span: Span, walk: impl FnOnce(&mut Self)) {
self.scopes.push(span);
walk(self);
self.scopes.pop();
}
fn record_factory(
&mut self,
name: &str,
span: Span,
init: &oxc_ast::ast::Expression<'_>,
confirmed: bool,
) {
if confirmed || self.track_resource_transactions {
self.found
.transactions
.entry(name.into())
.or_default()
.push(span);
}
if self.track_resource_transactions {
if let Some(initializer) = isolation::from_initializer(init) {
self.resource_isolation.push((span.start, initializer));
}
}
}
pub(super) fn finish_callback_transactions(&mut self) {
for (binding, initializer) in self.resource_isolation.drain(..) {
if !is_shadowed(
&initializer.factory,
initializer.call_start,
&self.factory_shadows,
) {
self.found
.transaction_isolation
.insert(binding, initializer.isolation);
}
}
self.finalize_callbacks();
}
pub(super) fn record_callback_function_shadow(
&mut self,
function: &oxc_ast::ast::Function<'_>,
) {
callback_transactions::record_function_shadow(
function,
*self.scopes.last().expect("scope"),
&self.factory_shadow_names,
&mut self.factory_shadows,
);
}
}
fn is_shadowed(factory: &str, at: u32, shadows: &HashMap<String, Vec<Span>>) -> bool {
shadows
.get(factory)
.is_some_and(|spans| spans.iter().any(|span| span.start <= at && at < span.end))
}
fn bound_names(pattern: &BindingPattern<'_>, names: &mut HashSet<String>) {
match pattern {
BindingPattern::BindingIdentifier(identifier) => {
names.insert(identifier.name.to_string());
}
BindingPattern::ObjectPattern(object) => {
for property in &object.properties {
bound_names(&property.value, names);
}
if let Some(rest) = &object.rest {
bound_names(&rest.argument, names);
}
}
BindingPattern::ArrayPattern(array) => {
for element in array.elements.iter().flatten() {
bound_names(element, names);
}
if let Some(rest) = &array.rest {
bound_names(&rest.argument, names);
}
}
BindingPattern::AssignmentPattern(assignment) => bound_names(&assignment.left, names),
}
}
#[cfg(all(test, any(nm_shard_all, nm_shard_0)))]
mod delayed_tests;