Skip to content

Commit

Permalink
format: keep new lines in between function arguments
Browse files Browse the repository at this point in the history
This snippet,

    r = contains(
        input.x,
        "y",
    )

would have been formatted as

    r = contains(input.x, "y")

before. Now, any new lines added between function arguments will be kept, and
the snippet will not be reformatted.

As a consequence, comments on the separate arguments we OK:

    r = contains(
        input.x, # haystack
        "y",     # needle
    )

and don't freak out the formatter.

Fixes #3836.

Signed-off-by: Stephan Renatus <[email protected]>
  • Loading branch information
srenatus committed Oct 6, 2021
1 parent 6fb0071 commit 183af7d
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 9 deletions.
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
)

0 comments on commit 183af7d

Please sign in to comment.