use super::super::super::{first_call_argument, EmbeddedSqlCall, EmbeddedSqlKind};
use super::{classify_init, BindingState, ScopeVisitor};
use no_mistakes_ts_facts::syntax_helpers::{byte_offset_to_line, unwrap_ts_wrappers};
use oxc_ast::ast::{CallExpression, Expression};
pub(super) mod variants;
fn legacy_executor_call(
visitor: &ScopeVisitor<'_>,
call: &CallExpression<'_>,
callee: String,
) -> EmbeddedSqlCall {
let line = byte_offset_to_line(visitor.source, call.span.start as usize);
let Some(argument) = first_call_argument(call) else {
return EmbeddedSqlCall {
variants: Vec::new(),
line,
callee,
sql_text: None,
kind: EmbeddedSqlKind::Dynamic,
declaration_line: None,
sql_source_positions: Vec::new(),
recovered_placeholder_positions: Vec::new(),
..Default::default()
};
};
match unwrap_ts_wrappers(argument) {
Expression::Identifier(ident) => {
let mut binding = visitor.lookup(ident.name.as_str());
let (sql_text, recovered_placeholder_positions) = binding
.as_ref()
.and_then(|binding| binding.sql.clone())
.map(super::super::super::placeholders::publish_placeholders_with_positions)
.map_or_else(
|| (None, Vec::new()),
|(sql, positions)| (Some(sql), positions),
);
let variants = binding
.as_mut()
.filter(|binding| {
binding.kind == EmbeddedSqlKind::Composed
&& !visitor.append_crosses_function(ident.name.as_str())
})
.map(|binding| variants::publish(binding.variants.take(), binding.kind))
.unwrap_or_default();
let sql_source_offsets =
super::exact_origins::binding(visitor, argument, binding.as_ref());
EmbeddedSqlCall {
variants,
line,
callee,
sql_text,
kind: binding
.as_ref()
.map_or(EmbeddedSqlKind::Dynamic, |binding| binding.kind),
sql_source_positions: binding
.as_ref()
.map(|binding| binding.sql_source_positions.clone())
.unwrap_or_default(),
recovered_placeholder_positions,
sql_source_offsets,
declaration_line: binding.map(|binding| binding.line),
..Default::default()
}
}
_ => {
let (sql, kind) = classify_init(argument, true, visitor);
let mut sql_source_offsets = Vec::new();
let mut sql_source_positions = super::compose::helper_provenance(argument, visitor)
.map(|provenance| {
sql_source_offsets =
super::rewrite_origins(&provenance.sql, &provenance.origins, 0, true);
provenance.positions
})
.or_else(|| super::compose::parameter_source_positions(argument, visitor))
.unwrap_or_else(|| {
super::super::super::source_positions::for_rendered_expression(
argument,
visitor.source,
call.span.start as usize,
line,
sql.as_deref(),
)
});
if let Some(positions) = sql
.as_deref()
.filter(|_| sql_source_positions.is_empty())
.and_then(|text| super::compose::binding_source_positions(argument, visitor, text))
{
sql_source_positions = positions;
}
if let Some(positions) = sql
.as_deref()
.filter(|_| sql_source_positions.is_empty())
.and_then(|text| {
super::compose::append_chain_source_positions(argument, visitor, text)
})
{
sql_source_positions = positions;
}
super::exact_origins::fill(
visitor,
argument,
sql.as_deref(),
(line, &sql_source_positions, &mut sql_source_offsets),
);
let (sql_text, recovered_placeholder_positions) = sql
.map(super::super::super::placeholders::publish_placeholders_with_positions)
.map_or_else(
|| (None, Vec::new()),
|(sql, positions)| (Some(sql), positions),
);
EmbeddedSqlCall {
variants: Vec::new(),
line,
callee,
sql_text,
kind: if kind == EmbeddedSqlKind::ImmutableLocal {
EmbeddedSqlKind::Inline
} else {
kind
},
declaration_line: None,
sql_source_positions,
sql_source_offsets: sql_source_offsets.into(),
recovered_placeholder_positions,
..Default::default()
}
}
}
}
pub(crate) fn executor_call(
visitor: &ScopeVisitor<'_>,
call: &CallExpression<'_>,
callee: String,
) -> EmbeddedSqlCall {
if visitor.head_calls_only {
return EmbeddedSqlCall {
line: byte_offset_to_line(visitor.source, call.span.start as usize),
callee,
sql_text: first_call_argument(call)
.and_then(|argument| super::super::head_calls::text(argument, visitor)),
kind: EmbeddedSqlKind::Dynamic,
..Default::default()
};
}
if let Some(recovered) = (!visitor.head_calls_only)
.then(|| super::builder_text::executor_call(visitor, call, &callee))
.flatten()
{
return recovered;
}
let mut recovered = legacy_executor_call(visitor, call, callee);
if recovered.variants.is_empty() {
recovered.variants = first_call_argument(call)
.map(|argument| variants::argument(visitor, argument, recovered.kind))
.unwrap_or_default();
}
recovered
}
pub(in crate::embedded::walk) fn executor_call_from_binding(
visitor: &ScopeVisitor<'_>,
call: &CallExpression<'_>,
callee: String,
binding: &BindingState,
) -> EmbeddedSqlCall {
let line = byte_offset_to_line(visitor.source, call.span.start as usize);
let (sql_text, recovered_placeholder_positions) = binding
.sql
.clone()
.map(super::super::super::placeholders::publish_placeholders_with_positions)
.map_or_else(
|| (None, Vec::new()),
|(sql, positions)| (Some(sql), positions),
);
let variants = if matches!(
binding.kind,
EmbeddedSqlKind::Dynamic | EmbeddedSqlKind::Composed
) {
variants::publish_for_argument(
visitor,
first_call_argument(call),
binding.variants.clone(),
binding.kind,
)
} else {
Vec::new()
};
EmbeddedSqlCall {
variants,
line,
callee,
sql_text,
kind: binding.kind,
declaration_line: Some(binding.line),
sql_source_positions: binding.sql_source_positions.clone(),
recovered_placeholder_positions,
..Default::default()
}
}