General refs in rule heads (#5913)

* Adding support for multiple variables at arbitrary locations in rule refs
* Updating type-checker to handle general ref heads

Fixes: #5993
Fixes: #5994

Signed-off-by: Johan Fylling <johan.dev@fylling.se>
This commit is contained in:
Johan Fylling
2023-08-31 12:29:49 +02:00
committed by GitHub
parent 9bf54788f5
commit 0431567c26
22 changed files with 2649 additions and 167 deletions
+1 -1
View File
@@ -461,7 +461,7 @@ func (w *writer) writeElse(rule *ast.Rule, o fmtOpts, comments []*ast.Comment) [
func (w *writer) writeHead(head *ast.Head, isDefault, isExpandedConst bool, o fmtOpts, comments []*ast.Comment) []*ast.Comment {
ref := head.Ref()
if head.Key != nil && head.Value == nil {
if head.Key != nil && head.Value == nil && !head.HasDynamicRef() {
ref = ref.GroundPrefix()
}
if o.refHeads || len(ref) == 1 {
+2
View File
@@ -78,6 +78,8 @@ func TestFormatSourceError(t *testing.T) {
}
func TestFormatSource(t *testing.T) {
t.Setenv("EXPERIMENTAL_GENERAL_RULE_REFS", "true")
regoFiles, err := filepath.Glob("testfiles/*.rego")
if err != nil {
panic(err)
+7
View File
@@ -11,3 +11,10 @@ q[1] = y if true
r[x] if x := 10
p.q.r[x] if x := 10
p.q.r[2] if true
g[h].i[j].k { true }
g[h].i[j].k { h := 1; j = 2 }
g[3].i[j].k = x { j := 3; x = 4 }
g[h].i[j].k[l] if { true }
g[h].i[j].k[l] contains x { x = "foo" }
g[h].i[j].k[l] contains x { h := 5; j := 6; l = 7; x = "foo" }
@@ -17,3 +17,26 @@ r[x] if x := 10
p.q.r[x] if x := 10
p.q.r[2] = true
g[h].i[j].k = true
g[h].i[j].k if {
h := 1
j = 2
}
g[3].i[j].k = x if {
j := 3
x = 4
}
g[h].i[j].k[l] = true
g[h].i[j].k[l] contains x if x = "foo"
g[h].i[j].k[l] contains x if {
h := 5
j := 6
l = 7
x = "foo"
}