use super::super::ScopeVisitor;
use crate::embedded::helpers::{sentinel, ImportedSite};
use crate::embedded::EmbeddedSqlKind;
use no_mistakes_ts_facts::syntax_helpers::{byte_offset_to_line, unwrap_ts_wrappers};
use oxc_ast::ast::{Expression, IdentifierReference};
#[path = "constant_exports.rs"]
mod exports;
#[path = "names.rs"]
mod names;
pub(in crate::embedded::walk) use exports::snapshot_exports;
#[path = "namespace_constants.rs"]
pub(in crate::embedded::walk) mod namespace;
pub(in crate::embedded::walk) fn note_fragment_const(
visitor: &mut ScopeVisitor<'_>,
init: &Expression<'_>,
scope_index: usize,
name: &str,
) {
if !matches!(
unwrap_ts_wrappers(init),
Expression::TaggedTemplateExpression(_)
) {
return;
}
if let Some(names) = visitor.literal_objects.fragment_consts.get_mut(scope_index) {
names.insert(name.to_string());
}
}
pub(in crate::embedded::walk) fn identifier_piece(
visitor: &ScopeVisitor<'_>,
ident: &IdentifierReference<'_>,
) -> Option<String> {
let name = ident.name.as_str();
if visitor.literal_objects.is_text_const(&visitor.scopes, name) {
let binding = visitor.lookup(name)?;
if binding.kind != EmbeddedSqlKind::ImmutableLocal {
return None;
}
return binding.sql;
}
imported_hole(visitor, name, ident.span.start, false)
}
pub(in crate::embedded::walk) fn appended_identifier(
visitor: &ScopeVisitor<'_>,
ident: &IdentifierReference<'_>,
) -> Option<String> {
let name = ident.name.as_str();
if let Some(binding) = visitor.lookup(name) {
return known_binding_sql(binding.kind, binding.sql);
}
imported_hole(visitor, name, ident.span.start, true)
}
fn imported_hole(
visitor: &ScopeVisitor<'_>,
name: &str,
start: u32,
allow_fragment: bool,
) -> Option<String> {
if visitor.shadowed_locally(name)
|| !visitor.imported_names.contains(name)
|| visitor.functions.imported_sql_tags().contains(name)
{
return None;
}
let line = byte_offset_to_line(visitor.source, start as usize);
let index = visitor.imported_sites.borrow_mut().intern(
start,
ImportedSite {
local: name.to_string(),
member: None,
kind: crate::embedded::helpers::SiteKind::Sql,
args: Vec::new(),
value_used: true,
line,
constant: Some(allow_fragment),
},
);
Some(sentinel::encode(index))
}
fn known_binding_sql(kind: EmbeddedSqlKind, sql: Option<String>) -> Option<String> {
match kind {
EmbeddedSqlKind::ImmutableLocal | EmbeddedSqlKind::Composed | EmbeddedSqlKind::Inline => {
sql
}
EmbeddedSqlKind::Dynamic => None,
}
}
#[cfg(all(test, any(nm_shard_all, nm_shard_0)))]
#[path = "constants_tests.rs"]
mod constants_tests;