-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
61 lines (50 loc) · 1.38 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
const globals = new Set(['window', 'globalThis']);
/**
Collapse `window` and `globalThis`.
@param {import('@babel/core').NodePath} main - Node that may be on a global object.
@returns {import('@babel/core').NodePath} Collapsed node.
*/
function collapseGlobal(main) {
if (main.isMemberExpression()) {
const object = main.get('object');
if (object.isIdentifier() && globals.has(object.node.name) && main.has('property')) {
return main.get('property');
}
}
return main;
}
/**
@param {import('@babel/core').NodePath<import('@babel/core').CallExpression>} path
*/
function isConsoleNode(path) {
const expression = path.get('callee');
if (!expression.isMemberExpression()) {
return;
}
const main = collapseGlobal(expression.get('object'));
return main.isIdentifier({name: 'console'}) && expression.has('property');
}
/**
@param {import('@babel/core').NodePath<import('@babel/core').CallExpression>} path
*/
function isAlertNode(path) {
const main = collapseGlobal(path.get('callee'));
return main.isIdentifier({name: 'alert'});
}
/**
@returns {import('@babel/core').PluginObj}
*/
export default function stripDebugPlugin({types}) {
return {
visitor: {
DebuggerStatement(path) {
path.remove();
},
CallExpression(path) {
if (isConsoleNode(path) || isAlertNode(path)) {
path.replaceWith(types.unaryExpression('void', types.numericLiteral(0)));
}
},
},
};
}