mod import;
mod raw;
mod shapes;
pub use shapes::looks_like_tag_implementation;
use shapes::{record_class_tag_shadow, record_function_tag_shadow};
use super::super::super::super::options::TrustedSqlTag;
use super::super::super::super::tags::SqlTagNames;
use super::super::for_each_bound_name;
use no_mistakes_ts_facts::syntax_helpers::unwrap_ts_wrappers;
use oxc_ast::ast::{
BindingPattern, Declaration, Expression, Program, Statement, VariableDeclaration,
VariableDeclarator,
};
use std::collections::HashSet;
#[derive(Default)]
pub(super) struct TagShadows {
names: HashSet<String>,
imported: SqlTagNames,
}
impl TagShadows {
pub(super) fn collect(
program: &Program<'_>,
trusted_sql_tags: &[TrustedSqlTag],
raw_sql_tags: &[TrustedSqlTag],
sql_statement_types: &HashSet<String>,
) -> Self {
let mut shadows = Self::default();
let top_level_functions = top_level_function_names(program);
for statement in &program.body {
record_statement(
statement,
&top_level_functions,
trusted_sql_tags,
&mut shadows,
);
}
shadows.configure_raw(program, raw_sql_tags);
shadows
.imported
.collect_fragments(program, sql_statement_types);
shadows
}
pub(super) fn contains(&self, name: &str) -> bool {
self.names.contains(name)
}
pub(super) fn imported_tags(&self) -> &SqlTagNames {
&self.imported
}
}
fn top_level_function_names<'a>(program: &Program<'a>) -> HashSet<&'a str> {
let mut names = HashSet::new();
for statement in &program.body {
let function = match statement {
Statement::FunctionDeclaration(function) => Some(function.as_ref()),
Statement::ExportDeclaration(export) => match &export.declaration {
Declaration::FunctionDeclaration(function) => Some(function.as_ref()),
_ => None,
},
_ => None,
};
if let Some(id) = function.and_then(|function| function.id.as_ref()) {
names.insert(id.name.as_str());
}
}
names
}
fn record_statement(
statement: &Statement<'_>,
top_level_functions: &HashSet<&str>,
trusted_sql_tags: &[TrustedSqlTag],
shadows: &mut TagShadows,
) {
match statement {
Statement::VariableDeclaration(declaration) => {
record_declaration(declaration, top_level_functions, shadows);
}
Statement::FunctionDeclaration(function) => {
record_function_tag_shadow(function, shadows);
}
Statement::ClassDeclaration(class) => {
record_class_tag_shadow(class, shadows);
}
Statement::ExportDeclaration(export) => match &export.declaration {
Declaration::VariableDeclaration(declaration) => {
record_declaration(declaration, top_level_functions, shadows);
}
Declaration::FunctionDeclaration(function) => {
record_function_tag_shadow(function, shadows);
}
Declaration::ClassDeclaration(class) => {
record_class_tag_shadow(class, shadows);
}
_ => {}
},
Statement::ImportDeclaration(import) => {
import::record_import(import, shadows, trusted_sql_tags);
}
_ => {}
}
}
fn record_declaration(
declaration: &VariableDeclaration<'_>,
top_level_functions: &HashSet<&str>,
shadows: &mut TagShadows,
) {
for declarator in &declaration.declarations {
record_declarator(declarator, top_level_functions, shadows);
}
}
fn record_declarator(
declarator: &VariableDeclarator<'_>,
top_level_functions: &HashSet<&str>,
shadows: &mut TagShadows,
) {
let is_helper_shape = declarator.init.as_ref().is_some_and(|init| {
matches!(&declarator.id, BindingPattern::BindingIdentifier(_)) && is_function_shaped(init)
});
for_each_bound_name(&declarator.id, &mut |name| {
if top_level_functions.contains(name) {
return;
}
if !is_helper_shape || is_trusted_tag_name(name) {
shadows.names.insert(name.to_string());
}
});
}
pub(super) fn is_function_shaped(expr: &Expression<'_>) -> bool {
matches!(
unwrap_ts_wrappers(expr),
Expression::FunctionExpression(_) | Expression::ArrowFunctionExpression(_)
)
}
pub(super) fn is_trusted_tag_name(name: &str) -> bool {
name.eq_ignore_ascii_case("sql") || name == "String"
}