no-mistakes-postgres-engine 0.109.131

PostgreSQL embedded SQL extraction, schema catalog, and replay-safety analysis for no-mistakes
Documentation
use super::ScopeVisitor;
use crate::embedded::{CallSqlFragment, CallSqlFragmentAssembly};
use oxc_ast::ast::{CallExpression, Expression, LogicalOperator};
use oxc_span::GetSpan;

impl ScopeVisitor<'_> {
    pub(super) fn runtime_loop_append(
        &self,
        key: &(usize, String),
        receiver: &str,
        decl_depth: (usize, usize),
        param_builder: bool,
    ) -> bool {
        self.call_fragment_state
            .runtime_for_of_scopes
            .last()
            .is_some_and(|(loop_scope, outer_depth)| {
                key.0 < *loop_scope && decl_depth.1 <= *outer_depth
            })
            && !self.append_crosses_function(receiver)
            && (self.call_fragment_state.builders.contains_key(key) || param_builder)
    }
}

/// Returns false when the append adds nothing the rule must track.
pub(super) fn keep_unknown(
    visitor: &ScopeVisitor<'_>,
    call: &CallExpression<'_>,
    added: &mut Vec<CallSqlFragment>,
    runtime_append: bool,
) -> bool {
    if added.is_empty() && runtime_append && !blank_argument(visitor, call) {
        added.push(unknown_fragment(visitor, call));
    }
    !added.is_empty()
}

/// An executed loop append with no recoverable SQL; the rule reports it per `unanalyzableSql`.
pub(super) fn unknown_fragment(
    visitor: &ScopeVisitor<'_>,
    call: &CallExpression<'_>,
) -> CallSqlFragment {
    let line = no_mistakes_ts_facts::syntax_helpers::byte_offset_to_line(
        visitor.source,
        call.span.start as usize,
    );
    CallSqlFragment {
        sql: String::new(),
        line,
        span: (call.span().start, call.span().end),
        context_sql: None,
        append_site: Some(call.span.start),
        deferred_append: false,
        assembly: CallSqlFragmentAssembly::Opaque,
        source_origins: Default::default(),
        preceding_tokens: None,
        append_owner: None,
    }
}

/// A literal append with only whitespace adds no SQL, so it is not an unrecoverable append.
fn blank_argument(visitor: &ScopeVisitor<'_>, call: &CallExpression<'_>) -> bool {
    let Some(arg) = crate::embedded::first_call_argument(call) else {
        return false;
    };
    blank_expr(visitor, arg, true)
}

/// The one test for an append that adds no SQL: a blank literal, `null`, the
/// unshadowed global `undefined`, a boolean, or a conditional or logical
/// expression whose parts are all such values. `inert_primitives` is false for
/// `+=`, which appends `null`, `false` and `undefined` as text.
pub(super) fn blank_expr(
    visitor: &ScopeVisitor<'_>,
    arg: &Expression<'_>,
    inert_primitives: bool,
) -> bool {
    match no_mistakes_ts_facts::syntax_helpers::unwrap_ts_wrappers(arg) {
        Expression::ConditionalExpression(cond) => {
            blank_expr(visitor, &cond.consequent, inert_primitives)
                && blank_expr(visitor, &cond.alternate, inert_primitives)
        }
        Expression::LogicalExpression(logic) if logic.operator == LogicalOperator::And => {
            (inert_primitives || blank_expr(visitor, &logic.left, false))
                && blank_expr(visitor, &logic.right, inert_primitives)
        }
        Expression::LogicalExpression(logic) => {
            blank_expr(visitor, &logic.left, inert_primitives)
                && blank_expr(visitor, &logic.right, inert_primitives)
        }
        Expression::NullLiteral(_) | Expression::BooleanLiteral(_) => inert_primitives,
        Expression::Identifier(id) => {
            inert_primitives
                && id.name == "undefined"
                && !super::super::resolve::compose::tag_shadowed("undefined", visitor)
        }
        Expression::StringLiteral(text) => text
            .value
            .as_str()
            .is_some_and(|value| value.trim().is_empty()),
        Expression::TemplateLiteral(template) => blank_template(template),
        Expression::TaggedTemplateExpression(tagged) => {
            blank_template(&tagged.quasi)
                && crate::embedded::tags::is_sql_tag(
                    &tagged.tag,
                    &mut |name| super::super::resolve::compose::tag_shadowed(name, visitor),
                    visitor.functions.imported_sql_tags(),
                )
        }
        _ => false,
    }
}

fn blank_template(template: &oxc_ast::ast::TemplateLiteral<'_>) -> bool {
    template.expressions.is_empty()
        && template.quasis.iter().all(|part| {
            crate::embedded::tags::quasi_text(part, false)
                .trim()
                .is_empty()
        })
}