-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
84 lines (78 loc) · 2.26 KB
/
index.js
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
const chainGet = require('chain-get');
const defualtOpts = {
debugger: true,
console: true
}
const visitor = {
DebuggerStatement(path, state) {
if (state) {
if (typeof state.opts === 'undefined') {
state.opts = defualtOpts;
}
if (typeof state.opts.debugger === 'undefined') {
state.opts.debugger = true;
}
if (state.opts.debugger) {
path.remove();
}
}
},
ExpressionStatement(path, state) {
if (state) {
if (typeof state.opts === 'undefined') {
state.opts = defualtOpts;
}
}
if (chainGet(path).node.expression.type() === 'CallExpression' && chainGet(path).node.expression.callee()) {
// remove alert
if (path.node.expression.callee.name === 'alert') {
if (state.opts.alert) {
path.remove()
}
return;
}
// remove console
if (path.node.expression.callee.type === 'MemberExpression' && chainGet(path).node.expression.callee.object.name() === 'console') {
if (state && state.opts) {
if (typeof state.opts.console === 'undefined') {
state.opts.console = true;
}
}
if (chainGet(state).opts.console()) {
path.remove();
}
return;
}
// remove customized debugger function
if (typeof chainGet(state).opts.debugFn() === 'string') {
const fn = state.opts.debugFn;
if (chainGet(path).node.expression.callee.name() === fn) {
path.remove();
}
}
}
},
FunctionDeclaration(path, state) {
if (!state || typeof state.opts === 'undefined' || !state.opts.debugFn || typeof state.opts.debugFn !== 'string') return;
if (chainGet(path).node.id.type() === 'Identifier' && path.node.id.name === state.opts.debugFn) {
path.remove();
}
},
VariableDeclarator(path, state) {
if (!state || typeof state.opts === 'undefined' || !state.opts.debugFn || typeof state.opts.debugFn !== 'string') return;
const fn = state.opts.debugFn;
if (chainGet(path).node.id.name() === fn) {
if (path.inList) {
path.remove();
} else {
const parentPath = path.parentPath;
parentPath.remove();
}
}
}
}
module.exports = function () {
return {
visitor
}
}