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
#[macro_export]
macro_rules! private_ident {
($s:expr) => {
private_ident!($crate::swc_common::DUMMY_SP, $s)
};
($span:expr, $s:expr) => {{
let mark = $crate::swc_common::Mark::fresh($crate::swc_common::Mark::root());
let span = $span.apply_mark(mark);
$crate::swc_ecma_ast::Ident::new($s.into(), span)
}};
}
#[macro_export]
macro_rules! quote_ident {
($s:expr) => {
quote_ident!($crate::swc_common::DUMMY_SP, $s)
};
($span:expr, $s:expr) => {{
$crate::swc_ecma_ast::Ident::new($s.into(), $span)
}};
}
#[macro_export]
macro_rules! quote_str {
($s:expr) => {
quote_str!($crate::swc_common::DUMMY_SP, $s)
};
($span:expr, $s:expr) => {{
$crate::swc_ecma_ast::Str {
span: $span,
raw: None,
value: $s.into(),
}
}};
}
#[macro_export]
macro_rules! quote_expr {
($span:expr, null) => {{
use $crate::swc_ecma_ast::*;
Expr::Lit(Lit::Null(Null { span: $span }))
}};
($span:expr, undefined) => {{
box Expr::Ident(Ident::new(js_word!("undefined"), $span))
}};
}
#[macro_export]
macro_rules! member_expr {
($span:expr, $first:ident) => {{
use $crate::swc_ecma_ast::Expr;
Box::new(Expr::Ident($crate::quote_ident!($span, stringify!($first))))
}};
($span:expr, $first:ident . $($rest:tt)+) => {{
let obj = member_expr!($span, $first);
member_expr!(@EXT, $span, obj, $($rest)* )
}};
(@EXT, $span:expr, $obj:expr, $first:ident . $($rest:tt)* ) => {{
use $crate::swc_ecma_ast::MemberProp;
let prop = MemberProp::Ident($crate::quote_ident!($span, stringify!($first)));
member_expr!(@EXT, $span, Box::new(Expr::Member(MemberExpr{
span: $crate::swc_common::DUMMY_SP,
obj: $obj,
prop,
})), $($rest)*)
}};
(@EXT, $span:expr, $obj:expr, $first:ident) => {{
use $crate::swc_ecma_ast::*;
let prop = MemberProp::Ident($crate::quote_ident!($span, stringify!($first)));
Box::new(Expr::Member(MemberExpr{
span: $crate::swc_common::DUMMY_SP,
obj: $obj,
prop,
}))
}};
}
#[cfg(test)]
mod tests {
use swc_common::DUMMY_SP as span;
use swc_ecma_ast::*;
use crate::drop_span;
#[test]
fn quote_member_expr() {
let expr: Box<Expr> = drop_span(member_expr!(span, Function.prototype.bind));
assert_eq!(
expr,
Box::new(Expr::Member(MemberExpr {
span,
obj: Box::new(Expr::Member(MemberExpr {
span,
obj: member_expr!(span, Function),
prop: MemberProp::Ident(quote_ident!(span, "prototype")),
})),
prop: MemberProp::Ident(quote_ident!(span, "bind")),
}))
);
}
}