1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// document: https://facebook.github.io/jsx/

use crate::lexer::span::{ Loc, Span, LineColumn, };
use crate::lexer::token::{ Identifier, LiteralString, LiteralBoolean, LiteralNumeric, };
use crate::ast::statement::Statement;
use crate::ast::expression::{ Expression, ParenthesizedExpression, };


// React only.
pub const CREATE_JSX_ELEMENT: &str = "React.createElement";
pub const CREATE_JSX_FRAGMENT: &str = "React.Fragment";

// IdentifierStart
// JSXIdentifier IdentifierPart
// JSXIdentifier NO WHITESPACE OR COMMENT -
pub type JSXIdentifier<'ast> = Identifier<'ast>;
pub type JSXAttributes<'ast> = &'ast [ JSXAttribute<'ast> ];

pub type JSXMemberExpression<'ast> = &'ast [ JSXIdentifier<'ast> ];
// SourceCharacter but not one of {, <, > or }
pub type JSXText<'ast> = LiteralString<'ast>;
pub type JSXChildExpression<'ast> = &'ast [ Expression<'ast> ];

pub type JSXChildren<'ast> = &'ast [ JSXChild<'ast> ];


// PrimaryExpression: JSXFragment, JSXElement
#[derive(Debug, PartialEq, Clone, Copy)]
pub struct JSXFragment<'ast> {
    pub loc: Loc,
    pub span: Span,
    pub children: Option<JSXChildren<'ast>>,
}

impl<'ast> JSXFragment<'ast> {
    pub fn loc(&self) -> Loc {
        self.loc
    }

    pub fn span(&self) -> Span {
        self.span
    }
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub enum JSXElement<'ast> {
    SelfClosing(JSXSelfClosingElement<'ast>),
    Normal(JSXNormalElement<'ast>),
}

impl<'ast> JSXElement<'ast> {
    pub fn loc(&self) -> Loc {
        match *self {
            JSXElement::SelfClosing(inner) => inner.loc,
            JSXElement::Normal(inner) => inner.loc,
        }
    }

    pub fn span(&self) -> Span {
        match *self {
            JSXElement::SelfClosing(inner) => inner.span,
            JSXElement::Normal(inner) => inner.span,
        }
    }

    pub fn is_self_closing(&self) -> bool {
        match *self {
            JSXElement::SelfClosing(_) => true,
            JSXElement::Normal(_) => false,
        }
    }

    pub fn is_element_name_match(&self) -> bool {
        match *self {
            JSXElement::SelfClosing(_) => true,
            JSXElement::Normal(ref elem) => elem.opening.name == elem.closing.name,
        }
    }
}



#[derive(Debug, PartialEq, Clone, Copy)]
pub struct JSXOpeningElement<'ast> {
    pub loc: Loc,
    pub span: Span,
    pub name: JSXElementName<'ast>,
    pub attrs: Option<JSXAttributes<'ast>>,
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub struct JSXClosingElement<'ast> {
    pub loc: Loc,
    pub span: Span,
    pub name: JSXElementName<'ast>,
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub struct JSXSelfClosingElement<'ast> {
    pub loc: Loc,
    pub span: Span,
    pub name: JSXElementName<'ast>,
    pub attrs: Option<JSXAttributes<'ast>>,
}


#[derive(Debug, PartialEq, Clone, Copy)]
pub struct JSXNormalElement<'ast> {
    pub loc: Loc,
    pub span: Span,
    pub opening: JSXOpeningElement<'ast>,
    pub children: Option<JSXChildren<'ast>>,
    pub closing: JSXClosingElement<'ast>,
}


#[derive(Debug, PartialEq, Clone, Copy)]
pub enum JSXElementName<'ast> {
    Identifier(JSXIdentifier<'ast>),
    NamespacedName(JSXNamespacedName<'ast>),
    MemberExpression(JSXMemberExpression<'ast>),
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub struct JSXNamespacedName<'ast> {
    pub loc: Loc,
    pub span: Span,
    pub namespace: JSXIdentifier<'ast>,
    pub name: JSXIdentifier<'ast>,
}


#[derive(Debug, PartialEq, Clone, Copy)]
pub enum JSXAttribute<'ast> {
    Normal(JSXNormalAttribute<'ast>),
    Spread(Expression<'ast>),
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub struct JSXNormalAttribute<'ast> {
    pub loc: Loc,
    pub span: Span,
    pub name: JSXNormalAttributeName<'ast>,
    pub init: Option<JSXNormalAttributeInitializer<'ast>>,
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub enum JSXNormalAttributeName<'ast> {
    Identifier(JSXIdentifier<'ast>),
    NamespacedName(JSXNamespacedName<'ast>),
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub enum JSXNormalAttributeInitializer<'ast> {
    Identifier(JSXIdentifier<'ast>),
    Assignment(Expression<'ast>),
    Element(JSXElement<'ast>),
    Fragment(JSXFragment<'ast>),
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub enum JSXChild<'ast> {
    Text(JSXText<'ast>),
    Element(JSXElement<'ast>),
    ChildExpression(Option<JSXChildExpression<'ast>>),
}