-
-
Notifications
You must be signed in to change notification settings - Fork 211
/
vars.go
453 lines (422 loc) · 12.3 KB
/
vars.go
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package js
import (
"bytes"
"sort"
"github.com/tdewolff/parse/v2/js"
)
const identStartLen = 54
const identContinueLen = 64
type renamer struct {
identStart []byte
identContinue []byte
identOrder map[byte]int
reserved map[string]struct{}
rename bool
}
func newRenamer(rename, useCharFreq bool) *renamer {
reserved := make(map[string]struct{}, len(js.Keywords))
for name := range js.Keywords {
reserved[name] = struct{}{}
}
identStart := []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$")
identContinue := []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$0123456789")
if useCharFreq {
// sorted based on character frequency of a collection of JS samples
identStart = []byte("etnsoiarclduhmfpgvbjy_wOxCEkASMFTzDNLRPHIBV$WUKqYGXQZJ")
identContinue = []byte("etnsoiarcldu14023hm8f6pg57v9bjy_wOxCEkASMFTzDNLRPHIBV$WUKqYGXQZJ")
}
if len(identStart) != identStartLen || len(identContinue) != identContinueLen {
panic("bad identStart or identContinue lengths")
}
identOrder := map[byte]int{}
for i, c := range identStart {
identOrder[c] = i
}
return &renamer{
identStart: identStart,
identContinue: identContinue,
identOrder: identOrder,
reserved: reserved,
rename: rename,
}
}
func (r *renamer) renameScope(scope js.Scope) {
if !r.rename {
return
}
i := 0
// keep function argument declaration order to improve GZIP compression
sort.Sort(js.VarsByUses(scope.Declared[scope.NumFuncArgs:]))
for _, v := range scope.Declared {
v.Data = r.getName(v.Data, i)
i++
for r.isReserved(v.Data, scope.Undeclared) {
v.Data = r.getName(v.Data, i)
i++
}
}
}
func (r *renamer) isReserved(name []byte, undeclared js.VarArray) bool {
if 1 < len(name) { // there are no keywords or known globals that are one character long
if _, ok := r.reserved[string(name)]; ok {
return true
}
}
for _, v := range undeclared {
for v.Link != nil {
v = v.Link
}
if bytes.Equal(v.Data, name) {
return true
}
}
return false
}
func (r *renamer) getIndex(name []byte) int {
index := 0
NameLoop:
for i := len(name) - 1; 0 <= i; i-- {
chars := r.identContinue
if i == 0 {
chars = r.identStart
index *= identStartLen
} else {
index *= identContinueLen
}
for j, c := range chars {
if name[i] == c {
index += j
continue NameLoop
}
}
return -1
}
for n := 0; n < len(name)-1; n++ {
offset := identStartLen
for i := 0; i < n; i++ {
offset *= identContinueLen
}
index += offset
}
return index
}
func (r *renamer) getName(name []byte, index int) []byte {
// Generate new names for variables where the last character is (a-zA-Z$_) and others are (a-zA-Z).
// Thus we can have 54 one-character names and 52*54=2808 two-character names for every branch leaf.
// That is sufficient for virtually all input.
// one character
if index < identStartLen {
name[0] = r.identStart[index]
return name[:1]
}
index -= identStartLen
// two characters or more
n := 2
for {
offset := identStartLen
for i := 0; i < n-1; i++ {
offset *= identContinueLen
}
if index < offset {
break
}
index -= offset
n++
}
if cap(name) < n {
name = make([]byte, n)
} else {
name = name[:n]
}
name[0] = r.identStart[index%identStartLen]
index /= identStartLen
for i := 1; i < n; i++ {
name[i] = r.identContinue[index%identContinueLen]
index /= identContinueLen
}
return name
}
////////////////////////////////////////////////////////////////
func hasDefines(v *js.VarDecl) bool {
for _, item := range v.List {
if item.Default != nil {
return true
}
}
return false
}
func bindingVars(ibinding js.IBinding) (vs []*js.Var) {
switch binding := ibinding.(type) {
case *js.Var:
vs = append(vs, binding)
case *js.BindingArray:
for _, item := range binding.List {
if item.Binding != nil {
vs = append(vs, bindingVars(item.Binding)...)
}
}
if binding.Rest != nil {
vs = append(vs, bindingVars(binding.Rest)...)
}
case *js.BindingObject:
for _, item := range binding.List {
if item.Value.Binding != nil {
vs = append(vs, bindingVars(item.Value.Binding)...)
}
}
if binding.Rest != nil {
vs = append(vs, binding.Rest)
}
}
return
}
func addDefinition(decl *js.VarDecl, binding js.IBinding, value js.IExpr, forward bool) {
if decl.TokenType != js.ErrorToken {
// see if not already defined in variable declaration list
// if forward is set, binding=value comes before decl, otherwise the reverse holds true
vars := bindingVars(binding)
// remove variables in destination
RemoveVarsLoop:
for _, vbind := range vars {
for i, item := range decl.List {
if v, ok := item.Binding.(*js.Var); ok && item.Default == nil && v == vbind {
v.Uses--
decl.List = append(decl.List[:i], decl.List[i+1:]...)
continue RemoveVarsLoop
}
}
if value != nil {
// variable declaration must be somewhere else, find and remove it
for _, decl2 := range decl.Scope.Func.VarDecls {
if !decl2.InForInOf {
for i, item := range decl2.List {
if v, ok := item.Binding.(*js.Var); ok && item.Default == nil && v == vbind {
v.Uses--
decl2.List = append(decl2.List[:i], decl2.List[i+1:]...)
continue RemoveVarsLoop
}
}
}
}
}
}
}
// add declaration to destination
item := js.BindingElement{Binding: binding, Default: value}
if forward {
decl.List = append([]js.BindingElement{item}, decl.List...)
} else {
decl.List = append(decl.List, item)
}
}
func mergeVarDecls(dst, src *js.VarDecl, forward bool) {
// Merge var declarations by moving declarations from src to dst. If forward is set, src comes first and dst after, otherwise the order is reverse.
if forward {
// reverse order so we can iterate from beginning to end, sometimes addDefinition may remove another declaration in the src list
n := len(src.List) - 1
for j := 0; j < len(src.List)/2; j++ {
src.List[j], src.List[n-j] = src.List[n-j], src.List[j]
}
}
for j := 0; j < len(src.List); j++ {
addDefinition(dst, src.List[j].Binding, src.List[j].Default, forward)
}
src.List = src.List[:0]
}
func mergeVarDeclExprStmt(decl *js.VarDecl, exprStmt *js.ExprStmt, forward bool) bool {
// Merge var declarations with an assignment expression. If forward is set than expr comes first and decl after, otherwise the order is reverse.
if decl2, ok := exprStmt.Value.(*js.VarDecl); ok {
// this happens when a variable declarations is converted to an expression due to hoisting
mergeVarDecls(decl, decl2, forward)
return true
} else if commaExpr, ok := exprStmt.Value.(*js.CommaExpr); ok {
n := 0
for i := 0; i < len(commaExpr.List); i++ {
item := commaExpr.List[i]
if forward {
item = commaExpr.List[len(commaExpr.List)-i-1]
}
if src, ok := item.(*js.VarDecl); ok {
// this happens when a variable declarations is converted to an expression due to hoisting
mergeVarDecls(decl, src, forward)
n++
continue
} else if binaryExpr, ok := item.(*js.BinaryExpr); ok && binaryExpr.Op == js.EqToken {
if v, ok := binaryExpr.X.(*js.Var); ok && v.Decl == js.VariableDecl {
addDefinition(decl, v, binaryExpr.Y, forward)
n++
continue
}
}
break
}
merge := n == len(commaExpr.List)
if !forward {
commaExpr.List = commaExpr.List[n:]
} else {
commaExpr.List = commaExpr.List[:len(commaExpr.List)-n]
}
return merge
} else if binaryExpr, ok := exprStmt.Value.(*js.BinaryExpr); ok && binaryExpr.Op == js.EqToken {
if v, ok := binaryExpr.X.(*js.Var); ok && v.Decl == js.VariableDecl {
addDefinition(decl, v, binaryExpr.Y, forward)
return true
}
}
return false
}
func (m *jsMinifier) countHoistLength(ibinding js.IBinding) int {
if !m.o.KeepVarNames {
return len(bindingVars(ibinding)) * 2 // assume that var name will be of length one, +1 for the comma
}
n := 0
for _, v := range bindingVars(ibinding) {
n += len(v.Data) + 1 // +1 for the comma when added to other declaration
}
return n
}
func (m *jsMinifier) hoistVars(body *js.BlockStmt) {
// Hoist all variable declarations in the current module/function scope to the variable
// declaration that reduces file size the most. All other declarations are converted to
// expressions and their variable names are copied to the only remaining declaration.
// This is possible because an ArrayBindingPattern and ObjectBindingPattern can be converted to
// an ArrayLiteral or ObjectLiteral respectively, as they are supersets of the BindingPatterns.
if 1 < len(body.Scope.VarDecls) {
// Select which variable declarations will be hoisted (convert to expression) and which not
best := 0
scores := make([]int, len(body.Scope.VarDecls)) // savings if hoisting target
hoist := make([]bool, len(body.Scope.VarDecls))
for i, varDecl := range body.Scope.VarDecls {
hoist[i] = true
if varDecl.InForInOf {
continue
}
// variable names in for-in or for-of cannot be removed
n := 0 // total number of vars with decls
score := 3 // "var"
nArrays := 0 // of which lhs arrays
nObjects := 0 // of which lhs objects
hasDefinitions := false
for j, item := range varDecl.List {
if item.Default != nil {
// move arrays/objects to the front (saves a space)
if _, ok := item.Binding.(*js.BindingObject); ok {
if j != 0 && nArrays == 0 && nObjects == 0 {
varDecl.List[0], varDecl.List[j] = varDecl.List[j], varDecl.List[0]
}
nObjects++
} else if _, ok := item.Binding.(*js.BindingArray); ok {
if j != 0 && nArrays == 0 && nObjects == 0 {
varDecl.List[0], varDecl.List[j] = varDecl.List[j], varDecl.List[0]
}
nArrays++
}
score -= m.countHoistLength(item.Binding) // var names and commas
hasDefinitions = true
n++
}
}
if nArrays == 0 && nObjects == 0 {
score++ // required space after var
}
if !hasDefinitions && varDecl.InFor {
score-- // semicolon can be reused
}
if nObjects != 0 && !varDecl.InFor && nObjects == n {
// required parenthesis around braces to not confound it with a block statement
score -= 2
}
if score < scores[best] || body.Scope.VarDecls[best].InForInOf {
// select var decl that reduces the least when hoist target
best = i
}
if score < 0 {
// don't hoist if it increases the amount of characters
hoist[i] = false
}
scores[i] = score
}
if body.Scope.VarDecls[best].InForInOf {
// no savings possible
return
}
decl := body.Scope.VarDecls[best]
if 10000 < len(decl.List) {
return
}
hoist[best] = false
// get original declarations
orig := []*js.Var{}
for _, item := range decl.List {
orig = append(orig, bindingVars(item.Binding)...)
}
// hoist other variable declarations in this function scope but don't initialize yet
j := 0
for i, varDecl := range body.Scope.VarDecls {
if hoist[i] {
varDecl.TokenType = js.ErrorToken
for _, item := range varDecl.List {
refs := bindingVars(item.Binding)
bindingElements := make([]js.BindingElement, 0, len(refs))
DeclaredLoop:
for _, ref := range refs {
for _, v := range orig {
if ref == v {
continue DeclaredLoop
}
}
bindingElements = append(bindingElements, js.BindingElement{Binding: ref, Default: nil})
orig = append(orig, ref)
s := decl.Scope
for s != nil && s != s.Func {
s.AddUndeclared(ref)
s = s.Parent
}
if item.Default != nil {
ref.Uses++
}
}
if i < best {
// prepend
decl.List = append(decl.List[:j], append(bindingElements, decl.List[j:]...)...)
j += len(bindingElements)
} else {
// append
decl.List = append(decl.List, bindingElements...)
}
}
}
}
// rearrange to put array/object first
var prevRefs []*js.Var
BeginArrayObject:
for i, item := range decl.List {
refs := bindingVars(item.Binding)
if _, ok := item.Binding.(*js.Var); !ok {
if i != 0 {
interferes := false
if item.Default != nil {
InterferenceLoop:
for _, ref := range refs {
for _, v := range prevRefs {
if ref == v {
interferes = true
break InterferenceLoop
}
}
}
}
if !interferes {
decl.List[0], decl.List[i] = decl.List[i], decl.List[0]
break BeginArrayObject
}
} else {
break BeginArrayObject
}
}
if item.Default != nil {
prevRefs = append(prevRefs, refs...)
}
}
}
}