use crate::{
query::{adj_ast, match_ast},
syntax::{
builders::for_statement::build_for_statement_node, SyntaxGraphArgs, SyntaxGraphError,
},
CodeGraph, NodeId,
};
pub fn reader(
args: &mut SyntaxGraphArgs<'_>,
n_id: NodeId,
) -> Result<Option<NodeId>, SyntaxGraphError> {
let graph = args.ast_graph;
let c_ids = match_ast(
graph,
n_id,
&[
"assignment_expression",
"binary_expression",
"update_expression",
],
Some(1),
);
let initializer_id = c_ids.get("assignment_expression").copied().flatten();
let condition_id = c_ids.get("binary_expression").copied().flatten();
let update_id = c_ids.get("update_expression").copied().flatten();
let mut body_id = adj_ast(graph, n_id, Some(1), &[])
.last()
.copied()
.ok_or(SyntaxGraphError::UnexpectedAstShape)?;
if graph.label_type(body_id) == Some("expression_statement") {
body_id = adj_ast(graph, body_id, Some(1), &[])
.first()
.copied()
.ok_or(SyntaxGraphError::UnexpectedAstShape)?;
}
build_for_statement_node(args, n_id, initializer_id, condition_id, update_id, body_id).map(Some)
}