no-mistakes-postgres-engine 0.109.130

PostgreSQL embedded SQL extraction, schema catalog, and replay-safety analysis for no-mistakes
Documentation
use super::{Helper, LockHelpers};
use no_mistakes_ts_facts::syntax_helpers::unwrap_ts_wrappers;
use oxc_ast::ast::{
    BindingPattern, Declaration, Expression, FormalParameters, Function, Program, Statement,
    VariableDeclaration, VariableDeclarationKind,
};
use std::collections::HashSet;

mod body;

struct Candidate<'a> {
    name: &'a str,
    declaration: Option<u32>,
    params: &'a FormalParameters<'a>,
    body: body::Body<'a>,
}

pub(in crate::embedded::walk) fn collect<'a>(
    program: &'a Program<'a>,
    enabled: bool,
    source: &str,
    bindings: &HashSet<String>,
    options: &crate::EmbeddedSqlOptions,
    functions: &super::super::resolve::LocalFunctions,
) -> LockHelpers {
    let mut helpers = LockHelpers::default();
    if !enabled {
        return helpers;
    }
    let escaped = if options.executor_option_properties.is_empty() {
        Default::default()
    } else {
        super::escapes::names(program)
    };
    let reassigned =
        super::super::resolve::functions::reassigned::ReassignedNames::collect(program);
    let mut candidates = Vec::new();
    for statement in &program.body {
        match statement {
            Statement::FunctionDeclaration(function) => {
                candidates.extend(named(function));
            }
            Statement::VariableDeclaration(declaration) => {
                candidates.extend(constants(declaration));
            }
            Statement::ExportDeclaration(export) => match &export.declaration {
                Declaration::FunctionDeclaration(function) => candidates.extend(named(function)),
                Declaration::VariableDeclaration(declaration) => {
                    candidates.extend(constants(declaration));
                }
                _ => {}
            },
            _ => {}
        }
    }
    candidates.retain(|candidate| !reassigned.contains(candidate.name));
    let context = body::Context {
        source,
        bindings,
        options,
        functions,
    };
    for _ in 0..candidates.len() {
        let mut added = false;
        for candidate in &candidates {
            if helpers.contains_key(candidate.name) {
                continue;
            }
            if let Some(calls) = context.summarize(candidate, &helpers) {
                if escaped.contains(candidate.name)
                    && calls.iter().any(|query| query.option_property.is_some())
                {
                    continue;
                }
                helpers.insert(
                    candidate.name.to_owned(),
                    Helper {
                        declaration: candidate.declaration,
                        calls,
                    },
                );
                added = true;
            }
        }
        if !added {
            break;
        }
    }
    helpers
}

fn named<'a>(function: &'a Function<'a>) -> Option<Candidate<'a>> {
    Some(Candidate {
        name: function.id.as_ref()?.name.as_str(),
        declaration: None,
        params: &function.params,
        body: function_body(function)?,
    })
}

fn constants<'a>(declaration: &'a VariableDeclaration<'a>) -> impl Iterator<Item = Candidate<'a>> {
    declaration
        .declarations
        .iter()
        .filter(|_| declaration.kind == VariableDeclarationKind::Const)
        .filter_map(|declarator| {
            let BindingPattern::BindingIdentifier(id) = &declarator.id else {
                return None;
            };
            let (params, body) = match unwrap_ts_wrappers(declarator.init.as_ref()?) {
                Expression::ArrowFunctionExpression(arrow) => {
                    use oxc_ast::ast::ArrowFunctionBody;
                    let body = if let Some(expression) = arrow.body.as_expression() {
                        body::Body::Expression(expression)
                    } else if let ArrowFunctionBody::FunctionBody(body) = &arrow.body {
                        body::Body::Block(&body.statements)
                    } else {
                        return None;
                    };
                    (&*arrow.params, body)
                }
                Expression::FunctionExpression(function) if function.id.is_none() => {
                    (&*function.params, function_body(function)?)
                }
                _ => return None,
            };
            Some(Candidate {
                name: id.name.as_str(),
                declaration: Some(id.span.start),
                params,
                body,
            })
        })
}

fn function_body<'a>(function: &'a Function<'a>) -> Option<body::Body<'a>> {
    let body = function.body.as_ref()?;
    (!function.generator).then_some(body::Body::Block(&body.statements))
}