swc_ecma_transforms_react/pure_annotations/
mod.rs1use rustc_hash::FxHashMap;
2use swc_atoms::Atom;
3use swc_common::{comments::Comments, Span};
4use swc_ecma_ast::*;
5use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith};
6
7#[cfg(test)]
8mod tests;
9
10pub fn pure_annotations<C>(comments: Option<C>) -> impl Pass
17where
18 C: Comments,
19{
20 visit_mut_pass(PureAnnotations {
21 imports: Default::default(),
22 comments,
23 })
24}
25
26struct PureAnnotations<C>
27where
28 C: Comments,
29{
30 imports: FxHashMap<Id, (Atom, Atom)>,
31 comments: Option<C>,
32}
33
34impl<C> VisitMut for PureAnnotations<C>
35where
36 C: Comments,
37{
38 noop_visit_mut_type!();
39
40 fn visit_mut_module(&mut self, module: &mut Module) {
41 for item in &module.body {
43 if let ModuleItem::ModuleDecl(ModuleDecl::Import(import)) = item {
44 let src_str = &*import.src.value;
45 if src_str != "react" && src_str != "react-dom" {
46 continue;
47 }
48
49 for specifier in &import.specifiers {
50 let src = import.src.value.clone();
51 match specifier {
52 ImportSpecifier::Named(named) => {
53 let imported = match &named.imported {
54 Some(ModuleExportName::Ident(imported)) => imported.sym.clone(),
55 Some(ModuleExportName::Str(..)) => named.local.sym.clone(),
56 None => named.local.sym.clone(),
57 };
58 self.imports.insert(named.local.to_id(), (src, imported));
59 }
60 ImportSpecifier::Default(default) => {
61 self.imports
62 .insert(default.local.to_id(), (src, "default".into()));
63 }
64 ImportSpecifier::Namespace(ns) => {
65 self.imports.insert(ns.local.to_id(), (src, "*".into()));
66 }
67 }
68 }
69 }
70 }
71
72 if self.imports.is_empty() {
73 return;
74 }
75
76 module.visit_mut_children_with(self);
78 }
79
80 fn visit_mut_call_expr(&mut self, call: &mut CallExpr) {
81 let is_react_call = match &call.callee {
82 Callee::Expr(expr) => match &**expr {
83 Expr::Ident(ident) => {
84 if let Some((src, specifier)) = self.imports.get(&ident.to_id()) {
85 is_pure(src, specifier)
86 } else {
87 false
88 }
89 }
90 Expr::Member(member) => match &*member.obj {
91 Expr::Ident(ident) => {
92 if let Some((src, specifier)) = self.imports.get(&ident.to_id()) {
93 if &**specifier == "default" || &**specifier == "*" {
94 match &member.prop {
95 MemberProp::Ident(ident) => is_pure(src, &ident.sym),
96 _ => false,
97 }
98 } else {
99 false
100 }
101 } else {
102 false
103 }
104 }
105 _ => false,
106 },
107 _ => false,
108 },
109 _ => false,
110 };
111
112 if is_react_call {
113 if let Some(comments) = &self.comments {
114 if call.span.lo.is_dummy() {
115 call.span.lo = Span::dummy_with_cmt().lo;
116 }
117
118 comments.add_pure_comment(call.span.lo);
119 }
120 }
121
122 call.visit_mut_children_with(self);
123 }
124}
125
126fn is_pure(src: &Atom, specifier: &Atom) -> bool {
127 match &**src {
128 "react" => matches!(
129 &**specifier,
130 "cloneElement"
131 | "createContext"
132 | "createElement"
133 | "createFactory"
134 | "createRef"
135 | "forwardRef"
136 | "isValidElement"
137 | "memo"
138 | "lazy"
139 ),
140 "react-dom" => matches!(&**specifier, "createPortal"),
141 _ => false,
142 }
143}