forked from babel/minify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
49 lines (43 loc) · 1.06 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
"use strict";
module.exports = function({ types: t }) {
const INFINITY = t.binaryExpression(
"/",
t.numericLiteral(1),
t.numericLiteral(0)
);
const badTransforms = {
ArrayPattern: () => true,
AssignmentExpression: path => path.container.left === path.node,
ObjectProperty: path => path.container.shorthand,
RestElement: () => true
};
return {
name: "minify-infinity",
visitor: {
// Infinity -> 1 / 0
Identifier(path) {
path.isLVal();
if (path.node.name !== "Infinity") {
return;
}
// It's a referenced identifier
if (path.scope.getBinding("Infinity")) {
return;
}
if (path.parentPath.isObjectProperty({ key: path.node })) {
return;
}
if (path.parentPath.isMemberExpression()) {
return;
}
if (
path.isLVal() &&
(badTransforms[path.parentPath.type] || (() => false))(path)
) {
return;
}
path.replaceWith(INFINITY);
}
}
};
};