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 new lines in between function arguments #3864

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,15 +490,13 @@ func (w *writer) writeFunctionCall(expr *ast.Expr, comments []*ast.Comment) []*a

func (w *writer) writeFunctionCallPlain(terms []*ast.Term, comments []*ast.Comment) []*ast.Comment {
w.write(terms[0].String() + "(")
if len(terms) > 1 {
for _, v := range terms[1 : len(terms)-1] {
comments = w.writeTerm(v, comments)
w.write(", ")
}
comments = w.writeTerm(terms[len(terms)-1], comments)
defer w.write(")")
args := make([]interface{}, len(terms)-1)
for i, t := range terms[1:] {
args[i] = t
}
w.write(")")
return comments
loc := terms[0].Location
return w.writeIterable(args, loc, closingLoc(0, 0, '(', ')', loc), comments, w.listWriter())
}

func (w *writer) writeWith(with *ast.With, comments []*ast.Comment) []*ast.Comment {
Expand Down
5 changes: 4 additions & 1 deletion format/testfiles/test.rego.formatted
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ fn2(
split(x, y, c)
trim(a, z, d) # function comment 1
split(c[0], d, b)
x = sprintf("hello %v", ["world"])
x = sprintf(
"hello %v",
["world"],
)
#function comment 2
} # function comment 3

Expand Down
16 changes: 16 additions & 0 deletions format/testfiles/test_fun_args_with_linebreaks.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package test

p {
x := count(
[1, 2, 3] # four
)
y := concat(
"/",
["foo", "bar"],
)
z := concat("/",
[
"foo",
"bar",
])
}
18 changes: 18 additions & 0 deletions format/testfiles/test_fun_args_with_linebreaks.rego.formatted
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package test

p {
x := count([1, 2, 3]) # four

y := concat(
"/",
["foo", "bar"],
)

z := concat(
"/",
[
"foo",
"bar",
],
)
}
6 changes: 6 additions & 0 deletions format/testfiles/test_issue_3836.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package testcase

rule1 = contains(
"", # first comment
"", # second comment
)
6 changes: 6 additions & 0 deletions format/testfiles/test_issue_3836.rego.formatted
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package testcase

rule1 = contains(
"", # first comment
"", # second comment
)