-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
174 lines (154 loc) · 4.39 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// @flow
/*::
type PluginOpts = {
falsyExpressions: Array<string>,
truthyExpressions: Array<string>,
};
*/
module.exports = (
api /*: any */,
{
falsyExpressions: falsyOpt = [],
truthyExpressions: truthyOpt = []
} /*: PluginOpts */ = {}
) => {
const { types: t } = api;
if (!Array.isArray(falsyOpt)) {
throw new Error("falsyExpressions must be an array");
}
if (!Array.isArray(truthyOpt)) {
throw new Error("truthyExpressions must be an array");
}
function stringToExpression(expr) {
if (typeof expr !== "string") {
expr = String(expr);
}
try {
const {
program: { body }
} = api.parse(expr);
if (body.length !== 1) {
throw new Error();
}
const [statement] = body;
if (!t.isExpressionStatement(statement)) {
throw new Error();
}
return statement.expression;
} catch (err) {
throw new Error(`Failed to parse "${expr}" as a valid expression`);
}
}
const falsyExpressions = [
t.booleanLiteral(false),
...falsyOpt.map(stringToExpression)
];
const truthyExpressions = [
t.booleanLiteral(true),
...truthyOpt.map(stringToExpression)
];
function isFalsyPath(path) {
return falsyExpressions.some(falsy =>
t.isNodesEquivalent(path.node, falsy)
);
}
function isTruthyPath(path) {
return truthyExpressions.some(truthy =>
t.isNodesEquivalent(path.node, truthy)
);
}
function isCertainlyFalsyExpression(path) {
// traverse chained LogicalExpressions
// to handle cases like: `false && unknown && to_be_shaken`
let _path = path;
while (_path) {
if (
_path.type === "LogicalExpression" &&
_path.get("operator").node === "&&"
) {
if (isCertainlyFalsyExpression(_path.get("right"))) {
return true;
}
_path = _path.get("left");
if (isCertainlyFalsyExpression(_path)) {
return true;
}
} else {
break;
}
}
if (isFalsyPath(_path)) {
return true;
}
}
function isPathCertainlyUnreachable(path) {
while (path) {
if (
path.parentPath &&
(path.parentPath.type === "IfStatement" ||
path.parentPath.type === "ConditionalExpression")
) {
const consequent = path.parentPath.get("consequent");
const alternate = path.parentPath.get("alternate");
const test = path.parentPath.get("test");
if (consequent === path && isCertainlyFalsyExpression(test)) {
return true;
}
if (alternate === path && isTruthyPath(test)) {
return true;
}
}
if (isCertainlyFalsyExpression(path)) {
return true;
}
path = path.parentPath;
}
}
return {
name: "babel-plugin-transform-prune-unused-imports",
visitor: {
ImportDeclaration(path /* :any */) {
if (path.removed) {
return;
}
const specifiers = path.get("specifiers");
// Imports with no specifiers is probably specifically for side effects
let shakeDeclaration = specifiers.length > 0;
for (const specifier of specifiers) {
let shakeSpecifier = true;
const localPath = specifier.get("local");
const localName = localPath.node.name;
// This should not be hardcoded to React and/or improve compat with JSX transform
if (localName === "React") {
shakeSpecifier = false;
shakeDeclaration = false;
break;
}
const binding = localPath.scope.bindings[localName];
if (binding) {
const refPaths = binding.referencePaths;
for (const path of refPaths) {
const unreachable = isPathCertainlyUnreachable(path);
if (!unreachable) {
shakeSpecifier = false;
shakeDeclaration = false;
}
}
} else {
// If binding doesn't exist, then this is an indication the import was
// added by a plugin (rather existing than the original source code)
// To be conservative, don't shake in this case.
shakeSpecifier = false;
shakeDeclaration = false;
}
if (shakeSpecifier) {
specifier.remove();
}
}
if (shakeDeclaration) {
path.remove();
}
}
}
};
};