-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (51 loc) · 1.33 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
"use strict";
module.exports = function vanillaShake() {
let defined, definedKeys;
const isDefined = (name) => definedKeys.includes(name);
return {
visitor: {
Program(path, PluginPass) {
defined = PluginPass.opts.defined;
definedKeys = Object.keys(defined || {});
},
Conditional: {
exit
}
}
};
function exit(path) {
const test = path.node.test;
const {name, operator, type, argument} = test;
if (evaluateUnary()) return;
if (evaluateIdentifier()) return;
function evaluateUnary() {
if (type !== "UnaryExpression" || operator !== "!") return;
evaluateDefined(argument.name, true);
return true;
}
function evaluateIdentifier() {
if (type !== "Identifier") return;
evaluateDefined(name, false);
return true;
}
function evaluateDefined(definedName, condition) {
if (!isDefined(definedName)) return;
if (defined[definedName] === condition) {
replace(path.get("alternate"));
} else {
replace(path.get("consequent"));
}
}
function replace({node} = {}) {
if (!node) {
path.remove();
return;
}
if (node.type === "BlockStatement") {
path.replaceWithMultiple(node.body);
} else {
path.replaceWith(node);
}
}
}
};