Skip to content

Commit 0fb7526

Browse files
authored
fix: don't panic on format due to unexpected comments (#7458)
fix: don't panic on format due to unexpected comments comments next to object elements is valid rego, instead of panicking catch the error and write the rule as-is. Signed-off-by: sspaink <[email protected]>
1 parent a3be450 commit 0fb7526

9 files changed

+1045
-209
lines changed

Diff for: v1/format/format.go

+688-209
Large diffs are not rendered by default.

Diff for: v1/format/testfiles/v0/test_issue_6330.rego

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package a
2+
3+
value := {"a":
4+
{"b":
5+
{"c":
6+
"d"}}}
7+
8+
value := {"a": # test 1
9+
"b"} # test 2
10+
11+
value := {"a": # test 1
12+
"b"}
13+
14+
value := {"a":
15+
{"b":
16+
{"c":
17+
"d"}}}
18+
19+
value := {"a": # this is
20+
{"b": # my ridiculous
21+
{"c": # way of
22+
"d"}}} # commenting code
23+
24+
value := {"a":
25+
{"b":
26+
{"c":
27+
"d"}}}
28+
29+
p[
30+
{"a": #
31+
"b"} #
32+
] := true
33+
34+
p.foo.bar[
35+
{"a": #
36+
"b"} #
37+
] := true
38+
39+
p[
40+
{"a": #
41+
"b"} #
42+
][
43+
{"c": #
44+
"d"} #
45+
] := true
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package a
2+
3+
value := {"a": {"b": {"c": "d"}}}
4+
5+
value := {"a": # test 1
6+
"b"} # test 2
7+
8+
value := {"a": "b"} # test 1
9+
10+
value := {"a": {"b": {"c": "d"}}}
11+
12+
value := {"a": # this is
13+
{"b": # my ridiculous
14+
{"c": # way of
15+
"d"}}} # commenting code
16+
17+
value := {"a": {"b": {"c": "d"}}}
18+
19+
p[{"a": #
20+
"b"} #
21+
] := true
22+
23+
p.foo.bar[{"a": #
24+
"b"} #
25+
] := true
26+
27+
p[{"a": #
28+
"b"} #
29+
][{"c": #
30+
"d"} #
31+
] := true

Diff for: v1/format/testfiles/v0/test_issue_6330_1.rego

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package a
2+
3+
p[
4+
{"a": #
5+
"b"} #
6+
] := true
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package a
2+
3+
p[
4+
{"a": #
5+
"b"} #
6+
] := true

