Skip to content

Commit

Permalink
format: keep whitespaces for multiple indented same-line withs
Browse files Browse the repository at this point in the history
Fixes #4634.

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
  • Loading branch information
srenatus committed Apr 28, 2022
1 parent 7e50293 commit 4433fae
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
6 changes: 4 additions & 2 deletions format/format.go
Expand Up @@ -455,7 +455,9 @@ func (w *writer) writeExpr(expr *ast.Expr, comments []*ast.Comment) []*ast.Comme

var indented bool
for i, with := range expr.With {
if i > 0 && with.Location.Row-expr.With[i-1].Location.Row > 0 {
if i == 0 || with.Location.Row == expr.With[i-1].Location.Row { // we're on the same line
comments = w.writeWith(with, comments, false)
} else { // we're on a new line
if !indented {
indented = true

Expand All @@ -464,8 +466,8 @@ func (w *writer) writeExpr(expr *ast.Expr, comments []*ast.Comment) []*ast.Comme
}
w.endLine()
w.startLine()
comments = w.writeWith(with, comments, true)
}
comments = w.writeWith(with, comments, indented)
}

return comments
Expand Down
6 changes: 6 additions & 0 deletions format/testfiles/test_with.rego
Expand Up @@ -16,6 +16,12 @@ multi_line_with {
3]
}

mixed_new_lines_with {
true with input.a as "a"
with input.b as "b" with input.c as "c"
with input.d as "d"
}

mock_f(_) = 123

func_replacements {
Expand Down
6 changes: 6 additions & 0 deletions format/testfiles/test_with.rego.formatted
Expand Up @@ -15,6 +15,12 @@ multi_line_with {
]
}

mixed_new_lines_with {
true with input.a as "a"
with input.b as "b" with input.c as "c"
with input.d as "d"
}

mock_f(_) = 123

func_replacements {
Expand Down

0 comments on commit 4433fae

Please sign in to comment.