Skip to content

Commit d28f450

Browse files
committed
chore: factorize formatStmts and formatExprs
1 parent fe0206e commit d28f450

File tree

1 file changed

+27
-32
lines changed

1 file changed

+27
-32
lines changed

shorten/format.go

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,6 @@ func (s *Shortener) formatDecl(decl dst.Decl) {
6565
}
6666
}
6767

68-
// formatFieldList formats a field list in a function declaration.
69-
func (s *Shortener) formatFieldList(fieldList *dst.FieldList) {
70-
for i, field := range fieldList.List {
71-
formatList(field, i)
72-
}
73-
}
74-
7568
// formatStmt formats an AST statement node.
7669
// Among other examples, these include assignments, case clauses,
7770
// for statements, if statements, and select statements.
@@ -87,14 +80,10 @@ func (s *Shortener) formatStmt(stmt dst.Stmt, force bool) {
8780

8881
switch st := stmt.(type) {
8982
case *dst.AssignStmt:
90-
for _, expr := range st.Rhs {
91-
s.formatExpr(expr, shouldShorten, false)
92-
}
83+
s.formatExprs(st.Rhs, shouldShorten, false)
9384

9485
case *dst.BlockStmt:
95-
for _, stmt := range st.List {
96-
s.formatStmt(stmt, false)
97-
}
86+
s.formatStmts(st.List, false)
9887

9988
case *dst.CaseClause:
10089
if shouldShorten {
@@ -105,14 +94,10 @@ func (s *Shortener) formatStmt(stmt dst.Stmt, force bool) {
10594
}
10695
}
10796

108-
for _, stmt := range st.Body {
109-
s.formatStmt(stmt, false)
110-
}
97+
s.formatStmts(st.Body, false)
11198

11299
case *dst.CommClause:
113-
for _, stmt := range st.Body {
114-
s.formatStmt(stmt, false)
115-
}
100+
s.formatStmts(st.Body, false)
116101

117102
case *dst.DeclStmt:
118103
s.formatDecl(st.Decl)
@@ -145,9 +130,7 @@ func (s *Shortener) formatStmt(stmt dst.Stmt, force bool) {
145130
s.formatStmt(st.Body, false)
146131

147132
case *dst.ReturnStmt:
148-
for _, expr := range st.Results {
149-
s.formatExpr(expr, shouldShorten, false)
150-
}
133+
s.formatExprs(st.Results, shouldShorten, false)
151134

152135
case *dst.SelectStmt:
153136
s.formatStmt(st.Body, false)
@@ -193,10 +176,7 @@ func (s *Shortener) formatExpr(expr dst.Expr, force, isChain bool) {
193176
s.config.ChainSplitDots && (isChain || chainLength(e) > 1) {
194177
e.Decorations().After = dst.NewLine
195178

196-
for _, arg := range e.Args {
197-
s.formatExpr(arg, false, true)
198-
}
199-
179+
s.formatExprs(e.Args, false, true)
200180
s.formatExpr(e.Fun, shouldShorten, true)
201181
} else {
202182
for i, arg := range e.Args {
@@ -221,9 +201,7 @@ func (s *Shortener) formatExpr(expr dst.Expr, force, isChain bool) {
221201
}
222202
}
223203

224-
for _, element := range e.Elts {
225-
s.formatExpr(element, false, isChain)
226-
}
204+
s.formatExprs(e.Elts, false, isChain)
227205

228206
case *dst.FuncLit:
229207
s.formatStmt(e.Body, false)
@@ -271,9 +249,7 @@ func (s *Shortener) formatSpec(spec dst.Spec, force bool) {
271249

272250
switch sp := spec.(type) {
273251
case *dst.ValueSpec:
274-
for _, expr := range sp.Values {
275-
s.formatExpr(expr, shouldShorten, false)
276-
}
252+
s.formatExprs(sp.Values, shouldShorten, false)
277253

278254
case *dst.TypeSpec:
279255
s.formatExpr(sp.Type, false, false)
@@ -288,6 +264,25 @@ func (s *Shortener) formatSpec(spec dst.Spec, force bool) {
288264
}
289265
}
290266

267+
func (s *Shortener) formatStmts(stmts []dst.Stmt, force bool) {
268+
for _, stmt := range stmts {
269+
s.formatStmt(stmt, force)
270+
}
271+
}
272+
273+
func (s *Shortener) formatExprs(exprs []dst.Expr, force, isChain bool) {
274+
for _, expr := range exprs {
275+
s.formatExpr(expr, force, isChain)
276+
}
277+
}
278+
279+
// formatFieldList formats a field list in a function declaration.
280+
func (s *Shortener) formatFieldList(fieldList *dst.FieldList) {
281+
for i, field := range fieldList.List {
282+
formatList(field, i)
283+
}
284+
}
285+
291286
func formatList(node dst.Node, index int) {
292287
decorations := node.Decorations()
293288

0 commit comments

Comments
 (0)