-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathswc_helpers.rs
82 lines (73 loc) · 1.91 KB
/
swc_helpers.rs
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
use swc_common::DUMMY_SP;
use swc_ecmascript::ast::*;
use swc_ecmascript::utils::quote_ident;
pub fn rename_var_decl(new_name: &str, old: &str) -> ModuleItem {
ModuleItem::Stmt(Stmt::Decl(Decl::Var(Box::new(VarDecl {
span: DUMMY_SP,
kind: VarDeclKind::Const,
declare: false,
decls: vec![VarDeclarator {
span: DUMMY_SP,
name: pat_id(new_name),
init: Some(Box::new(Expr::Ident(quote_ident!(old)))),
definite: false,
}],
}))))
}
pub fn window_assign(name: &str, expr: Expr) -> ModuleItem {
ModuleItem::Stmt(Stmt::Expr(ExprStmt {
span: DUMMY_SP,
expr: Box::new(Expr::Assign(AssignExpr {
span: DUMMY_SP,
op: AssignOp::Assign,
left: PatOrExpr::Expr(Box::new(simple_member_expr("window", name))),
right: Box::new(expr),
})),
}))
}
pub fn pat_id(id: &str) -> Pat {
Pat::Ident(BindingIdent {
id: quote_ident!(id),
type_ann: None,
})
}
pub fn import_name(name: &str) -> ImportSpecifier {
ImportSpecifier::Named(ImportNamedSpecifier {
span: DUMMY_SP,
local: quote_ident!(name),
imported: None,
is_type_only: false,
})
}
pub fn new_member_expr(obj: Expr, key: &str) -> MemberExpr {
MemberExpr {
span: DUMMY_SP,
obj: Box::new(obj),
prop: MemberProp::Ident(quote_ident!(key)),
}
}
pub fn simple_member_expr(obj: &str, key: &str) -> Expr {
Expr::Member(MemberExpr {
span: DUMMY_SP,
obj: Box::new(Expr::Ident(quote_ident!(obj))),
prop: MemberProp::Ident(quote_ident!(key)),
})
}
pub fn is_call_expr_by_name(call: &CallExpr, name: &str) -> bool {
let callee = match &call.callee {
Callee::Super(_) => return false,
Callee::Import(_) => return name.eq("import"),
Callee::Expr(callee) => callee.as_ref(),
};
match callee {
Expr::Ident(id) => id.sym.as_ref().eq(name),
_ => false,
}
}
pub fn new_str(s: &str) -> Str {
Str {
span: DUMMY_SP,
value: s.into(),
raw: None,
}
}