swc_ecma_transforms_optimization/simplify/
const_propagation.rs1#![allow(clippy::borrowed_box)]
2
3use rustc_hash::FxHashMap;
4use swc_common::util::take::Take;
5use swc_ecma_ast::*;
6use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith};
7
8pub fn constant_propagation() -> impl 'static + Pass + VisitMut {
10 visit_mut_pass(ConstPropagation::default())
11}
12
13#[derive(Default)]
14struct ConstPropagation<'a> {
15 scope: Scope<'a>,
16}
17#[derive(Default)]
18struct Scope<'a> {
19 parent: Option<&'a Scope<'a>>,
20 vars: FxHashMap<Id, Box<Expr>>,
22}
23
24impl<'a> Scope<'a> {
25 fn new(parent: &'a Scope<'a>) -> Self {
26 Self {
27 parent: Some(parent),
28 vars: Default::default(),
29 }
30 }
31
32 fn find_var(&self, id: &Id) -> Option<&Box<Expr>> {
33 if let Some(v) = self.vars.get(id) {
34 return Some(v);
35 }
36
37 self.parent.and_then(|parent| parent.find_var(id))
38 }
39}
40
41impl VisitMut for ConstPropagation<'_> {
42 noop_visit_mut_type!(fail);
43
44 fn visit_mut_assign_expr(&mut self, _: &mut AssignExpr) {}
46
47 fn visit_mut_export_named_specifier(&mut self, n: &mut ExportNamedSpecifier) {
48 let id = match &n.orig {
49 ModuleExportName::Ident(ident) => ident.to_id(),
50 ModuleExportName::Str(..) => return,
51 };
52 if let Some(expr) = self.scope.find_var(&id) {
53 if let Expr::Ident(v) = &**expr {
54 let orig = n.orig.clone();
55 n.orig = ModuleExportName::Ident(v.clone());
56
57 if n.exported.is_none() {
58 n.exported = Some(orig);
59 }
60 }
61 }
62
63 match &n.exported {
64 Some(ModuleExportName::Ident(exported)) => match &n.orig {
65 ModuleExportName::Ident(orig) => {
66 if exported.sym == orig.sym && exported.ctxt == orig.ctxt {
67 n.exported = None;
68 }
69 }
70 ModuleExportName::Str(..) => {}
71 },
72 Some(ModuleExportName::Str(..)) => {}
73 None => {}
74 }
75 }
76
77 fn visit_mut_expr(&mut self, e: &mut Expr) {
78 if let Expr::Ident(i) = e {
79 if let Some(expr) = self.scope.find_var(&i.to_id()) {
80 *e = *expr.clone();
81 return;
82 }
83 }
84
85 e.visit_mut_children_with(self);
86 }
87
88 fn visit_mut_function(&mut self, n: &mut Function) {
95 let scope = Scope::new(&self.scope);
96 let mut v = ConstPropagation { scope };
97 n.visit_mut_children_with(&mut v);
98 }
99
100 fn visit_mut_prop(&mut self, p: &mut Prop) {
101 p.visit_mut_children_with(self);
102
103 if let Prop::Shorthand(i) = p {
104 if let Some(expr) = self.scope.find_var(&i.to_id()) {
105 *p = Prop::KeyValue(KeyValueProp {
106 key: PropName::Ident(i.take().into()),
107 value: expr.clone(),
108 });
109 }
110 }
111 }
112
113 fn visit_mut_var_decl(&mut self, var: &mut VarDecl) {
114 var.decls.visit_mut_with(self);
115
116 if let VarDeclKind::Const = var.kind {
117 for decl in &var.decls {
118 if let Pat::Ident(name) = &decl.name {
119 if let Some(init) = &decl.init {
120 match &**init {
121 Expr::Lit(Lit::Bool(..))
122 | Expr::Lit(Lit::Num(..))
123 | Expr::Lit(Lit::Null(..)) => {
124 self.scope.vars.insert(name.to_id(), init.clone());
125 }
126
127 Expr::Ident(init)
128 if name.span.is_dummy()
129 || var.span.is_dummy()
130 || init.span.is_dummy() =>
131 {
132 if let Some(value) = self.scope.vars.get(&init.to_id()).cloned() {
134 self.scope.vars.insert(name.to_id(), value);
135 } else {
136 self.scope.vars.insert(name.to_id(), init.clone().into());
137 }
138 }
139 _ => {}
140 }
141 }
142 }
143 }
144 }
145 }
146}