Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

format: keep whitespaces for multiple indented same-line withs #4635

Merged
merged 1 commit into from Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be nice if we put each with on its own line, but at least this fixes the bug!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#4508 😉

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hah! Yeah I suppose so :)

with input.d as "d"
}

mock_f(_) = 123

func_replacements {
Expand Down