#[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::new();
let ctxt = $crate::swc_common::SyntaxContext::empty().apply_mark(mark);
$crate::swc_ecma_ast::Ident::new($s.into(), $span, ctxt)
}};
}
#[macro_export]
macro_rules! quote_ident {
($s:expr) => {{
let sym: $crate::swc_atoms::Atom = $s.into();
let id: $crate::swc_ecma_ast::IdentName = sym.into();
id
}};
($ctxt:expr, $s:expr) => {{
let sym: $crate::swc_atoms::Atom = $s.into();
let id: $crate::swc_ecma_ast::Ident =
$crate::swc_ecma_ast::Ident::new(sym, $crate::swc_common::DUMMY_SP, $ctxt);
id
}};
($ctxt:expr, $span:expr, $s:expr) => {{
$crate::swc_ecma_ast::Ident::new($s.into(), $span, $ctxt)
}};
}
#[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("undefined", $span))
}};
}
#[macro_export]
macro_rules! member_expr {
($ctxt:expr, $span:expr, $first:ident) => {{
use $crate::swc_ecma_ast::Expr;
Box::new(Expr::Ident($crate::quote_ident!($ctxt, $span, stringify!($first))))
}};
($ctxt:expr, $span:expr, $first:ident . $($rest:tt)+) => {{
let obj = member_expr!($ctxt, $span, $first);
member_expr!(@EXT, $span, obj, $($rest)* )
}};
(@EXT, $span:expr, $obj:expr, $first:ident . $($rest:tt)* ) => {{
use $crate::swc_ecma_ast::MemberProp;
use $crate::swc_ecma_ast::IdentName;
let prop = MemberProp::Ident(IdentName::new(stringify!($first).into(), $span));
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(IdentName::new(stringify!($first).into(), $span));
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!(
Default::default(),
Default::default(),
Function.prototype.bind
))
.into();
assert_eq!(
expr,
Box::new(Expr::Member(MemberExpr {
span,
obj: Box::new(Expr::Member(MemberExpr {
span,
obj: member_expr!(Default::default(), Default::default(), Function),
prop: MemberProp::Ident("prototype".into()),
})),
prop: MemberProp::Ident("bind".into()),
}))
);
}
}