esr/ast/
function.rs

1use crate::ast::{Node, Loc, IdentifierNode, ExpressionNode};
2use crate::ast::{BlockNode, Statement, PatternList, PropertyKey};
3
4pub trait Name<'ast>: Copy {
5    fn empty() -> Self;
6}
7
8#[derive(Debug, PartialEq, Clone, Copy)]
9pub struct EmptyName;
10
11#[derive(Debug, PartialEq, Clone, Copy)]
12pub struct MandatoryName<'ast>(pub IdentifierNode<'ast>);
13
14#[derive(Debug, PartialEq, Clone, Copy)]
15pub struct OptionalName<'ast>(pub Option<IdentifierNode<'ast>>);
16
17pub type Method<'ast> = Function<'ast, EmptyName>;
18
19impl<'ast> Name<'ast> for EmptyName {
20    fn empty() -> Self {
21        EmptyName
22    }
23}
24
25impl<'ast> Name<'ast> for MandatoryName<'ast> {
26    fn empty() -> Self {
27        MandatoryName(Node::new(&Loc {
28            start: 0,
29            end: 0,
30            item: ""
31        }))
32    }
33}
34
35impl<'ast> Name<'ast> for OptionalName<'ast> {
36    fn empty() -> Self {
37        OptionalName(None)
38    }
39}
40
41#[cfg(test)]
42impl<'ast> From<IdentifierNode<'ast>> for MandatoryName<'ast> {
43    #[inline]
44    fn from(name: IdentifierNode<'ast>) -> Self {
45        MandatoryName(name)
46    }
47}
48
49#[cfg(test)]
50impl<'ast> From<IdentifierNode<'ast>> for OptionalName<'ast> {
51    #[inline]
52    fn from(name: IdentifierNode<'ast>) -> Self {
53        OptionalName(Some(name))
54    }
55}
56
57#[cfg(test)]
58impl<'ast> From<Option<IdentifierNode<'ast>>> for OptionalName<'ast> {
59    #[inline]
60    fn from(name: Option<IdentifierNode<'ast>>) -> Self {
61        OptionalName(name)
62    }
63}
64
65#[derive(Debug, PartialEq, Clone, Copy)]
66pub struct Function<'ast, N: Name<'ast>> {
67    pub name: N,
68    pub generator: bool,
69    pub params: PatternList<'ast>,
70    pub body: BlockNode<'ast, Statement<'ast>>,
71}
72
73#[derive(Debug, PartialEq, Clone, Copy)]
74pub enum MethodKind {
75    Constructor,
76    Method,
77    Get,
78    Set,
79}
80
81#[derive(Debug, PartialEq, Clone, Copy)]
82pub enum ClassMember<'ast> {
83    Error,
84    Method {
85        is_static: bool,
86        key: Node<'ast, PropertyKey<'ast>>,
87        kind: MethodKind,
88        value: Node<'ast, Function<'ast, EmptyName>>,
89    },
90    Literal {
91        is_static: bool,
92        key: Node<'ast, PropertyKey<'ast>>,
93        value: ExpressionNode<'ast>,
94    }
95}
96
97#[derive(Debug, PartialEq, Clone, Copy)]
98pub struct Class<'ast, N: Name<'ast>> {
99    pub name: N,
100    pub extends: Option<ExpressionNode<'ast>>,
101    pub body: BlockNode<'ast, ClassMember<'ast>>,
102}