use super::scoped_bindings::ScopedExecutors;
use super::EmbeddedSqlOptions;
use no_mistakes_ts_facts::syntax_helpers::unwrap_ts_wrappers;
use oxc_ast::ast::{
CallExpression, Expression, ImportDeclaration, ImportDeclarationSpecifier, ImportOrExportKind,
ModuleExportName, Program, Statement,
};
use std::collections::HashSet;
const TRANSACTION_IMPORTS: &[&str] = &["withTransaction", "withTransactionOptions"];
const QUERY_PROPERTY: &str = "query";
pub(super) const MEMBER_QUERY_OPT_IN: &str = "\0no-mistakes:member-query";
const SQL_TEMPLATE_STRINGS: &str = "sql-template-strings";
const SQL_STATEMENT: &str = "SQLStatement";
pub(super) fn sql_statement_type_bindings(program: &Program<'_>) -> HashSet<String> {
let mut bindings = HashSet::new();
for statement in &program.body {
let Statement::ImportDeclaration(import) = statement else {
continue;
};
if no_mistakes_ts_facts::ast::js_literal(&import.source).as_ref() != SQL_TEMPLATE_STRINGS {
continue;
}
let Some(specifiers) = &import.specifiers else {
continue;
};
for specifier in specifiers {
let ImportDeclarationSpecifier::ImportSpecifier(named) = specifier else {
continue;
};
if module_export_name(&named.imported) == SQL_STATEMENT {
bindings.insert(named.local.name.to_string());
}
}
}
bindings
}
pub fn executor_bindings(program: &Program<'_>, options: &EmbeddedSqlOptions) -> HashSet<String> {
let mut bindings = HashSet::new();
for statement in &program.body {
let Statement::ImportDeclaration(import) = statement else {
continue;
};
collect_import_bindings(import, options, &mut bindings);
}
if member_query_enabled(options) {
bindings.insert(MEMBER_QUERY_OPT_IN.to_string());
}
bindings
}
fn member_query_enabled(options: &EmbeddedSqlOptions) -> bool {
options.import_specifier.is_empty()
&& options
.executor_names
.iter()
.any(|name| name == QUERY_PROPERTY)
}
fn executor_name_enabled(options: &EmbeddedSqlOptions, imported: &str) -> bool {
options.executor_names.iter().any(|name| name == imported)
|| (options.uses_legacy_executor_defaults()
&& !options.import_specifier.is_empty()
&& ["query", "read", "write"].contains(&imported))
}
fn collect_import_bindings(
import: &ImportDeclaration<'_>,
options: &EmbeddedSqlOptions,
bindings: &mut HashSet<String>,
) {
if import.import_kind == ImportOrExportKind::Type
|| (!options.import_specifier.is_empty()
&& no_mistakes_ts_facts::ast::js_literal(&import.source).as_ref()
!= options.import_specifier)
{
return;
}
let Some(specifiers) = &import.specifiers else {
return;
};
for specifier in specifiers {
let ImportDeclarationSpecifier::ImportSpecifier(named) = specifier else {
continue;
};
if named.import_kind == ImportOrExportKind::Type {
continue;
}
let imported = module_export_name(&named.imported);
if !options.import_specifier.is_empty() && TRANSACTION_IMPORTS.contains(&imported.as_str())
{
bindings.insert(QUERY_PROPERTY.to_string());
}
if executor_name_enabled(options, &imported) {
bindings.insert(named.local.name.to_string());
}
}
}
fn module_export_name(name: &ModuleExportName<'_>) -> String {
no_mistakes_ts_facts::ast::module_export_name(name).into_owned()
}
pub fn is_database_call(call: &CallExpression<'_>, bindings: &HashSet<String>) -> bool {
callee_name(call, bindings, &ScopedExecutors::default()).is_some()
}
pub(super) fn member_query_receiver<'a>(
call: &'a CallExpression<'a>,
) -> Option<&'a Expression<'a>> {
match unwrap_ts_wrappers(&call.callee) {
Expression::StaticMemberExpression(member) if member.property.name == QUERY_PROPERTY => {
Some(&member.object)
}
Expression::ComputedMemberExpression(member) if static_query_key(&member.expression) => {
Some(&member.object)
}
_ => None,
}
}
pub(super) fn callee_name(
call: &CallExpression<'_>,
bindings: &HashSet<String>,
scoped: &ScopedExecutors,
) -> Option<String> {
match unwrap_ts_wrappers(&call.callee) {
Expression::Identifier(ident)
if bindings.contains(ident.name.as_str())
|| scoped.contains(ident.name.as_str(), call.span.start) =>
{
Some(ident.name.to_string())
}
Expression::StaticMemberExpression(member)
if member.property.name == QUERY_PROPERTY && bindings.contains(MEMBER_QUERY_OPT_IN) =>
{
Some(QUERY_PROPERTY.to_string())
}
Expression::ComputedMemberExpression(member) => (bindings.contains(MEMBER_QUERY_OPT_IN)
&& static_query_key(&member.expression))
.then(|| QUERY_PROPERTY.to_string()),
_ => None,
}
}
fn static_query_key(expr: &Expression<'_>) -> bool {
match unwrap_ts_wrappers(expr) {
Expression::StringLiteral(literal) => literal.value == QUERY_PROPERTY,
Expression::TemplateLiteral(template)
if template.expressions.is_empty() && template.quasis.len() == 1 =>
{
no_mistakes_ts_facts::ast::template_quasi_text(&template.quasis[0]) == QUERY_PROPERTY
}
_ => false,
}
}