Repository navigation
Expand file tree
/
Copy pathstmtlist.go
More file actions
238 lines (231 loc) · 9.53 KB
/
Copy pathstmtlist.go
File metadata and controls
238 lines (231 loc) · 9.53 KB
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
package js
import (
"github.com/tdewolff/parse/v2/js"
)
func (m *jsMinifier) optimizeStmt(i js.IStmt) js.IStmt {
// convert if/else into expression statement, and optimize blocks
if ifStmt, ok := i.(*js.IfStmt); ok {
hasIf := !m.isEmptyStmt(ifStmt.Body)
hasElse := !m.isEmptyStmt(ifStmt.Else)
if unaryExpr, ok := ifStmt.Cond.(*js.UnaryExpr); ok && unaryExpr.Op == js.NotToken && hasElse {
ifStmt.Cond = unaryExpr.X
ifStmt.Body, ifStmt.Else = ifStmt.Else, ifStmt.Body
hasIf, hasElse = hasElse, hasIf
}
if !hasIf && !hasElse {
return &js.ExprStmt{ifStmt.Cond}
} else if hasIf && !hasElse {
ifStmt.Body = m.optimizeStmt(ifStmt.Body)
if X, isExprBody := ifStmt.Body.(*js.ExprStmt); isExprBody {
if unaryExpr, ok := ifStmt.Cond.(*js.UnaryExpr); ok && unaryExpr.Op == js.NotToken {
left := groupExpr(unaryExpr.X, binaryLeftPrecMap[js.OrToken])
right := groupExpr(X.Value, binaryRightPrecMap[js.OrToken])
return &js.ExprStmt{&js.BinaryExpr{js.OrToken, left, right}}
} else {
left := groupExpr(ifStmt.Cond, binaryLeftPrecMap[js.AndToken])
right := groupExpr(X.Value, binaryRightPrecMap[js.AndToken])
return &js.ExprStmt{&js.BinaryExpr{js.AndToken, left, right}}
}
}
} else if !hasIf && hasElse {
ifStmt.Else = m.optimizeStmt(ifStmt.Else)
if X, isExprElse := ifStmt.Else.(*js.ExprStmt); isExprElse {
left := groupExpr(ifStmt.Cond, binaryLeftPrecMap[js.OrToken])
right := groupExpr(X.Value, binaryRightPrecMap[js.OrToken])
return &js.ExprStmt{&js.BinaryExpr{js.OrToken, left, right}}
}
} else if hasIf && hasElse {
ifStmt.Body = m.optimizeStmt(ifStmt.Body)
if endsInIf(ifStmt.Body) {
ifStmt.Body = &js.BlockStmt{[]js.IStmt{ifStmt.Body}, js.Scope{}}
}
ifStmt.Else = m.optimizeStmt(ifStmt.Else)
XExpr, isExprBody := ifStmt.Body.(*js.ExprStmt)
YExpr, isExprElse := ifStmt.Else.(*js.ExprStmt)
if isExprBody && isExprElse {
return &js.ExprStmt{condExpr(ifStmt.Cond, XExpr.Value, YExpr.Value)}
}
XReturn, isReturnBody := ifStmt.Body.(*js.ReturnStmt)
YReturn, isReturnElse := ifStmt.Else.(*js.ReturnStmt)
if isReturnBody && isReturnElse {
if XReturn.Value == nil && YReturn.Value == nil {
return &js.ReturnStmt{&js.BinaryExpr{js.CommaToken, ifStmt.Cond, &js.UnaryExpr{js.VoidToken, &js.LiteralExpr{js.NumericToken, zeroBytes}}}}
} else if XReturn.Value != nil && YReturn.Value != nil {
return &js.ReturnStmt{condExpr(ifStmt.Cond, XReturn.Value, YReturn.Value)}
}
return ifStmt
}
XThrow, isThrowBody := ifStmt.Body.(*js.ThrowStmt)
YThrow, isThrowElse := ifStmt.Else.(*js.ThrowStmt)
if isThrowBody && isThrowElse {
return &js.ThrowStmt{condExpr(ifStmt.Cond, XThrow.Value, YThrow.Value)}
}
}
} else if decl, ok := i.(*js.VarDecl); ok && m.varsHoisted != nil && decl != m.varsHoisted {
// convert hoisted var declaration to expression of empty (if there are no defines) statement
for _, item := range decl.List {
if item.Default != nil {
return &js.ExprStmt{decl}
}
}
return &js.EmptyStmt{}
} else if blockStmt, ok := i.(*js.BlockStmt); ok {
// merge body and remove braces if it is not a lexical declaration
blockStmt.List = m.optimizeStmtList(blockStmt.List, defaultBlock)
if len(blockStmt.List) == 1 {
varDecl, isVarDecl := blockStmt.List[0].(*js.VarDecl)
_, isClassDecl := blockStmt.List[0].(*js.ClassDecl)
if !isClassDecl && (!isVarDecl || varDecl.TokenType == js.VarToken) {
return m.optimizeStmt(blockStmt.List[0])
}
} else if len(blockStmt.List) == 0 {
return &js.EmptyStmt{}
}
return blockStmt
}
return i
}
func (m *jsMinifier) optimizeStmtList(list []js.IStmt, blockType blockType) []js.IStmt {
// merge expression statements as well as if/else statements followed by flow control statements
if len(list) == 0 {
return list
}
j := 0 // write index
for i := 0; i < len(list); i++ { // read index
list[i] = m.optimizeStmt(list[i])
if ifStmt, ok := list[i].(*js.IfStmt); ok && !m.isEmptyStmt(ifStmt.Else) && isFlowStmt(lastStmt(ifStmt.Body)) {
// if body ends in flow statement (return, throw, break, continue), so we can remove the else statement and put its body in the current scope
if blockStmt, ok := ifStmt.Else.(*js.BlockStmt); ok {
list = append(append(list[:i+1], blockStmt.List...), list[i+1:]...)
} else {
list = append(append(list[:i+1], ifStmt.Else), list[i+1:]...)
}
ifStmt.Else = nil
} else if _, ok := list[i].(*js.EmptyStmt); ok {
continue
}
if 0 < i {
// merge expression statements with expression, return, and throw statements
if left, ok := list[i-1].(*js.ExprStmt); ok {
if right, ok := list[i].(*js.ExprStmt); ok {
right.Value = &js.BinaryExpr{js.CommaToken, left.Value, right.Value}
j--
} else if returnStmt, ok := list[i].(*js.ReturnStmt); ok && returnStmt.Value != nil {
returnStmt.Value = &js.BinaryExpr{js.CommaToken, left.Value, returnStmt.Value}
j--
} else if throwStmt, ok := list[i].(*js.ThrowStmt); ok {
throwStmt.Value = &js.BinaryExpr{js.CommaToken, left.Value, throwStmt.Value}
j--
} else if forStmt, ok := list[i].(*js.ForStmt); ok && forStmt.Init == nil {
// TODO: only merge statements that don't have 'in' or 'of' keywords (slow to check?)
forStmt.Init = left.Value
j--
} else if whileStmt, ok := list[i].(*js.WhileStmt); ok {
// TODO: only merge statements that don't have 'in' or 'of' keywords (slow to check?)
var body js.BlockStmt
if blockStmt, ok := whileStmt.Body.(*js.BlockStmt); ok {
body = *blockStmt
} else {
body.List = []js.IStmt{whileStmt.Body}
}
list[i] = &js.ForStmt{left.Value, whileStmt.Cond, nil, body}
j--
} else if switchStmt, ok := list[i].(*js.SwitchStmt); ok {
switchStmt.Init = &js.BinaryExpr{js.CommaToken, left.Value, switchStmt.Init}
j--
} else if withStmt, ok := list[i].(*js.WithStmt); ok {
withStmt.Cond = &js.BinaryExpr{js.CommaToken, left.Value, withStmt.Cond}
j--
} else if ifStmt, ok := list[i].(*js.IfStmt); ok {
ifStmt.Cond = &js.BinaryExpr{js.CommaToken, left.Value, ifStmt.Cond}
j--
}
} else if left, ok := list[i-1].(*js.VarDecl); ok {
if right, ok := list[i].(*js.VarDecl); ok && left.TokenType == right.TokenType {
// merge const and let declarations
right.List = append(left.List, right.List...)
j--
} else if forStmt, ok := list[i].(*js.ForStmt); ok && left.TokenType == js.VarToken {
// TODO: only merge statements that don't have 'in' or 'of' keywords (slow to check?)
if forStmt.Init == nil {
forStmt.Init = left
j--
} else {
// TODO: merge with expressions if they define the declarations
}
} else if whileStmt, ok := list[i].(*js.WhileStmt); ok && left.TokenType == js.VarToken {
// TODO: only merge statements that don't have 'in' or 'of' keywords (slow to check?)
var body js.BlockStmt
if blockStmt, ok := whileStmt.Body.(*js.BlockStmt); ok {
body = *blockStmt
} else {
body.List = []js.IStmt{whileStmt.Body}
}
list[i] = &js.ForStmt{left, whileStmt.Cond, nil, body}
j--
}
}
}
list[j] = list[i]
// merge if/else with return/throw when followed by return/throw
MergeIfReturnThrow:
if 0 < j {
// separate from expression merging in case of: if(a)return b;b=c;return d
if ifStmt, ok := list[j-1].(*js.IfStmt); ok && m.isEmptyStmt(ifStmt.Body) != m.isEmptyStmt(ifStmt.Else) {
// either the if body is empty or the else body is empty. In case where both bodies have return/throw, we already rewrote that if statement to an return/throw statement
if returnStmt, ok := list[j].(*js.ReturnStmt); ok {
if returnStmt.Value == nil {
if left, ok := ifStmt.Body.(*js.ReturnStmt); ok && left.Value == nil {
list[j-1] = &js.ExprStmt{ifStmt.Cond}
} else if left, ok := ifStmt.Else.(*js.ReturnStmt); ok && left.Value == nil {
list[j-1] = &js.ExprStmt{ifStmt.Cond}
}
} else {
if left, ok := ifStmt.Body.(*js.ReturnStmt); ok && left.Value != nil {
returnStmt.Value = condExpr(ifStmt.Cond, left.Value, returnStmt.Value)
list[j-1] = returnStmt
j--
goto MergeIfReturnThrow
} else if left, ok := ifStmt.Else.(*js.ReturnStmt); ok && left.Value != nil {
returnStmt.Value = condExpr(ifStmt.Cond, returnStmt.Value, left.Value)
list[j-1] = returnStmt
j--
goto MergeIfReturnThrow
}
}
} else if throwStmt, ok := list[j].(*js.ThrowStmt); ok {
if left, ok := ifStmt.Body.(*js.ThrowStmt); ok {
throwStmt.Value = condExpr(ifStmt.Cond, left.Value, throwStmt.Value)
list[j-1] = throwStmt
j--
goto MergeIfReturnThrow
} else if left, ok := ifStmt.Else.(*js.ThrowStmt); ok {
throwStmt.Value = condExpr(ifStmt.Cond, throwStmt.Value, left.Value)
list[j-1] = throwStmt
j--
goto MergeIfReturnThrow
}
}
}
}
j++
}
// remove superfluous return or continue
if 0 < j {
if blockType == functionBlock {
if returnStmt, ok := list[j-1].(*js.ReturnStmt); ok {
if returnStmt.Value == nil || m.isUndefined(returnStmt.Value) {
j--
} else if binaryExpr, ok := returnStmt.Value.(*js.BinaryExpr); ok && binaryExpr.Op == js.CommaToken && m.isUndefined(binaryExpr.Y) {
// rewrite function f(){return a,void 0} => function f(){a}
list[j-1] = &js.ExprStmt{binaryExpr.X}
}
}
} else if blockType == iterationBlock {
if branchStmt, ok := list[j-1].(*js.BranchStmt); ok && branchStmt.Type == js.ContinueToken && branchStmt.Label == nil {
j--
}
}
}
return list[:j]
}