Diff for: v1/format/testfiles/v1/test_issue_6330.rego

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package a
2+
3+
p[
4+
{"a": #
5+
"b"} #
6+
] := true
7+
8+
value := {"a":
9+
{"b":
10+
{"c":
11+
"d"}}}
12+
13+
value := {"a": # test 1
14+
"b"} # test 2
15+
16+
value := {"a": # test 1
17+
"b"}
18+
19+
value := {"a":
20+
{"b":
21+
{"c":
22+
"d"}}}
23+
24+
value := {"a": # this is
25+
{"b": # my ridiculous
26+
{"c": # way of
27+
"d"}}} # commenting code
28+
29+
p := {"a": #
30+
"b"} if { #
31+
str := "my \n string"
32+
}
33+
34+
value := {"a":
35+
{"b":
36+
{"c":
37+
"d"}}}
38+
39+
f(_) := value if {
40+
value := {"a": # this is
41+
{"b": # my ridiculous
42+
{"c": # way of
43+
"d"}}} # commenting code
44+
}
45+
46+
p := value if {
47+
value := {"a": # this is
48+
{"b": # my ridiculous
49+
{"c": # way of
50+
"d"}}} # commenting code
51+
}
52+
53+
p := x if {
54+
x := [v | v := {"a": # this is
55+
{"b": # my ridiculous
56+
{"c": # way of
57+
"d"}}} # commenting code
58+
]
59+
}
60+
61+
p if {
62+
every x in input.foo {
63+
x == {"a": # this is
64+
{"b": # my ridiculous
65+
{"c": # way of
66+
"d"}}} # commenting code
67+
}
68+
}
69+
70+
value contains {"a": #
71+
"b"} #
72+
73+
p := {"a": #
74+
str} if {
75+
str := "my \n string"
76+
}
77+
78+
p := {"a":
79+
str} if {
80+
#
81+
str := "my \n string"
82+
}
83+
84+
authorize := "allow" if {
85+
value := {"a": # this is
86+
{"b": # my ridiculous
87+
{"c": # way of
88+
"d"}}} # commenting code
89+
input.user == "superuser" # allow 'superuser' to perform any operation.
90+
} else := "deny" if {
91+
value := {"a": # this is
92+
{"b": # my ridiculous
93+
{"c": # way of
94+
"d"}}} # commenting code
95+
input.path[0] == value # disallow 'admin' operations...
96+
input.source_network == "external" # from external networks.
97+
} # ... more rules
98+
99+
p[
100+
{"a": #
101+
"b"} #
102+
] := true
103+
104+
p.foo.bar[
105+
{"a": #
106+
"b"} #
107+
] := true
108+
109+
p[
110+
{"a": #
111+
"b"} #
112+
][
113+
{"c": #
114+
"d"} #
115+
] := true
116+
117+
p if {
118+
x := {"a": # do
119+
"b"} # re
120+
1 + 2 == 3
121+
y := {"c": # mi
122+
"d"} # fa
123+
x != y
124+
}
125+
126+
p[x].r := y if {
127+
x := "q"
128+
y := 1
129+
y := {"c": # hello
130+
"d"} # world
131+
132+
133+
134+
y := 1
135+
}
+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package a
2+
3+
p[{"a": #
4+
"b"} #
5+
] := true
6+
7+
value := {"a": {"b": {"c": "d"}}}
8+
9+
value := {"a": # test 1
10+
"b"} # test 2
11+
12+
value := {"a": "b"} # test 1
13+
14+
value := {"a": {"b": {"c": "d"}}}
15+
16+
value := {"a": # this is
17+
{"b": # my ridiculous
18+
{"c": # way of
19+
"d"}}} # commenting code
20+
21+
p := {"a": #
22+
"b"} if { #
23+
str := "my \n string"
24+
}
25+
26+
value := {"a": {"b": {"c": "d"}}}
27+
28+
f(_) := value if {
29+
value := {"a": # this is
30+
{"b": # my ridiculous
31+
{"c": # way of
32+
"d"}}} # commenting code
33+
}
34+
35+
p := value if {
36+
value := {"a": # this is
37+
{"b": # my ridiculous
38+
{"c": # way of
39+
"d"}}} # commenting code
40+
}
41+
42+
p := x if {
43+
x := [v | v := {"a": # this is
44+
{"b": # my ridiculous
45+
{"c": # way of
46+
"d"}}} # commenting code
47+
]
48+
}
49+
50+
p if {
51+
every x in input.foo {
52+
x == {"a": # this is
53+
{"b": # my ridiculous
54+
{"c": # way of
55+
"d"}}} # commenting code
56+
}
57+
}
58+
59+
value contains {"a": #
60+
"b"} #
61+
62+
p := {"a": str} if { #
63+
str := "my \n string"
64+
}
65+
66+
p := {"a": str} if {
67+
#
68+
str := "my \n string"
69+
}
70+
71+
authorize := "allow" if {
72+
value := {"a": # this is
73+
{"b": # my ridiculous
74+
{"c": # way of
75+
"d"}}} # commenting code
76+
input.user == "superuser" # allow 'superuser' to perform any operation.
77+
} else := "deny" if {
78+
value := {"a": # this is
79+
{"b": # my ridiculous
80+
{"c": # way of
81+
"d"}}} # commenting code
82+
input.path[0] == value # disallow 'admin' operations...
83+
input.source_network == "external" # from external networks.
84+
} # ... more rules
85+
86+
p[{"a": #
87+
"b"} #
88+
] := true
89+
90+
p.foo.bar[{"a": #
91+
"b"} #
92+
] := true
93+
94+
p[{"a": #
95+
"b"} #
96+
][{"c": #
97+
"d"} #
98+
] := true
99+
100+
p if {
101+
x := {"a": # do
102+
"b"} # re
103+
1 + 2 == 3
104+
y := {"c": # mi
105+
"d"} # fa
106+
x != y
107+
}
108+
109+
p[x].r := y if {
110+
x := "q"
111+
y := 1
112+
y := {"c": # hello
113+
"d"} # world
114+
115+
y := 1
116+
}

Diff for: v1/format/testfiles/v1/test_issue_6330_1.rego

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package a
2+
3+
# A ref-head rule by itself is formatted differently when there are multiple ref-head rules in a file.
4+
# This file handles the case when there is a single ref-head rule, while `test_issue_6330.rego` handles multiple.
5+
6+
p[
7+
{"a": #
8+
"b"} #
9+
] := true
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package a
2+
3+
# A ref-head rule by itself is formatted differently when there are multiple ref-head rules in a file.
4+
# This file handles the case when there is a single ref-head rule, while `test_issue_6330.rego` handles multiple.
5+
6+
p[
7+
{"a": #
8+
"b"} #
9+
] := true

0 commit comments

Comments
 (0)