MCPcopy
hub / github.com/tdewolff/minify / minifyStmt

Method minifyStmt

js/js.go:130–413  ·  view source on GitHub ↗
(i js.IStmt)

Source from the content-addressed store, hash-verified

128}
129
130func (m *jsMinifier) minifyStmt(i js.IStmt) {
131 switch stmt := i.(type) {
132 case *js.ExprStmt:
133 m.expectExpr = expectExprStmt
134 m.minifyExpr(stmt.Value, js.OpExpr)
135 if m.groupedStmt {
136 m.write(closeParenBytes)
137 m.groupedStmt = false
138 }
139 m.requireSemicolon()
140 case *js.VarDecl:
141 m.minifyVarDecl(stmt, false)
142 m.requireSemicolon()
143 case *js.IfStmt:
144 hasIf := !isEmptyStmt(stmt.Body)
145 hasElse := !isEmptyStmt(stmt.Else)
146 if !hasIf && !hasElse {
147 break
148 }
149
150 m.write(ifOpenBytes)
151 m.minifyExpr(stmt.Cond, js.OpExpr)
152 m.write(closeParenBytes)
153
154 if !hasIf && hasElse {
155 m.requireSemicolon()
156 } else if hasIf {
157 if hasElse && endsInIf(stmt.Body) {
158 // prevent: if(a){if(b)c}else d; => if(a)if(b)c;else d;
159 m.write(openBraceBytes)
160 m.minifyStmt(stmt.Body)
161 m.write(closeBraceBytes)
162 m.needsSemicolon = false
163 } else {
164 m.minifyStmt(stmt.Body)
165 }
166 }
167 if hasElse {
168 m.writeSemicolon()
169 m.write(elseBytes)
170 m.writeSpaceBeforeIdent()
171 m.minifyStmt(stmt.Else)
172 }
173 case *js.BlockStmt:
174 m.renamer.renameScope(stmt.Scope)
175 m.minifyBlockStmt(stmt)
176 case *js.ReturnStmt:
177 m.write(returnBytes)
178 m.writeSpaceBeforeIdent()
179 m.minifyExpr(stmt.Value, js.OpExpr)
180 m.requireSemicolon()
181 case *js.LabelledStmt:
182 m.write(stmt.Label)
183 m.write(colonBytes)
184 m.minifyStmtOrBlock(stmt.Value, defaultBlock)
185 case *js.BranchStmt:
186 m.write(stmt.Type.Bytes())
187 if stmt.Label != nil {

Callers 4

MinifyMethod · 0.95
minifyBlockStmtMethod · 0.95
minifyBlockAsStmtMethod · 0.95
minifyStmtOrBlockMethod · 0.95

Calls 15

minifyExprMethod · 0.95
writeMethod · 0.95
requireSemicolonMethod · 0.95
minifyVarDeclMethod · 0.95
writeSemicolonMethod · 0.95
writeSpaceBeforeIdentMethod · 0.95
minifyBlockStmtMethod · 0.95
minifyStmtOrBlockMethod · 0.95
minifyBlockAsStmtMethod · 0.95
writeSpaceAfterIdentMethod · 0.95
minifyBindingMethod · 0.95
minifyFuncDeclMethod · 0.95

Tested by

no test coverage detected