no_mistakes_postgres_engine/embedded/
scoped_bindings.rs1use super::relative::{PendingRelativeSpan, RelativeScopedCandidate};
9use super::EmbeddedSqlOptions;
10use candidates::classify;
11use collector::ScopeCollector;
12use oxc_ast::ast::{ImportDeclarationSpecifier, ImportOrExportKind, Program, Statement};
13use oxc_ast_visit::Visit;
14use oxc_span::Span;
15use std::collections::HashMap;
16
17mod candidates;
18mod collector;
19mod owners;
20
21pub fn from_configured_module(source: &str, specifier: &str) -> bool {
24 candidates::from_configured_module(source, specifier)
25}
26
27pub type PreparedScopes = std::collections::BTreeMap<EmbeddedSqlOptions, ScopedCollection>;
28
29#[doc(hidden)]
30#[derive(Debug, Default, Clone, PartialEq, Eq)]
31pub struct TransactionCallbacks {
32 pub parents: crate::fx::FxHashMap<u32, u32>,
33 pub opaque_callers: crate::fx::FxHashSet<u32>,
34}
35
36pub struct ScopedCollection {
37 pub executors: ScopedExecutors,
38 pub(super) candidates: Vec<RelativeScopedCandidate>,
39 pub(super) spans: Vec<PendingRelativeSpan>,
40}
41
42impl ScopedCollection {
43 pub(super) fn prune_unused_diagnostic_candidates(
45 &mut self,
46 calls: &[super::UndeclaredExecutorType],
47 ) {
48 if calls.is_empty()
49 && self
50 .candidates
51 .iter()
52 .all(|candidate| !candidate.factory && !candidate.type_import)
53 {
54 self.candidates.clear();
55 }
56 }
57}
58
59pub(super) type UndeclaredReceiverTypes = HashMap<String, super::UndeclaredExecutorType>;
60
61#[derive(Debug, Default)]
63pub struct ScopedExecutors {
64 spans: HashMap<String, Vec<Span>>,
65 pub(super) transactions: HashMap<String, Vec<Span>>,
66 pub(super) transaction_isolation:
67 HashMap<u32, no_mistakes_postgres_sql::statement_facts::SqlIsolation>,
68 pub(super) transaction_parameters: crate::fx::FxHashSet<u32>,
69 pub(super) transaction_callback_starts: crate::fx::FxHashSet<u32>,
70 pub(super) opaque_transaction_calls: crate::fx::FxHashSet<u32>,
71 pub(super) transaction_callbacks: crate::fx::FxHashSet<u32>,
72 pub(super) delayed: std::collections::HashSet<u32>,
73 pub(super) undeclared: HashMap<u32, HashMap<String, UndeclaredReceiverTypes>>,
74 pub(super) undeclared_this: Vec<(Span, UndeclaredReceiverTypes)>,
75 pub(super) undeclared_this_exclusions: Vec<Span>,
76 pub(super) undeclared_imports: HashMap<String, super::UndeclaredExecutorType>,
77}
78
79impl ScopedExecutors {
80 pub fn delayed_reference(&self, start: u32) -> bool {
81 self.delayed.contains(&start)
82 }
83
84 pub(super) fn contains_declaration(&self, name: &str, start: u32) -> bool {
85 self.spans
86 .get(name)
87 .is_some_and(|spans| spans.iter().any(|span| span.start == start))
88 }
89
90 pub(super) fn contains(&self, name: &str, position: u32) -> bool {
91 self.spans.get(name).is_some_and(|spans| {
92 spans
93 .iter()
94 .any(|span| span.start <= position && position < span.end)
95 })
96 }
97
98 pub(super) fn add(&mut self, name: &str, span: Span) {
99 self.spans.entry(name.to_string()).or_default().push(span);
100 }
101}
102
103pub fn collect(program: &Program<'_>, options: &EmbeddedSqlOptions) -> ScopedCollection {
104 record_embedded_collection!("postgres.scoped.collections");
105 let mut imports = classify(program, options);
106 let candidates = std::mem::take(&mut imports.candidates);
107 let mut visitor = ScopeCollector::from_imports(imports, program.source_text.contains("let"));
108 visitor.track_resource_transactions = options.track_transaction_helpers;
109 if visitor.needs_walk() {
110 visitor.visit_program(program);
111 visitor.finish_callback_transactions();
112 visitor.finish_transaction_parameters();
113 visitor.finish_undeclared_annotations();
114 visitor.finish_delayed_receivers();
115 }
116 ScopedCollection {
117 executors: visitor.found,
118 candidates,
119 spans: visitor.spans,
120 }
121}
122
123pub(super) fn matched_names(
126 program: &Program<'_>,
127 options: &EmbeddedSqlOptions,
128) -> (Vec<String>, Vec<String>) {
129 let (mut factories, mut types) = (Vec::new(), Vec::new());
130 for statement in &program.body {
131 let Statement::ImportDeclaration(import) = statement else {
132 continue;
133 };
134 if !candidates::from_configured_module(
135 no_mistakes_ts_facts::ast::js_literal(&import.source).as_ref(),
136 &options.import_specifier,
137 ) {
138 continue;
139 }
140 for specifier in import.specifiers.iter().flatten() {
141 let ImportDeclarationSpecifier::ImportSpecifier(named) = specifier else {
142 continue;
143 };
144 let imported = no_mistakes_ts_facts::ast::module_export_name(&named.imported);
145 let type_only = import.import_kind == ImportOrExportKind::Type
146 || named.import_kind == ImportOrExportKind::Type;
147 if !type_only
148 && candidates::contains(&options.executor_factory_names, imported.as_ref())
149 {
150 factories.push(imported.to_string());
151 }
152 if candidates::contains(&options.executor_type_names, imported.as_ref()) {
153 types.push(imported.to_string());
154 }
155 }
156 }
157 (sorted_unique(factories), sorted_unique(types))
158}
159
160fn sorted_unique(mut names: Vec<String>) -> Vec<String> {
161 names.sort();
162 names.dedup();
163 names
164}