fluidattacks-blends-domain 0.17.1

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

use crate::query::{match_ast_group_d, pred_ast};
use crate::syntax::builders::class_declaration::build_class_node;
use crate::syntax::fields::javascript::class_declaration;
use crate::syntax::{SyntaxGraphArgs, SyntaxGraphError};
use crate::utilities::text_nodes::node_to_str;
use crate::{CodeGraph, NodeId};

/// Collects every decorator applied to a class. A plain class carries them
/// itself, but an exported one does not: the grammar hands them to the
/// `export_statement` above it, so that parent is read as well.
fn decorator_ids(args: &SyntaxGraphArgs<'_>, n_id: NodeId) -> Vec<NodeId> {
    let graph = args.ast_graph;
    let own = match_ast_group_d(graph, n_id, "decorator", Some(1));
    if !own.is_empty() {
        return own;
    }

    pred_ast(graph, n_id, None)
        .first()
        .filter(|p_id| graph.label_type(**p_id) == Some("export_statement"))
        .map(|p_id| match_ast_group_d(graph, *p_id, "decorator", Some(1)))
        .unwrap_or_default()
}

pub fn reader(
    args: &mut SyntaxGraphArgs<'_>,
    n_id: NodeId,
) -> Result<Option<NodeId>, SyntaxGraphError> {
    let name_id = args.required_field_alt(n_id, class_declaration::NAME)?;
    let name = node_to_str(args.ast_graph, name_id);
    let block_id = args.required_field_alt(n_id, class_declaration::BODY)?;
    let attr_list_ids = decorator_ids(args, n_id);

    build_class_node(
        args,
        n_id,
        name,
        Some(block_id),
        &attr_list_ids,
        None,
        None,
        None,
    )
    .map(Some)
}