/
remove_console.rs
146 lines (128 loc) · 3.95 KB
/
remove_console.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
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
135
136
137
138
139
140
141
142
143
144
145
146
use serde::Deserialize;
use swc_atoms::JsWord;
use swc_common::collections::AHashSet;
use swc_common::DUMMY_SP;
use swc_ecmascript::ast::*;
use swc_ecmascript::utils::ident::IdentLike;
use swc_ecmascript::utils::Id;
use swc_ecmascript::visit::{noop_fold_type, Fold, FoldWith};
use crate::top_level_binding_collector::collect_top_level_decls;
#[derive(Clone, Debug, Deserialize)]
#[serde(untagged)]
pub enum Config {
All(bool),
WithOptions(Options),
}
impl Config {
pub fn truthy(&self) -> bool {
match self {
Config::All(b) => *b,
Config::WithOptions(_) => true,
}
}
}
#[derive(Clone, Debug, Deserialize)]
pub struct Options {
#[serde(default)]
pub exclude: Vec<JsWord>,
}
struct RemoveConsole {
exclude: Vec<JsWord>,
bindings: Vec<AHashSet<Id>>,
in_function_params: bool,
}
impl RemoveConsole {
fn is_global_console(&self, ident: &Ident) -> bool {
&ident.sym == "console"
&& self
.bindings
.iter()
.find(|x| x.contains(&ident.to_id()))
.is_none()
}
fn should_remove_call(&mut self, n: &CallExpr) -> bool {
let callee = &n.callee;
let member_expr = match callee {
ExprOrSuper::Expr(e) => match &**e {
Expr::Member(m) => m,
_ => return false,
},
_ => return false,
};
// Don't attempt to evaluate computed properties.
if member_expr.computed {
return false;
}
// Only proceed if the object is the global `console` object.
match &member_expr.obj {
ExprOrSuper::Expr(e) => match &**e {
Expr::Ident(i) if self.is_global_console(i) => {}
_ => return false,
},
_ => return false,
}
// Check if the property is requested to be excluded.
// Here we do an O(n) search on the list of excluded properties because the size
// should be small.
match &*member_expr.prop {
Expr::Ident(i) if self.exclude.iter().find(|x| **x == i.sym).is_none() => {}
_ => return false,
}
true
}
}
impl Fold for RemoveConsole {
noop_fold_type!();
fn fold_stmt(&mut self, stmt: Stmt) -> Stmt {
match &stmt {
Stmt::Expr(e) => match &*e.expr {
Expr::Call(c) => {
if self.should_remove_call(c) {
return Stmt::Empty(EmptyStmt { span: DUMMY_SP });
}
}
_ => {}
},
_ => {}
}
stmt.fold_children_with(self)
}
fn fold_function(&mut self, mut func: Function) -> Function {
self.in_function_params = true;
let mut new_params: AHashSet<Id> = AHashSet::default();
for param in &func.params {
new_params.extend(collect_top_level_decls(param));
}
self.in_function_params = false;
self.bindings.push(new_params);
self.bindings.push(collect_top_level_decls(&func));
func.body = func.body.fold_with(self);
self.bindings.pop().unwrap();
self.bindings.pop().unwrap();
func
}
fn fold_module(&mut self, module: Module) -> Module {
self.bindings.push(collect_top_level_decls(&module));
let m = module.fold_children_with(self);
self.bindings.pop().unwrap();
m
}
fn fold_script(&mut self, script: Script) -> Script {
self.bindings.push(collect_top_level_decls(&script));
let s = script.fold_with(self);
self.bindings.pop().unwrap();
s
}
}
pub fn remove_console(config: Config) -> impl Fold {
let exclude = match config {
Config::WithOptions(x) => x.exclude,
_ => vec![],
};
let remover = RemoveConsole {
exclude,
bindings: Default::default(),
in_function_params: false,
};
remover
}