use crate::syntax::fields::php::case_statement;
use crate::{
query::adj_ast,
syntax::{
builders::switch_section::build_switch_section_node, SyntaxGraphArgs, SyntaxGraphError,
},
utilities::text_nodes::node_to_str,
CodeGraph, NodeId,
};
use alloc::borrow::ToOwned;
use alloc::vec::Vec;
pub fn reader(
args: &mut SyntaxGraphArgs<'_>,
n_id: NodeId,
) -> Result<Option<NodeId>, SyntaxGraphError> {
let graph = args.ast_graph;
let case_n_id = args.required_field_alt(n_id, case_statement::VALUE)?;
let raw = node_to_str(graph, case_n_id);
let case_expr = if raw.starts_with(['"', '\'']) {
let mut chars = raw.chars();
chars.next();
chars.next_back();
chars.as_str().to_owned()
} else {
raw
};
let mut execution_ids: Vec<NodeId> = Vec::new();
for &id in adj_ast(graph, n_id, Some(1), &[]).iter().skip(3) {
if graph.label_type(id) == Some("expression_statement") {
if let Some(&first) = adj_ast(graph, id, Some(1), &[]).first() {
execution_ids.push(first);
continue;
}
}
execution_ids.push(id);
}
build_switch_section_node(args, n_id, case_expr, &execution_ids).map(Some)
}