diff --git a/format/format.go b/format/format.go index 0f3a7faba4..1ce1d6dab5 100644 --- a/format/format.go +++ b/format/format.go @@ -308,6 +308,12 @@ func (w *writer) writeElse(rule *ast.Rule, comments []*ast.Comment) []*ast.Comme rule.Else.Head.Args = nil comments = w.insertComments(comments, rule.Else.Head.Location) + if hasCommentAbove && !wasInline { + // The comments would have ended the line, be sure to start one again + // before writing the rest of the "else" rule. + w.startLine() + } + // For backwards compatibility adjust the rule head value location // TODO: Refactor the logic for inserting comments, or special // case comments in a rule head value so this can be removed @@ -1005,9 +1011,6 @@ func dedupComments(comments []*ast.Comment) []*ast.Comment { // startLine begins a line with the current indentation level. func (w *writer) startLine() { - if w.inline { - panic("currently in a line") - } w.inline = true for i := 0; i < w.level; i++ { w.write(w.indent) @@ -1016,9 +1019,6 @@ func (w *writer) startLine() { // endLine ends a line with a newline. func (w *writer) endLine() { - if !w.inline { - panic("not in a line") - } w.inline = false if w.beforeEnd != nil && !w.delay { w.write(" " + w.beforeEnd.String()) diff --git a/format/testfiles/test_issue_2420.rego b/format/testfiles/test_issue_2420.rego new file mode 100644 index 0000000000..f5bab2c0e6 --- /dev/null +++ b/format/testfiles/test_issue_2420.rego @@ -0,0 +1,11 @@ +package example + +allow { + some_condition +} + +# some comments + +else { + another_condition +} diff --git a/format/testfiles/test_issue_2420.rego.formatted b/format/testfiles/test_issue_2420.rego.formatted new file mode 100644 index 0000000000..4684a00ab2 --- /dev/null +++ b/format/testfiles/test_issue_2420.rego.formatted @@ -0,0 +1,11 @@ +package example + +allow { + some_condition +} + +# some comments + +else { + another_condition +}