use crate::query::{get_node_by_path, match_ast_d, match_ast_group_d};
use crate::syntax::builders::jsx_element::build_jsx_element_node;
use crate::syntax::{SyntaxGraphArgs, SyntaxGraphError};
use crate::NodeId;
pub fn reader(
args: &mut SyntaxGraphArgs<'_>,
n_id: NodeId,
) -> Result<Option<NodeId>, SyntaxGraphError> {
let graph = args.ast_graph;
let mut childs_with_attrs = match_ast_group_d(graph, n_id, "jsx_attribute", None);
let mut childs = match_ast_group_d(graph, n_id, "jsx_opening_element", None);
childs.extend(match_ast_group_d(
graph,
n_id,
"jsx_self_closing_element",
None,
));
let single = childs.len() == 1;
for child in &childs {
let jsx_attributes = match_ast_group_d(graph, *child, "jsx_attribute", None);
if !jsx_attributes.is_empty() && single {
childs_with_attrs.extend(jsx_attributes);
} else if !jsx_attributes.is_empty() {
childs_with_attrs.push(*child);
}
if let Some(identifier) = match_ast_d(graph, *child, "identifier", None) {
childs_with_attrs.push(identifier);
}
if let Some(nested) = match_ast_d(graph, *child, "nested_identifier", None) {
childs_with_attrs.push(nested);
}
if let Some(spread) = get_node_by_path(graph, *child, &["jsx_expression", "spread_element"])
{
childs_with_attrs.push(spread);
}
}
childs_with_attrs.extend(match_ast_group_d(graph, n_id, "jsx_element", None));
build_jsx_element_node(args, n_id, &childs_with_attrs).map(Some)
}