-
-
Notifications
You must be signed in to change notification settings - Fork 225
/
minify-builtins.js
187 lines (173 loc) · 4.91 KB
/
minify-builtins.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
175
176
177
178
179
180
181
182
183
184
185
186
187
jest.autoMockOff();
const babel = require("babel-core");
const unpad = require("../../../utils/unpad");
const plugin = require("../src/index");
function transform(code) {
return babel.transform(code, {
plugins: [plugin]
}).code;
}
describe("minify-builtins", () => {
it("should minify standard built in methods", () => {
const source = unpad(`
function c() {
let a = 10;
const d = Number.isNaN(a);
Math.max(a, b) + Math.max(b, a);
return d && Number.isFinite(a);
}
`);
// Jest arranges in alphabetical order, So keeping it as _source
expect({ _source: source, expected: transform(source) }).toMatchSnapshot();
});
it("should minify standard built in properties", () => {
const source = unpad(`
function a () {
Number.NAN + Number.NAN;
return Math.PI + Math.PI + Number.EPSILON + Number.NAN;
}
`);
expect({ _source: source, expected: transform(source) }).toMatchSnapshot();
});
it("should take no of occurences in to account", () => {
const source = unpad(`
function a() {
Math.floor(a) + Math.floor(b) + Math.min(a, b);
}
`);
expect({ _source: source, expected: transform(source) }).toMatchSnapshot();
});
it("should collect and minify no matter any depth", () => {
const source = unpad(`
function a (){
Math.max(b, a);
const b = () => {
const a = Math.floor(c);
Math.min(b, a) * Math.floor(b);
function c() {
Math.floor(c) + Math.min(b, a)
}
}
}
`);
expect({ _source: source, expected: transform(source) }).toMatchSnapshot();
});
it("shouldn't minify builtins in the program scope to avoid leaking", () => {
const source = unpad(`
Math.max(c, d)
function a (){
Math.max(b, a) + Math.max(c, d);
}
Math.max(e, f)
`);
expect({ _source: source, expected: transform(source) }).toMatchSnapshot();
});
it("should minify builtins to method scope for class declarations", () => {
const source = unpad(`
class Test {
foo() {
Math.max(c, d)
Math.max(c, d)
const c = function() {
Math.max(c, d)
Math.floor(m);
Math.floor(m);
}
}
bar() {
Math.min(c, d)
Math.min(c, d)
}
}
`);
expect({ _source: source, expected: transform(source) }).toMatchSnapshot();
});
it("should minify builtins to function scopes ", () => {
const source = unpad(`
var a = () => {
Math.floor(b);
Math.floor(b);
c: () => {
Math.floor(d);
Math.max(2,1);
}
}
A.b("asdas", function() {
Math.floor(d) + Math.max(d,e);
Math.max(e,d);
})
A.b("asdas1", function() {
Math.floor(d) + Math.floor(d,e);
Math.max(e,d);
})
`);
expect({ _source: source, expected: transform(source) }).toMatchSnapshot();
});
it("should collect and minify in segments in any depth if there is no LCA", () => {
const source = unpad(`
function b(){
Math.floor(as, bb);
function d(){
Math.floor(as, bb);
}
}
const a = {
c : () => Math.floor(bbb) + Math.floor(bbb) ,
d : () => {
Math.abs(aa);
Math.abs(aa);
Math.floor(aa);
return () => {
Math.floor(aa);
}
},
e : () => Math.abs(aa) + Math.abs(aa)
};
class A {
constructor() {
let a = Math.floor(b,c) + Math.floor(b,c);
}
c() {
Math.floor(asdas);
Math.floor(dasda);
}
d() {
var a = Math.floor;
a(aa, bb);
Math.floor(aa, bb);
}
};
new A()
`);
expect({ _source: source, expected: transform(source) }).toMatchSnapshot();
});
it("should evalaute expressions if applicable and optimize it", () => {
const source = unpad(`
const a = Math.max(Math.floor(2), 5);
let b = 1.8;
let x = Math.floor(Math.max(a, b));
foo(x);
`);
expect({ _source: source, expected: transform(source) }).toMatchSnapshot();
});
it("should not evaluate if its side effecty", () => {
const source = unpad(`
Math.max(foo(), 1);
Math.random();
`);
expect({ _source: source, expected: transform(source) }).toMatchSnapshot();
});
it("should not minify for computed properties", () => {
const source = unpad(`
let max = "floor";
Math[max](1.5);
`);
expect({ _source: source, expected: transform(source) }).toMatchSnapshot();
});
it("should not minify for arrow fn without block statment", () => {
const source = unpad(`
const a = () => Math.floor(b) + Math.floor(b);
`);
expect({ _source: source, expected: transform(source) }).toMatchSnapshot();
});
});