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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
use std::collections::HashMap;
use swc_atoms::{js_word, JsWord};
use swc_common::comments::Comments;
use swc_ecma_ast::*;
use swc_ecma_utils::{id, Id};
use swc_ecma_visit::{Fold, FoldWith};
#[cfg(test)]
mod tests;
pub fn pure_annotations<C>(comments: Option<C>) -> impl Fold
where
C: Comments,
{
PureAnnotations {
imports: HashMap::new(),
comments,
}
}
struct PureAnnotations<C>
where
C: Comments,
{
imports: HashMap<Id, (JsWord, JsWord)>,
comments: Option<C>,
}
impl<C> Fold for PureAnnotations<C>
where
C: Comments,
{
fn fold_module(&mut self, module: Module) -> Module {
for item in &module.body {
match item {
ModuleItem::ModuleDecl(ModuleDecl::Import(import)) => {
let src_str = import.src.value.to_string();
if src_str != "react" && src_str != "react-dom" {
continue;
}
for specifier in &import.specifiers {
let src = import.src.value.clone();
match specifier {
ImportSpecifier::Named(named) => {
let imported = match &named.imported {
Some(imported) => imported.sym.clone(),
None => named.local.sym.clone(),
};
self.imports.insert(id(&named.local), (src, imported));
}
ImportSpecifier::Default(default) => {
self.imports
.insert(id(&default.local), (src, js_word!("default")));
}
ImportSpecifier::Namespace(ns) => {
self.imports.insert(id(&ns.local), (src, "*".into()));
}
}
}
}
_ => {}
}
}
module.fold_children_with(self)
}
fn fold_call_expr(&mut self, call: CallExpr) -> CallExpr {
let is_react_call = match &call.callee {
ExprOrSuper::Expr(expr) => match &**expr {
Expr::Ident(ident) => {
if let Some((src, specifier)) = self.imports.get(&id(&ident)) {
is_pure(src, specifier)
} else {
false
}
}
Expr::Member(member) => match &member.obj {
ExprOrSuper::Expr(expr) => match &**expr {
Expr::Ident(ident) => {
if let Some((src, specifier)) = self.imports.get(&id(&ident)) {
let specifier_str = specifier.to_string();
if specifier_str == "default" || specifier_str == "*" {
match &*member.prop {
Expr::Ident(ident) => is_pure(src, &ident.sym),
_ => false,
}
} else {
false
}
} else {
false
}
}
_ => false,
},
_ => false,
},
_ => false,
},
_ => false,
};
if is_react_call {
if let Some(comments) = &self.comments {
comments.add_pure_comment(call.span.lo);
}
}
call.fold_children_with(self)
}
}
fn is_pure(src: &JsWord, specifier: &JsWord) -> bool {
match src.to_string().as_str() {
"react" => match specifier.to_string().as_str() {
"cloneElement" | "createContext" | "createElement" | "createFactory" | "createRef"
| "forwardRef" | "isValidElement" | "memo" | "lazy" => true,
_ => false,
},
"react-dom" => match specifier.to_string().as_str() {
"createPortal" => true,
_ => false,
},
_ => false,
}
}