Skip to main content

react_compiler_ast/
lib.rs

1pub mod common;
2pub mod declarations;
3pub mod expressions;
4pub mod jsx;
5pub mod literals;
6pub mod operators;
7pub mod patterns;
8pub mod scope;
9pub mod statements;
10pub mod visitor;
11
12use serde::Serialize;
13
14use crate::common::{BaseNode, Comment};
15use crate::expressions::Expression;
16use crate::patterns::PatternLike;
17use crate::statements::{Directive, Statement};
18
19/// An original source AST node preserved verbatim for re-emission when the
20/// compiler bails on a construct it does not model (`UnsupportedNode`).
21///
22/// Holding the typed node directly — rather than a `serde_json::Value` — lets
23/// lowering stash it and codegen restore it without round-tripping through
24/// serde, which is what kept the AST (de)serializers out of the generated
25/// binary. The variant records which syntactic position the node came from, so
26/// codegen can dispatch without re-parsing a `type` tag.
27#[derive(Debug, Clone)]
28pub enum OriginalNode {
29    Expression(Box<Expression>),
30    Statement(Box<Statement>),
31    Pattern(Box<PatternLike>),
32}
33
34/// The root type returned by @babel/parser
35#[derive(Debug, Clone, Serialize)]
36pub struct File {
37    #[serde(flatten)]
38    pub base: BaseNode,
39    pub program: Program,
40    #[serde(default)]
41    pub comments: Vec<Comment>,
42    #[serde(default)]
43    pub errors: Vec<serde_json::Value>,
44}
45
46#[derive(Debug, Clone, Serialize)]
47pub struct Program {
48    #[serde(flatten)]
49    pub base: BaseNode,
50    pub body: Vec<Statement>,
51    #[serde(default)]
52    pub directives: Vec<Directive>,
53    #[serde(rename = "sourceType")]
54    pub source_type: SourceType,
55    #[serde(default)]
56    pub interpreter: Option<InterpreterDirective>,
57    #[serde(
58        rename = "sourceFile",
59        default,
60        skip_serializing_if = "Option::is_none"
61    )]
62    pub source_file: Option<String>,
63}
64
65#[derive(Debug, Clone, Serialize)]
66#[serde(rename_all = "lowercase")]
67pub enum SourceType {
68    Module,
69    Script,
70}
71
72#[derive(Debug, Clone, Serialize)]
73pub struct InterpreterDirective {
74    #[serde(flatten)]
75    pub base: BaseNode,
76    pub value: String,
77}