fluidattacks-blends-domain 0.2.0

Blends functional core: pure AST graph to syntax graph (no_std)
Documentation
//! Counterpart of `blends/syntax/builders/execution_block.py` (the `StackGraph`
//! scope wiring is excluded: the migration covers only AST, Syntax and CFG).

use crate::query::adj_ast;
use crate::syntax::builders::utils::{enter_scope_stack, exit_scope_stack_if_current};
use crate::syntax::SyntaxNode;
use crate::syntax::{SyntaxGraphArgs, SyntaxGraphError};
use crate::{CodeGraph, Language, NodeId};

pub fn build_execution_block_node(
    args: &mut SyntaxGraphArgs<'_>,
    n_id: NodeId,
    c_ids: &[NodeId],
) -> Result<NodeId, SyntaxGraphError> {
    args.syntax_graph.add_node(n_id, SyntaxNode::ExecutionBlock);

    let use_block_scope = matches!(args.language, Language::JavaScript | Language::TypeScript);

    if use_block_scope {
        enter_scope_stack(args.syntax_graph, args.metadata, n_id);
    }

    for c_id in c_ids {
        let child_id = if args.ast_graph.label_type(*c_id) == Some("expression_statement") {
            *adj_ast(args.ast_graph, *c_id, None, &[])
                .first()
                .ok_or(SyntaxGraphError::UnexpectedAstShape)?
        } else {
            *c_id
        };
        let built = args.generic(child_id)?;
        args.syntax_graph.add_ast_edge(n_id, built);
    }

    if use_block_scope {
        exit_scope_stack_if_current(args.metadata, n_id);
    }

    Ok(n_id)
}