no-mistakes-postgres-engine 0.109.129

PostgreSQL embedded SQL extraction, schema catalog, and replay-safety analysis for no-mistakes
Documentation
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;

/// Top-level `const`/`let`/`var` bindings whose initializer isn't itself a
/// same-file callable helper shape (`function`/arrow expression) — anything
/// else rebinds the name away from whatever a same-file helper's body would
/// otherwise trust it to mean when referencing it through closure (not
/// through the helper's own parameters, which `shadows_param` already
/// covers). A top-level `function` declaration is out of scope here: that
/// shape is a legitimate same-file helper collected elsewhere, including one
/// referencing its own top-level declaration by name.
///
/// The callable-helper-shape exemption never applies to a binding spelled
/// `sql` (case-insensitively) or `String`. The spelling does not grant trust.
/// It only stops a same-file helper from standing in for a configured import
/// or for the intrinsic `String.raw` tag.
///
/// Default imports from `sql-template-strings`, and configured named
/// `trustedSqlTags` imports, are the opposite: they *are* the trusted tag,
/// recorded in [`TagShadows::imported`] under whatever local name the file used.
#[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
    }
}

/// Names of every top-level `function` declaration (including
/// `export function …`), gathered up front so a same-named top-level
/// `const`/`let`/`var` never counts as shadowing it — matching the
/// established rule in [`super::super::record_function_declaration`]'s own
/// doc comment: "existing fixtures rely on a same-named top-level helper
/// never shadowing itself." The parser doesn't reject the real-world
/// collision (JS would), and fixtures such as
/// `composed-chain-append-tagged-trusted.ts` and `composed-concat-tagged.ts`
/// deliberately reuse the trusted tag's own name for a const holding its
/// composed result.
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);
    }
}

/// A destructured declarator (`const { tag: sql } = providers;`) has no
/// single callable-helper shape to exempt — the callable-helper exemption
/// below applies only to a simple `BindingIdentifier` — so every name it
/// binds is recorded as a shadow, matching how a non-function-shaped
/// identifier declarator is already handled. A name that also names a
/// top-level `function` declaration is exempted unconditionally, even from
/// the `sql`-specific override just below: see
/// [`top_level_function_names`].
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"
}