fluidattacks-blends-domain 0.6.1

Blends functional core: pure AST graph to syntax graph (no_std)
Documentation
use alloc::borrow::ToOwned;

use crate::query::match_ast;
use crate::syntax::builders::variable_declaration::build_variable_declaration_node;
use crate::syntax::{SyntaxGraphArgs, SyntaxGraphError};
use crate::utilities::text_nodes::node_to_str;
use crate::{CodeGraph, NodeId};

pub fn reader(
    args: &mut SyntaxGraphArgs<'_>,
    n_id: NodeId,
) -> Result<Option<NodeId>, SyntaxGraphError> {
    let graph = args.ast_graph;
    let childs = match_ast(graph, n_id, &["="], None);
    let var_name = childs.get("__0__").copied().flatten().map_or_else(
        || "UnnamedVar".to_owned(),
        |var_id| node_to_str(graph, var_id),
    );

    let mut value_id = childs.get("__1__").copied().flatten();
    if let Some(id) = value_id {
        if graph.label_type(id) == Some("jsx_expression") {
            value_id = match_ast(graph, id, &["{"], None)
                .get("__0__")
                .copied()
                .flatten();
        }
    }

    build_variable_declaration_node(args, n_id, var_name, None, value_id, None, None).map(Some)
}