use super::relative::{PendingRelativeSpan, RelativeScopedCandidate};
use super::EmbeddedSqlOptions;
use candidates::classify;
use collector::ScopeCollector;
use oxc_ast::ast::{ImportDeclarationSpecifier, ImportOrExportKind, Program, Statement};
use oxc_ast_visit::Visit;
use oxc_span::Span;
use std::collections::HashMap;
mod candidates;
mod collector;
mod owners;
pub fn from_configured_module(source: &str, specifier: &str) -> bool {
candidates::from_configured_module(source, specifier)
}
pub type PreparedScopes = std::collections::BTreeMap<EmbeddedSqlOptions, ScopedCollection>;
pub struct ScopedCollection {
pub executors: ScopedExecutors,
pub(super) candidates: Vec<RelativeScopedCandidate>,
pub(super) spans: Vec<PendingRelativeSpan>,
}
impl ScopedCollection {
pub(super) fn prune_unused_diagnostic_candidates(
&mut self,
calls: &[super::UndeclaredExecutorType],
) {
if calls.is_empty()
&& self
.candidates
.iter()
.all(|candidate| !candidate.factory && !candidate.type_import)
{
self.candidates.clear();
}
}
}
pub(super) type UndeclaredReceiverTypes = HashMap<String, super::UndeclaredExecutorType>;
#[derive(Debug, Default)]
pub struct ScopedExecutors {
spans: HashMap<String, Vec<Span>>,
pub(super) transactions: HashMap<String, Vec<Span>>,
pub(super) transaction_parameters: crate::fx::FxHashSet<u32>,
pub(super) delayed: std::collections::HashSet<u32>,
pub(super) undeclared: HashMap<u32, HashMap<String, UndeclaredReceiverTypes>>,
pub(super) undeclared_this: Vec<(Span, UndeclaredReceiverTypes)>,
pub(super) undeclared_this_exclusions: Vec<Span>,
pub(super) undeclared_imports: HashMap<String, super::UndeclaredExecutorType>,
}
impl ScopedExecutors {
pub fn delayed_reference(&self, start: u32) -> bool {
self.delayed.contains(&start)
}
pub(super) fn contains_declaration(&self, name: &str, start: u32) -> bool {
self.spans
.get(name)
.is_some_and(|spans| spans.iter().any(|span| span.start == start))
}
pub(super) fn contains(&self, name: &str, position: u32) -> bool {
self.spans.get(name).is_some_and(|spans| {
spans
.iter()
.any(|span| span.start <= position && position < span.end)
})
}
pub(super) fn add(&mut self, name: &str, span: Span) {
self.spans.entry(name.to_string()).or_default().push(span);
}
}
pub fn collect(program: &Program<'_>, options: &EmbeddedSqlOptions) -> ScopedCollection {
record_embedded_collection!("postgres.scoped.collections");
let mut imports = classify(program, options);
let candidates = std::mem::take(&mut imports.candidates);
let mut visitor = ScopeCollector::from_imports(imports, program.source_text.contains("let"));
visitor.track_resource_transactions = options.track_transaction_helpers;
if visitor.needs_walk() {
visitor.visit_program(program);
visitor.finish_callback_transactions();
visitor.finish_transaction_parameters();
visitor.finish_undeclared_annotations();
visitor.finish_delayed_receivers();
}
ScopedCollection {
executors: visitor.found,
candidates,
spans: visitor.spans,
}
}
pub(super) fn matched_names(
program: &Program<'_>,
options: &EmbeddedSqlOptions,
) -> (Vec<String>, Vec<String>) {
let (mut factories, mut types) = (Vec::new(), Vec::new());
for statement in &program.body {
let Statement::ImportDeclaration(import) = statement else {
continue;
};
if !candidates::from_configured_module(
no_mistakes_ts_facts::ast::js_literal(&import.source).as_ref(),
&options.import_specifier,
) {
continue;
}
for specifier in import.specifiers.iter().flatten() {
let ImportDeclarationSpecifier::ImportSpecifier(named) = specifier else {
continue;
};
let imported = no_mistakes_ts_facts::ast::module_export_name(&named.imported);
let type_only = import.import_kind == ImportOrExportKind::Type
|| named.import_kind == ImportOrExportKind::Type;
if !type_only
&& candidates::contains(&options.executor_factory_names, imported.as_ref())
{
factories.push(imported.to_string());
}
if candidates::contains(&options.executor_type_names, imported.as_ref()) {
types.push(imported.to_string());
}
}
}
(sorted_unique(factories), sorted_unique(types))
}
fn sorted_unique(mut names: Vec<String>) -> Vec<String> {
names.sort();
names.dedup();
names
}