no-mistakes-postgres-engine 0.109.131

PostgreSQL embedded SQL extraction, schema catalog, and replay-safety analysis for no-mistakes
Documentation
use super::super::ScopeVisitor;
use crate::embedded::{placeholders, source_positions, EmbeddedSqlSourcePosition};
use no_mistakes_ts_facts::syntax_helpers::{byte_offset_to_line, unwrap_ts_wrappers};
use oxc_ast::ast::{CallExpression, Expression};

pub(in crate::embedded::walk) fn fragment(
    call: &CallExpression<'_>,
    visitor: &ScopeVisitor<'_>,
    offset: placeholders::Counts,
) -> (u32, Vec<EmbeddedSqlSourcePosition>) {
    // apply_append has already verified that its first argument is static.
    let expression = call.arguments[0].as_expression().unwrap();
    if let Expression::Identifier(ident) = unwrap_ts_wrappers(expression) {
        // A same-file const has a binding. An imported const is only a sentinel
        // until projection fills it, so it has no local positions.
        if let Some(binding) = visitor.lookup(ident.name.as_str()) {
            let mut positions = binding.sql_source_positions;
            renumber(&mut positions, binding.sql.as_deref().unwrap(), offset);
            return (binding.line, positions);
        }
        let line = byte_offset_to_line(visitor.source, call.span.start as usize);
        return (line, Vec::new());
    }
    if let Some(mut provenance) = super::super::compose::helper_provenance(expression, visitor) {
        renumber(&mut provenance.positions, &provenance.sql, offset);
        return (provenance.line, provenance.positions);
    }
    let line = byte_offset_to_line(visitor.source, call.span.start as usize);
    let positions = source_positions::for_expression_with_offset(
        expression,
        visitor.source,
        call.span.start as usize,
        line,
        offset.all as usize,
    );
    (line, positions)
}

pub(in crate::embedded) fn renumber(
    positions: &mut [EmbeddedSqlSourcePosition],
    sql: &str,
    offset: impl Into<placeholders::Counts>,
) {
    let offset = offset.into();
    for position in positions {
        let original_column = position.sql_column;
        let mut seen = placeholders::Counts::default();
        for (at, internal, _) in placeholders::markers(sql) {
            let ordinal = seen.next(internal);
            let digits = sql[at + internal.len()..]
                .bytes()
                .take_while(u8::is_ascii_digit)
                .count();
            let delta = (offset.shift(internal) + ordinal).to_string().len() as i32 - digits as i32;
            let prefix = placeholders::publish_placeholders(sql[..at].to_string());
            let line = prefix.bytes().filter(|byte| *byte == b'\n').count() as u32 + 1;
            let column = prefix.rsplit('\n').next().unwrap().chars().count() as u32 + 1;
            if position.sql_line == line && original_column > column {
                position.sql_column = position.sql_column.saturating_add_signed(delta);
            }
        }
    }
}

pub(in crate::embedded) fn append(
    positions: &mut Vec<EmbeddedSqlSourcePosition>,
    prefix: &str,
    origin: u32,
    fragment_origin: u32,
    fragment_positions: &[EmbeddedSqlSourcePosition],
) {
    let prefix = placeholders::publish_placeholders(prefix.to_string());
    let line = prefix.bytes().filter(|byte| *byte == b'\n').count() as u32 + 1;
    let column = prefix.rsplit('\n').next().unwrap().chars().count() as u32 + 1;
    let expected = positions
        .last()
        .map(|position| position.source_line + line - position.sql_line)
        .unwrap_or(origin + line - 1);
    let fragment_path = fragment_positions
        .first()
        .and_then(|position| position.source_path.clone());
    if expected != fragment_origin
        || positions
            .last()
            .and_then(|position| position.source_path.as_ref())
            != fragment_path.as_ref()
    {
        positions.push(EmbeddedSqlSourcePosition {
            sql_line: line,
            sql_column: column,
            source_line: fragment_origin,
            source_path: fragment_path,
        });
    }
    positions.extend(
        fragment_positions
            .iter()
            .map(|position| EmbeddedSqlSourcePosition {
                sql_line: line + position.sql_line - 1,
                sql_column: position.sql_column
                    + if position.sql_line == 1 {
                        column - 1
                    } else {
                        0
                    },
                source_line: position.source_line,
                source_path: position.source_path.clone(),
            }),
    );
}