Skip to content
Draft
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
22 changes: 16 additions & 6 deletions v1/format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -1869,20 +1869,30 @@ func (w *writer) writeIterable(elements []any, last *ast.Location, close *ast.Lo
return nil, err
}

// If there are comments within the single line, don't collapse it and keep it as-is
// Return an error so that writeTerm will write the original formatting
if len(lines) == 1 {
isOneLine := len(lines) == 1
isNonEmptyMultilineSource := len(elements) > 0 && close.Row > last.Row
hasBoundaryComment := false
if isOneLine && isNonEmptyMultilineSource {
if w.beforeEnd != nil {
row := w.beforeEnd.Location.Row
hasBoundaryComment = row >= last.Row && row <= close.Row
}
for _, c := range comments {
if c.Location.Row > last.Row && c.Location.Row < close.Row {
row := c.Location.Row
if row > last.Row && row < close.Row {
return comments, unexpectedCommentError{
newComment: truncatedString(c.String(), 100),
newCommentRow: c.Location.Row,
}
}
if row == last.Row || row == close.Row {
hasBoundaryComment = true
}
}
}

if len(lines) > 1 {
isMultiline := len(lines) > 1 || (isOneLine && isNonEmptyMultilineSource && !hasBoundaryComment)
if isMultiline {
w.delayBeforeEnd()
w.startMultilineSeq()
}
Expand All @@ -1904,7 +1914,7 @@ func (w *writer) writeIterable(elements []any, last *ast.Location, close *ast.Lo
return nil, err
}

if len(lines) > 1 {
if isMultiline {
w.write(",")
w.endLine()
comments, err = w.insertComments(comments, close)
Expand Down
18 changes: 14 additions & 4 deletions v1/format/testfiles/v0/test.rego.formatted
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,15 @@ r = y {
# Comment on else
else = y {
y = ["howdy"]
x = {"x": {"y": "z"}}
x = {
"x": {
"y": "z",
},
}
a = {
"a": {"b": "c"},
"a": {
"b": "c",
},
"b": "c", "c": [
1, 2,
3, 4,
Expand Down Expand Up @@ -231,11 +237,15 @@ union_list := [(declare3 | declare4)]

union_set_2 := {(declare3 | declare4)}

union_object_multi_line := {"response": (declare3 | declare4)}
union_object_multi_line := {
"response": (declare3 | declare4),
}

union_object_key := {(declare3 | declare4): "foo"}

union_object_key_multi_line := {(declare3 | declare4): "foo"}
union_object_key_multi_line := {
(declare3 | declare4): "foo",
}

# more comments!
# more comments!
Expand Down
24 changes: 21 additions & 3 deletions v1/format/testfiles/v0/test_issue_6330.rego.formatted
Original file line number Diff line number Diff line change
@@ -1,20 +1,38 @@
package a

value := {"a": {"b": {"c": "d"}}}
value := {
"a": {
"b": {
"c": "d",
},
},
}

value := {"a": # test 1
"b"} # test 2

value := {"a": "b"} # test 1

value := {"a": {"b": {"c": "d"}}}
value := {
"a": {
"b": {
"c": "d",
},
},
}

value := {"a": # this is
{"b": # my ridiculous
{"c": # way of
"d"}}} # commenting code

value := {"a": {"b": {"c": "d"}}}
value := {
"a": {
"b": {
"c": "d",
},
},
}

p[{"a": #
"b"} #
Expand Down
4 changes: 3 additions & 1 deletion v1/format/testfiles/v0/test_with.rego.formatted
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ single_line_with {
multi_line_with {
fn(1) with input.a as "a"
with input.b as "b"
with input.c as {"foo": "bar"}
with input.c as {
"foo": "bar",
}
with input.d as [
1,
2,
Expand Down
18 changes: 14 additions & 4 deletions v1/format/testfiles/v1/test.rego.formatted
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,15 @@ r := y if {
# Comment on else
else := y if {
y = ["howdy"]
x = {"x": {"y": "z"}}
x = {
"x": {
"y": "z",
},
}
a = {
"a": {"b": "c"},
"a": {
"b": "c",
},
"b": "c", "c": [
1, 2,
3, 4,
Expand Down Expand Up @@ -222,11 +228,15 @@ union_list := [(declare3 | declare4)]

union_set_2 := {(declare3 | declare4)}

union_object_multi_line := {"response": (declare3 | declare4)}
union_object_multi_line := {
"response": (declare3 | declare4),
}

union_object_key := {(declare3 | declare4): "foo"}

union_object_key_multi_line := {(declare3 | declare4): "foo"}
union_object_key_multi_line := {
(declare3 | declare4): "foo",
}

# more comments!
# more comments!
Expand Down
28 changes: 24 additions & 4 deletions v1/format/testfiles/v1/test_issue_6330.rego.formatted
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,26 @@ p[{"a": #
"b"} #
] := true

value := {"a": {"b": {"c": "d"}}}
value := {
"a": {
"b": {
"c": "d",
},
},
}

value := {"a": # test 1
"b"} # test 2

value := {"a": "b"} # test 1

value := {"a": {"b": {"c": "d"}}}
value := {
"a": {
"b": {
"c": "d",
},
},
}

value := {"a": # this is
{"b": # my ridiculous
Expand All @@ -23,7 +35,13 @@ p := {"a": #
str := "my \n string"
}

value := {"a": {"b": {"c": "d"}}}
value := {
"a": {
"b": {
"c": "d",
},
},
}

f(_) := value if {
value := {"a": # this is
Expand Down Expand Up @@ -63,7 +81,9 @@ p := {"a": str} if { #
str := "my \n string"
}

p := {"a": str} if {
p := {
"a": str,
} if {
#
str := "my \n string"
}
Expand Down
19 changes: 19 additions & 0 deletions v1/format/testfiles/v1/test_issue_8557.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package test

test_with_and_indentation if {
my_mock_object := {
"the_first_key": {
"a_nested_key": {
"and_sub_nested_key": [
{
"yet_another_key": [
"and_sub_nested_key_value", "foooo", "baaar",
],
},
],
}
}
}

_ = my_mock_object
}
19 changes: 19 additions & 0 deletions v1/format/testfiles/v1/test_issue_8557.rego.formatted
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package test

test_with_and_indentation if {
my_mock_object := {
"the_first_key": {
"a_nested_key": {
"and_sub_nested_key": [
{
"yet_another_key": [
"and_sub_nested_key_value", "foooo", "baaar",
],
},
],
},
},
}

_ = my_mock_object
}
4 changes: 3 additions & 1 deletion v1/format/testfiles/v1/test_with.rego.formatted
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ single_line_with if {
multi_line_with if {
fn(1) with input.a as "a"
with input.b as "b"
with input.c as {"foo": "bar"}
with input.c as {
"foo": "bar",
}
with input.d as [
1,
2,
Expand Down
Loading