Skip to content

Commit

Permalink
Fix For Loop Initialization
Browse files Browse the repository at this point in the history
Fix formatting of for loops that don't have all three
init/expression/update clauses.
  • Loading branch information
cwarden committed Dec 6, 2023
1 parent 7425ead commit 386610e
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions formatter/visitors.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,17 +335,19 @@ func (v *Visitor) VisitForControl(ctx *parser.ForControlContext) interface{} {
if enhancedForControl := ctx.EnhancedForControl(); enhancedForControl != nil {
return v.visitRule(enhancedForControl)
}
parts := []string{}
var init strings.Builder
if forInit := ctx.ForInit(); forInit != nil {
parts = append(parts, v.visitRule(forInit).(string))
init.WriteString(v.visitRule(forInit).(string))
}
init.WriteString(";")
if expression := ctx.Expression(); expression != nil {
parts = append(parts, v.visitRule(expression).(string))
init.WriteString(fmt.Sprintf(" %s", v.visitRule(expression).(string)))
}
init.WriteString(";")
if forUpdate := ctx.ForUpdate(); forUpdate != nil {
parts = append(parts, v.visitRule(forUpdate).(string))
init.WriteString(fmt.Sprintf(" %s", v.visitRule(forUpdate).(string)))
}
return strings.Join(parts, "; ")
return init.String()
}

func (v *Visitor) VisitEnhancedForControl(ctx *parser.EnhancedForControlContext) interface{} {
Expand Down

0 comments on commit 386610e

Please sign in to comment.