use alloc::vec::Vec;
use crate::query::match_ast_group_d;
use crate::syntax::builders::export_statement::{build_export_statement_node, ExportStatement};
use crate::syntax::fields::javascript::export_statement;
use crate::syntax::readers::javascript::common::strip_quotes;
use crate::syntax::{SyntaxGraphArgs, SyntaxGraphError};
use crate::utilities::text_nodes::node_to_str;
use crate::{CodeGraph, NodeId};
fn specifier_ids(args: &SyntaxGraphArgs<'_>, n_id: NodeId) -> Vec<NodeId> {
let graph = args.ast_graph;
match_ast_group_d(graph, n_id, "export_clause", Some(1))
.into_iter()
.flat_map(|clause_id| match_ast_group_d(graph, clause_id, "export_specifier", Some(1)))
.collect()
}
pub fn reader(
args: &mut SyntaxGraphArgs<'_>,
n_id: NodeId,
) -> Result<Option<NodeId>, SyntaxGraphError> {
let graph = args.ast_graph;
let mut statement = ExportStatement::default();
let mut declaration_id = args.optional_field_alt(n_id, export_statement::DECLARATION);
if let Some(block) = declaration_id {
if matches!(graph.label_type(block), Some("identifier" | "string")) {
statement.expression = Some(node_to_str(graph, block));
declaration_id = None;
}
} else if let Some(child_id) = match_ast_group_d(graph, n_id, "identifier", Some(1))
.first()
.copied()
{
statement.expression = Some(node_to_str(graph, child_id));
}
statement.declaration_id = declaration_id;
statement.specifier_ids = specifier_ids(args, n_id);
if let Some(source_id) = args.optional_field_alt(n_id, export_statement::SOURCE) {
statement.expression = Some(strip_quotes(&node_to_str(graph, source_id)));
}
if let Some(namespace_id) = match_ast_group_d(graph, n_id, "namespace_export", Some(1))
.first()
.copied()
{
statement.alias = match_ast_group_d(graph, namespace_id, "identifier", Some(1))
.first()
.map(|alias_id| node_to_str(graph, *alias_id));
}
statement.default_id = match_ast_group_d(graph, n_id, "default", Some(1))
.first()
.copied();
build_export_statement_node(args, n_id, statement).map(Some)
}