use super::{bound_names, is_shadowed, owners, ScopeCollector};
use no_mistakes_postgres_sql::statement_facts::SqlIsolation;
use no_mistakes_ts_facts::syntax_helpers::unwrap_ts_wrappers;
use oxc_ast::ast::{BindingPattern, CallExpression, Expression, FormalParameters};
use oxc_span::{GetSpan, Span};
use std::collections::{HashMap, HashSet};
pub(super) struct Candidate {
factory: String,
call_start: u32,
binding: Option<(String, u32)>,
scope: Option<Span>,
isolation: SqlIsolation,
}
pub(super) fn candidate(
call: &CallExpression<'_>,
factories: &HashSet<String>,
) -> Option<Candidate> {
let Expression::Identifier(factory) = unwrap_ts_wrappers(&call.callee) else {
return None;
};
factories.contains(factory.name.as_str()).then_some(())?;
let callback = unwrap_ts_wrappers(call.arguments.first()?.as_expression()?);
let params = match callback {
Expression::ArrowFunctionExpression(callback) => Some(&callback.params),
Expression::FunctionExpression(callback) => Some(&callback.params),
Expression::ObjectExpression(_) | Expression::ArrayExpression(_) => return None,
literal if literal.is_literal() || literal.is_string_literal() => return None,
_ => None,
};
let binding = params
.and_then(|params| params.items.first())
.and_then(|param| match ¶m.pattern {
BindingPattern::BindingIdentifier(name) => {
Some((name.name.to_string(), name.span.start))
}
_ => None,
});
Some(Candidate {
factory: factory.name.to_string(),
call_start: call.span.start,
binding,
scope: params.map(|_| callback.span()),
isolation: super::isolation::from_argument(call.arguments.get(1)),
})
}
impl ScopeCollector {
pub(super) fn finalize_callbacks(&mut self) {
for candidate in std::mem::take(&mut self.callback_candidates) {
if is_shadowed(
&candidate.factory,
candidate.call_start,
&self.factory_shadows,
) {
continue;
}
let Some(scope) = candidate.scope else {
self.found
.opaque_transaction_calls
.insert(candidate.call_start);
continue;
};
self.found.transaction_callback_starts.insert(scope.start);
let Some((name, binding_start)) = candidate.binding else {
continue;
};
let hit = owners::name_hit(
&self.factories,
&self.provisional_factories,
&candidate.factory,
);
let span = Span::new(binding_start, scope.end);
self.found.transaction_callbacks.insert(binding_start);
if hit.confirmed {
self.found
.transactions
.entry(name.clone())
.or_default()
.push(span);
}
self.record(&name, span, hit);
self.found
.transaction_isolation
.insert(binding_start, candidate.isolation);
}
}
}
pub(super) fn record_parameter_shadows(
params: &FormalParameters<'_>,
scope: Span,
factories: &HashSet<String>,
shadows: &mut HashMap<String, Vec<Span>>,
) {
for param in ¶ms.items {
let mut names = HashSet::new();
bound_names(¶m.pattern, &mut names);
for name in names {
if factories.contains(&name) {
record_shadow(&name, scope, shadows);
}
}
}
if let Some(rest) = ¶ms.rest {
let mut names = HashSet::new();
bound_names(&rest.rest.argument, &mut names);
for name in names {
if factories.contains(&name) {
record_shadow(&name, scope, shadows);
}
}
}
}
pub(super) fn record_shadow(name: &str, span: Span, shadows: &mut HashMap<String, Vec<Span>>) {
shadows.entry(name.to_string()).or_default().push(span);
}
pub(super) fn record_function_shadow(
function: &oxc_ast::ast::Function<'_>,
outer_scope: Span,
factories: &HashSet<String>,
shadows: &mut HashMap<String, Vec<Span>>,
) {
let Some(name) = &function.id else { return };
if !factories.contains(name.name.as_str()) {
return;
}
let scope = match function.r#type {
oxc_ast::ast::FunctionType::FunctionDeclaration
| oxc_ast::ast::FunctionType::TSDeclareFunction => outer_scope,
oxc_ast::ast::FunctionType::FunctionExpression
| oxc_ast::ast::FunctionType::TSEmptyBodyFunctionExpression => function.span,
};
record_shadow(name.name.as_str(), scope, shadows);
}
pub(super) fn record_pattern_shadows(
pattern: &BindingPattern<'_>,
scope: Span,
factories: &HashSet<String>,
shadows: &mut HashMap<String, Vec<Span>>,
) {
let mut names = HashSet::new();
bound_names(pattern, &mut names);
for name in names {
if factories.contains(&name) {
record_shadow(&name, Span::new(pattern.span().start, scope.end), shadows);
}
}
}
pub(super) fn record_local_shadows(
declaration: &oxc_ast::ast::VariableDeclaration<'_>,
function_scope: Option<Span>,
block_scope: Span,
factories: &HashSet<String>,
shadows: &mut HashMap<String, Vec<Span>>,
) {
for declarator in &declaration.declarations {
let mut names = HashSet::new();
bound_names(&declarator.id, &mut names);
let scope = if declaration.kind == oxc_ast::ast::VariableDeclarationKind::Var {
function_scope.unwrap_or(block_scope)
} else {
block_scope
};
for name in names {
if factories.contains(&name) {
record_shadow(
&name,
Span::new(scope.start.min(declarator.id.span().start), scope.end),
shadows,
);
}
}
}
}