swc_ecma_transforms_base/
ext.rs

1//! Do not use: This is not a public api and it can be changed without a version
2//! bump.
3
4use swc_ecma_ast::*;
5use swc_ecma_utils::ExprExt;
6
7pub trait ExprRefExt: ExprExt {
8    fn as_ident(&self) -> Option<&Ident> {
9        match self.as_expr() {
10            Expr::Ident(ref i) => Some(i),
11            _ => None,
12        }
13    }
14}
15
16impl<T> ExprRefExt for T where T: ExprExt {}
17
18/// Do not use: This is not a public api and it can be changed without a version
19/// bump.
20pub trait AsOptExpr {
21    fn as_expr(&self) -> Option<&Expr>;
22    fn as_expr_mut(&mut self) -> Option<&mut Expr>;
23}
24
25impl AsOptExpr for Callee {
26    fn as_expr(&self) -> Option<&Expr> {
27        match self {
28            Callee::Super(_) | Callee::Import(_) => None,
29            Callee::Expr(e) => Some(e),
30            #[cfg(swc_ast_unknown)]
31            _ => None,
32        }
33    }
34
35    fn as_expr_mut(&mut self) -> Option<&mut Expr> {
36        match self {
37            Callee::Super(_) | Callee::Import(_) => None,
38            Callee::Expr(e) => Some(e),
39            #[cfg(swc_ast_unknown)]
40            _ => None,
41        }
42    }
43}
44
45impl<N> AsOptExpr for Option<N>
46where
47    N: AsOptExpr,
48{
49    fn as_expr(&self) -> Option<&Expr> {
50        match self {
51            Some(n) => n.as_expr(),
52            None => None,
53        }
54    }
55
56    fn as_expr_mut(&mut self) -> Option<&mut Expr> {
57        match self {
58            None => None,
59            Some(n) => n.as_expr_mut(),
60        }
61    }
62}