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)
}