diff --git a/ast/policy.go b/ast/policy.go index 29963a09a4..43e9bba4a3 100644 --- a/ast/policy.go +++ b/ast/policy.go @@ -100,7 +100,7 @@ var Wildcard = &Term{Value: Var("_")} var WildcardPrefix = "$" // Keywords contains strings that map to language keywords. -var Keywords = KeywordsV0 +var Keywords = KeywordsForRegoVersion(DefaultRegoVersion) var KeywordsV0 = [...]string{ "not", @@ -134,9 +134,23 @@ var KeywordsV1 = [...]string{ "every", } +func KeywordsForRegoVersion(v RegoVersion) []string { + switch v { + case RegoV0: + return KeywordsV0[:] + case RegoV1, RegoV0CompatV1: + return KeywordsV1[:] + } + return nil +} + // IsKeyword returns true if s is a language keyword. func IsKeyword(s string) bool { - for _, x := range Keywords { + return IsInKeywords(s, Keywords) +} + +func IsInKeywords(s string, keywords []string) bool { + for _, x := range keywords { if x == s { return true } diff --git a/compile/compile_test.go b/compile/compile_test.go index ef683c7591..be1790ba63 100644 --- a/compile/compile_test.go +++ b/compile/compile_test.go @@ -1741,7 +1741,7 @@ contains := 2 { import rego.v1 p if { - data.foo.contains = input.x + data.foo["contains"] = input.x } `, `package foo diff --git a/format/format.go b/format/format.go index 820c645d6e..43e5594669 100644 --- a/format/format.go +++ b/format/format.go @@ -126,7 +126,16 @@ type fmtOpts struct { // than if they don't. refHeads bool - regoV1 bool + regoV1 bool + futureKeywords []string +} + +func (o fmtOpts) keywords() []string { + if o.regoV1 { + return ast.KeywordsV1[:] + } + kws := ast.KeywordsV0[:] + return append(kws, o.futureKeywords...) } func AstWithOpts(x interface{}, opts Opts) ([]byte, error) { @@ -171,6 +180,10 @@ func AstWithOpts(x interface{}, opts Opts) ([]byte, error) { } case *ast.Import: + if kw, ok := future.WhichFutureKeyword(n); ok { + o.futureKeywords = append(o.futureKeywords, kw) + } + switch { case isRegoV1Compatible(n): o.contains = true @@ -200,8 +213,9 @@ func AstWithOpts(x interface{}, opts Opts) ([]byte, error) { }) w := &writer{ - indent: "\t", - errs: make([]*ast.Error, 0), + indent: "\t", + errs: make([]*ast.Error, 0), + fmtOpts: o, } switch x := x.(type) { @@ -219,18 +233,17 @@ func AstWithOpts(x interface{}, opts Opts) ([]byte, error) { x.Imports = ensureFutureKeywordImport(x.Imports, kw) } } - w.writeModule(x, o) + w.writeModule(x) case *ast.Package: w.writePackage(x, nil) case *ast.Import: w.writeImports([]*ast.Import{x}, nil) case *ast.Rule: - w.writeRule(x, false /* isElse */, o, nil) + w.writeRule(x, false /* isElse */, nil) case *ast.Head: w.writeHead(x, false, // isDefault false, // isExpandedConst - o, nil) case ast.Body: w.writeBody(x, nil) @@ -302,9 +315,10 @@ type writer struct { beforeEnd *ast.Comment delay bool errs ast.Errors + fmtOpts fmtOpts } -func (w *writer) writeModule(module *ast.Module, o fmtOpts) { +func (w *writer) writeModule(module *ast.Module) { var pkg *ast.Package var others []interface{} var comments []*ast.Comment @@ -342,7 +356,7 @@ func (w *writer) writeModule(module *ast.Module, o fmtOpts) { imports, others = gatherImports(others) comments = w.writeImports(imports, comments) rules, others = gatherRules(others) - comments = w.writeRules(rules, o, comments) + comments = w.writeRules(rules, comments) } for i, c := range comments { @@ -365,7 +379,15 @@ func (w *writer) writePackage(pkg *ast.Package, comments []*ast.Comment) []*ast. comments = w.insertComments(comments, pkg.Location) w.startLine() - w.write(pkg.String()) + + // Omit head as all packages have the DefaultRootDocument prepended at parse time. + path := make(ast.Ref, len(pkg.Path)-1) + path[0] = ast.VarTerm(string(pkg.Path[1].Value.(ast.String))) + copy(path[1:], pkg.Path[2:]) + + w.write("package ") + w.writeRef(path) + w.blankLine() return comments @@ -380,16 +402,16 @@ func (w *writer) writeComments(comments []*ast.Comment) { } } -func (w *writer) writeRules(rules []*ast.Rule, o fmtOpts, comments []*ast.Comment) []*ast.Comment { +func (w *writer) writeRules(rules []*ast.Rule, comments []*ast.Comment) []*ast.Comment { for _, rule := range rules { comments = w.insertComments(comments, rule.Location) - comments = w.writeRule(rule, false, o, comments) + comments = w.writeRule(rule, false, comments) w.blankLine() } return comments } -func (w *writer) writeRule(rule *ast.Rule, isElse bool, o fmtOpts, comments []*ast.Comment) []*ast.Comment { +func (w *writer) writeRule(rule *ast.Rule, isElse bool, comments []*ast.Comment) []*ast.Comment { if rule == nil { return comments } @@ -408,17 +430,17 @@ func (w *writer) writeRule(rule *ast.Rule, isElse bool, o fmtOpts, comments []*a // pretend that the rule has no body in this case. isExpandedConst := rule.Body.Equal(ast.NewBody(ast.NewExpr(ast.BooleanTerm(true)))) && rule.Else == nil - comments = w.writeHead(rule.Head, rule.Default, isExpandedConst, o, comments) + comments = w.writeHead(rule.Head, rule.Default, isExpandedConst, comments) // this excludes partial sets UNLESS `contains` is used - partialSetException := o.contains || rule.Head.Value != nil + partialSetException := w.fmtOpts.contains || rule.Head.Value != nil if len(rule.Body) == 0 || isExpandedConst { w.endLine() return comments } - if (o.regoV1 || o.ifs) && partialSetException { + if (w.fmtOpts.regoV1 || w.fmtOpts.ifs) && partialSetException { w.write(" if") if len(rule.Body) == 1 { if rule.Body[0].Location.Row == rule.Head.Location.Row { @@ -426,7 +448,7 @@ func (w *writer) writeRule(rule *ast.Rule, isElse bool, o fmtOpts, comments []*a comments = w.writeExpr(rule.Body[0], comments) w.endLine() if rule.Else != nil { - comments = w.writeElse(rule, o, comments) + comments = w.writeElse(rule, comments) } return comments } @@ -454,12 +476,12 @@ func (w *writer) writeRule(rule *ast.Rule, isElse bool, o fmtOpts, comments []*a w.startLine() w.write("}") if rule.Else != nil { - comments = w.writeElse(rule, o, comments) + comments = w.writeElse(rule, comments) } return comments } -func (w *writer) writeElse(rule *ast.Rule, o fmtOpts, comments []*ast.Comment) []*ast.Comment { +func (w *writer) writeElse(rule *ast.Rule, comments []*ast.Comment) []*ast.Comment { // If there was nothing else on the line before the "else" starts // then preserve this style of else block, otherwise it will be // started as an "inline" else eg: @@ -521,16 +543,16 @@ func (w *writer) writeElse(rule *ast.Rule, o fmtOpts, comments []*ast.Comment) [ rule.Else.Head.Value.Location = rule.Else.Head.Location } - return w.writeRule(rule.Else, true, o, comments) + return w.writeRule(rule.Else, true, comments) } -func (w *writer) writeHead(head *ast.Head, isDefault, isExpandedConst bool, o fmtOpts, comments []*ast.Comment) []*ast.Comment { +func (w *writer) writeHead(head *ast.Head, isDefault, isExpandedConst bool, comments []*ast.Comment) []*ast.Comment { ref := head.Ref() if head.Key != nil && head.Value == nil && !head.HasDynamicRef() { ref = ref.GroundPrefix() } - if o.refHeads || len(ref) == 1 { - w.write(ref.String()) + if w.fmtOpts.refHeads || len(ref) == 1 { + w.writeRef(ref) } else { w.write(ref[0].String()) w.write("[") @@ -548,7 +570,7 @@ func (w *writer) writeHead(head *ast.Head, isDefault, isExpandedConst bool, o fm w.write(")") } if head.Key != nil { - if o.contains && head.Value == nil { + if w.fmtOpts.contains && head.Value == nil { w.write(" contains ") comments = w.writeTerm(head.Key, comments) } else if head.Value == nil { // no `if` for p[x] notation @@ -566,7 +588,7 @@ func (w *writer) writeHead(head *ast.Head, isDefault, isExpandedConst bool, o fm // * a.b -> a contains "b" // * a.b.c -> a.b.c := true // * a.b.c.d -> a.b.c.d := true - isRegoV1RefConst := o.regoV1 && isExpandedConst && head.Key == nil && len(head.Args) == 0 + isRegoV1RefConst := w.fmtOpts.regoV1 && isExpandedConst && head.Key == nil && len(head.Args) == 0 if head.Location == head.Value.Location && head.Name != "else" && @@ -578,7 +600,7 @@ func (w *writer) writeHead(head *ast.Head, isDefault, isExpandedConst bool, o fm return comments } - if head.Assign || o.regoV1 { + if head.Assign || w.fmtOpts.regoV1 { // preserve assignment operator, and enforce it if formatting for Rego v1 w.write(" := ") } else { @@ -856,7 +878,7 @@ var varRegexp = regexp.MustCompile("^[[:alpha:]_][[:alpha:][:digit:]_]*$") func (w *writer) writeRefStringPath(s ast.String) { str := string(s) - if varRegexp.MatchString(str) && !ast.IsKeyword(str) { + if varRegexp.MatchString(str) && !ast.IsInKeywords(str, w.fmtOpts.keywords()) { w.write("." + str) } else { w.writeBracketed(s.String()) @@ -1067,7 +1089,7 @@ func (w *writer) writeImports(imports []*ast.Import, comments []*ast.Comment) [] }) for _, i := range group { w.startLine() - w.write(i.String()) + w.writeImport(i) if c, ok := m[i]; ok { w.write(" " + c.String()) } @@ -1079,6 +1101,28 @@ func (w *writer) writeImports(imports []*ast.Import, comments []*ast.Comment) [] return comments } +func (w *writer) writeImport(imp *ast.Import) { + path := imp.Path.Value.(ast.Ref) + + buf := []string{"import"} + + if _, ok := future.WhichFutureKeyword(imp); ok { + // We don't want to wrap future.keywords imports in parens, so we create a new writer that doesn't + w2 := writer{ + buf: bytes.Buffer{}, + } + w2.writeRef(path) + buf = append(buf, w2.buf.String()) + } else { + buf = append(buf, path.String()) + } + + if len(imp.Alias) > 0 { + buf = append(buf, "as "+imp.Alias.String()) + } + w.write(strings.Join(buf, " ")) +} + type entryWriter func(interface{}, []*ast.Comment) []*ast.Comment func (w *writer) writeIterable(elements []interface{}, last *ast.Location, close *ast.Location, comments []*ast.Comment, fn entryWriter) []*ast.Comment { @@ -1505,7 +1549,12 @@ func ensureFutureKeywordImport(imps []*ast.Import, kw string) []*ast.Import { } } imp := &ast.Import{ - Path: ast.MustParseTerm("future.keywords." + kw), + // NOTE: This is a hack to not error on the ref containing a keyword already present in v1. + // A cleaner solution would be to instead allow refs to contain keyword terms. + // E.g. in v1, `import future.keywords["in"]` is valid, but `import future.keywords.in` is not + // as it contains a reserved keyword. + Path: ast.MustParseTerm("future.keywords[\"" + kw + "\"]"), + //Path: ast.MustParseTerm("future.keywords." + kw), } imp.Location = defaultLocation(imp) return append(imps, imp) diff --git a/format/format_test.go b/format/format_test.go index 34f3e8404b..d3a6a60786 100644 --- a/format/format_test.go +++ b/format/format_test.go @@ -17,21 +17,43 @@ import ( ) func TestFormatNilLocation(t *testing.T) { - rule := ast.MustParseRule(`r = y { y = "foo" }`) - rule.Head.Location = nil - - bs, err := Ast(rule) - if err != nil { - t.Fatal(err) + tests := []struct { + note string + regoVersion ast.RegoVersion + rule string + exp string + }{ + { + note: "v0", + regoVersion: ast.RegoV0, + rule: `r = y { y = "foo" }`, + exp: `r = y { + y = "foo" +}`, + }, + { + note: "v1", + regoVersion: ast.RegoV1, + rule: `r = y if { y = "foo" }`, + exp: `r := y if y = "foo" +`, + }, } - exp := strings.Trim(` -r = y { - y = "foo" -}`, " \n") + for _, tc := range tests { + t.Run(tc.note, func(t *testing.T) { + rule := ast.MustParseRuleWithOpts(tc.rule, ast.ParserOptions{RegoVersion: tc.regoVersion}) + rule.Head.Location = nil - if string(bs) != exp { - t.Fatalf("Expected %q but got %q", exp, string(bs)) + bs, err := AstWithOpts(rule, Opts{RegoVersion: tc.regoVersion}) + if err != nil { + t.Fatal(err) + } + + if string(bs) != tc.exp { + t.Fatalf("Expected:\n\n%q\n\nbut got:\n\n%q", tc.exp, string(bs)) + } + }) } } @@ -59,7 +81,7 @@ func TestFormatNilLocationFunctionArgs(t *testing.T) { } func TestFormatSourceError(t *testing.T) { - rego := "testfiles/test.rego.error" + rego := "testfiles/v0/test.rego.error" contents, err := os.ReadFile(rego) if err != nil { t.Fatalf("Failed to read rego source: %v", err) @@ -70,15 +92,67 @@ func TestFormatSourceError(t *testing.T) { t.Fatal("Expected parsing error, not nil") } - exp := "1 error occurred: testfiles/test.rego.error:27: rego_parse_error: unexpected eof token" + exp := "1 error occurred: testfiles/v0/test.rego.error:27: rego_parse_error: unexpected eof token" if !strings.HasPrefix(err.Error(), exp) { t.Fatalf("Expected error message '%s', got '%s'", exp, err.Error()) } } -func TestFormatSource(t *testing.T) { - regoFiles, err := filepath.Glob("testfiles/*.rego") +func TestFormatV0Source(t *testing.T) { + regoFiles, err := filepath.Glob("testfiles/v0/*.rego") + if err != nil { + panic(err) + } + + for _, rego := range regoFiles { + t.Run(rego, func(t *testing.T) { + contents, err := os.ReadFile(rego) + if err != nil { + t.Fatalf("Failed to read rego source: %v", err) + } + + expected, err := os.ReadFile(rego + ".formatted") + if err != nil { + t.Fatalf("Failed to read expected rego source: %v", err) + } + + popts := ast.ParserOptions{ + RegoVersion: ast.RegoV0, + } + opts := Opts{ + RegoVersion: ast.RegoV0, + ParserOptions: &popts, + } + + formatted, err := SourceWithOpts(rego, contents, opts) + if err != nil { + t.Fatalf("Failed to format file: %v", err) + } + + if ln, at := differsAt(formatted, expected); ln != 0 { + t.Fatalf("Expected formatted bytes to equal expected bytes but differed near line %d / byte %d (got: %q, expected: %q):\n%s", ln, at, formatted[at], expected[at], prefixWithLineNumbers(formatted)) + } + + if _, err := ast.ParseModuleWithOpts(rego+".tmp", string(formatted), popts); err != nil { + t.Fatalf("Failed to parse formatted bytes: %v", err) + } + + formatted, err = SourceWithOpts(rego, formatted, opts) + if err != nil { + t.Fatalf("Failed to double format file") + } + + if ln, at := differsAt(formatted, expected); ln != 0 { + t.Fatalf("Expected roundtripped bytes to equal expected bytes but differed near line %d / byte %d:\n%s", ln, at, prefixWithLineNumbers(formatted)) + } + + }) + } +} + +func TestFormatV1Source(t *testing.T) { + regoFiles, err := filepath.Glob("testfiles/v1/*.rego") if err != nil { panic(err) } @@ -95,7 +169,15 @@ func TestFormatSource(t *testing.T) { t.Fatalf("Failed to read expected rego source: %v", err) } - formatted, err := Source(rego, contents) + popts := ast.ParserOptions{ + RegoVersion: ast.RegoV1, + } + opts := Opts{ + RegoVersion: ast.RegoV1, + ParserOptions: &popts, + } + + formatted, err := SourceWithOpts(rego, contents, opts) if err != nil { t.Fatalf("Failed to format file: %v", err) } @@ -104,11 +186,11 @@ func TestFormatSource(t *testing.T) { t.Fatalf("Expected formatted bytes to equal expected bytes but differed near line %d / byte %d (got: %q, expected: %q):\n%s", ln, at, formatted[at], expected[at], prefixWithLineNumbers(formatted)) } - if _, err := ast.ParseModule(rego+".tmp", string(formatted)); err != nil { + if _, err := ast.ParseModuleWithOpts(rego+".tmp", string(formatted), popts); err != nil { t.Fatalf("Failed to parse formatted bytes: %v", err) } - formatted, err = Source(rego, formatted) + formatted, err = SourceWithOpts(rego, formatted, opts) if err != nil { t.Fatalf("Failed to double format file") } @@ -121,8 +203,8 @@ func TestFormatSource(t *testing.T) { } } -func TestFormatSourceToRegoV1(t *testing.T) { - regoFiles, err := filepath.Glob("testfiles/rego_v1/*.rego") +func TestFormatV0SourceToRegoV1(t *testing.T) { + regoFiles, err := filepath.Glob("testfiles/v0_to_v1/*.rego") if err != nil { panic(err) } @@ -149,8 +231,19 @@ func TestFormatSourceToRegoV1(t *testing.T) { } } + sourceOpts := Opts{ + RegoVersion: ast.RegoV0CompatV1, // Target syntax is v0 compat v1 + ParserOptions: &ast.ParserOptions{ + RegoVersion: ast.RegoV0, // Original syntax is v0 + }, + } + targetOpts := Opts{ + RegoVersion: ast.RegoV0CompatV1, // Target syntax is v0 compat v1 + } + if errorExpected { - formatted, err := SourceWithOpts(rego, contents, Opts{RegoVersion: ast.RegoV0CompatV1}) + formatted, err := SourceWithOpts(rego, contents, sourceOpts) + if err == nil { t.Fatalf("Expected error, got: %s", formatted) } @@ -158,7 +251,8 @@ func TestFormatSourceToRegoV1(t *testing.T) { t.Fatalf("Expected error:\n\n'%s'\n\ngot:\n\n'%s'", expected, err.Error()) } } else { - formatted, err := SourceWithOpts(rego, contents, Opts{RegoVersion: ast.RegoV0CompatV1}) + formatted, err := SourceWithOpts(rego, contents, sourceOpts) + if err != nil { t.Fatalf("Failed to format file: %v", err) } @@ -171,7 +265,7 @@ func TestFormatSourceToRegoV1(t *testing.T) { t.Fatalf("Failed to parse formatted bytes: %v", err) } - formatted, err = SourceWithOpts(rego, formatted, Opts{RegoVersion: ast.RegoV0CompatV1}) + formatted, err = SourceWithOpts(rego, formatted, targetOpts) if err != nil { t.Fatalf("Failed to double format file") } @@ -181,7 +275,7 @@ func TestFormatSourceToRegoV1(t *testing.T) { } // rego-v1 formatted code is still compliant with v0, and should not be changed if formatted as such - formatted, err = Source(rego, formatted) + formatted, err = SourceWithOpts(rego, formatted, targetOpts) if err != nil { t.Fatalf("Failed to double format file as v0") } @@ -196,9 +290,10 @@ func TestFormatSourceToRegoV1(t *testing.T) { func TestFormatAST(t *testing.T) { cases := []struct { - note string - toFmt interface{} - expected string + note string + regoVersion ast.RegoVersion + toFmt interface{} + expected string }{ { note: "var", @@ -321,12 +416,16 @@ func TestFormatAST(t *testing.T) { expected: `some x, y in xs`, }, { - note: "every adds import if missing", + note: "v0, every adds import if missing", + regoVersion: ast.RegoV0, toFmt: ast.MustParseModuleWithOpts(`package test p { every k, v in [1, 2] { k != v } }`, - ast.ParserOptions{FutureKeywords: []string{"every"}}), + ast.ParserOptions{ + RegoVersion: ast.RegoV0, + FutureKeywords: []string{"every"}, + }), expected: `package test import future.keywords.every @@ -336,13 +435,31 @@ p { }`, }, { - note: "every does not add import if all future KWs are there", + note: "v1, every doesn't add import if missing", + regoVersion: ast.RegoV1, + toFmt: ast.MustParseModuleWithOpts(`package test + p if { + every k, v in [1, 2] { k != v } + }`, + ast.ParserOptions{RegoVersion: ast.RegoV1}), + expected: `package test + +p if { + every k, v in [1, 2] { k != v } +}`, + }, + { + note: "v0: every does not add import if all future KWs are there", + regoVersion: ast.RegoV0, toFmt: ast.MustParseModuleWithOpts(`package test import future.keywords p { every k, v in [1, 2] { k != v } }`, - ast.ParserOptions{FutureKeywords: []string{"every"}}), + ast.ParserOptions{ + FutureKeywords: []string{"every"}, + RegoVersion: ast.RegoV0, + }), expected: `package test import future.keywords @@ -352,13 +469,17 @@ p if { }`, }, { - note: "every does not add import if already present", + note: "v0: every does not add import if already present", + regoVersion: ast.RegoV0, toFmt: ast.MustParseModuleWithOpts(`package test import future.keywords p { every k, v in [1, 2] { k != v } }`, - ast.ParserOptions{FutureKeywords: []string{"every"}}), + ast.ParserOptions{ + FutureKeywords: []string{"every"}, + RegoVersion: ast.RegoV0, + }), expected: `package test import future.keywords @@ -520,7 +641,12 @@ a[_x[y][[z, w]]]`, for _, tc := range cases { t.Run(tc.note, func(t *testing.T) { - bs, err := Ast(tc.toFmt) + bs, err := AstWithOpts(tc.toFmt, Opts{ + RegoVersion: tc.regoVersion, + ParserOptions: &ast.ParserOptions{ + RegoVersion: tc.regoVersion, + }, + }) if err != nil { t.Fatalf("Unexpected error: %s", err) } diff --git a/format/testfiles/rego_v1/deprecated_builtins.rego.error b/format/testfiles/rego_v1/deprecated_builtins.rego.error deleted file mode 100644 index bc23726d89..0000000000 --- a/format/testfiles/rego_v1/deprecated_builtins.rego.error +++ /dev/null @@ -1,16 +0,0 @@ -15 errors occurred: -testfiles/rego_v1/deprecated_builtins.rego:4: rego_type_error: deprecated built-in function calls in expression: any -testfiles/rego_v1/deprecated_builtins.rego:5: rego_type_error: deprecated built-in function calls in expression: all -testfiles/rego_v1/deprecated_builtins.rego:6: rego_type_error: deprecated built-in function calls in expression: cast_array -testfiles/rego_v1/deprecated_builtins.rego:7: rego_type_error: deprecated built-in function calls in expression: cast_boolean -testfiles/rego_v1/deprecated_builtins.rego:8: rego_type_error: deprecated built-in function calls in expression: cast_null -testfiles/rego_v1/deprecated_builtins.rego:9: rego_type_error: deprecated built-in function calls in expression: cast_object -testfiles/rego_v1/deprecated_builtins.rego:10: rego_type_error: deprecated built-in function calls in expression: cast_set -testfiles/rego_v1/deprecated_builtins.rego:11: rego_type_error: deprecated built-in function calls in expression: cast_string -testfiles/rego_v1/deprecated_builtins.rego:12: rego_type_error: deprecated built-in function calls in expression: net.cidr_overlap -testfiles/rego_v1/deprecated_builtins.rego:13: rego_type_error: deprecated built-in function calls in expression: re_match -testfiles/rego_v1/deprecated_builtins.rego:14: rego_type_error: deprecated built-in function calls in expression: set_diff -testfiles/rego_v1/deprecated_builtins.rego:17: rego_type_error: deprecated built-in function calls in expression: any -testfiles/rego_v1/deprecated_builtins.rego:19: rego_type_error: deprecated built-in function calls in expression: any -testfiles/rego_v1/deprecated_builtins.rego:21: rego_type_error: deprecated built-in function calls in expression: any -testfiles/rego_v1/deprecated_builtins.rego:21: rego_type_error: deprecated built-in function calls in expression: all \ No newline at end of file diff --git a/format/testfiles/rego_v1/duplicate_imports.rego.error b/format/testfiles/rego_v1/duplicate_imports.rego.error deleted file mode 100644 index 8ef51d2533..0000000000 --- a/format/testfiles/rego_v1/duplicate_imports.rego.error +++ /dev/null @@ -1,3 +0,0 @@ -2 errors occurred: -testfiles/rego_v1/duplicate_imports.rego:4: rego_compile_error: import must not shadow import data.foo -testfiles/rego_v1/duplicate_imports.rego:5: rego_compile_error: import must not shadow import data.foo \ No newline at end of file diff --git a/format/testfiles/rego_v1/keywords.rego.error b/format/testfiles/rego_v1/keywords.rego.error deleted file mode 100644 index fb9c3340be..0000000000 --- a/format/testfiles/rego_v1/keywords.rego.error +++ /dev/null @@ -1,5 +0,0 @@ -4 errors occurred: -testfiles/rego_v1/keywords.rego:3: rego_parse_error: if keyword cannot be used for rule name -testfiles/rego_v1/keywords.rego:5: rego_parse_error: contains keyword cannot be used for rule name -testfiles/rego_v1/keywords.rego:7: rego_parse_error: in keyword cannot be used for rule name -testfiles/rego_v1/keywords.rego:9: rego_parse_error: every keyword cannot be used for rule name \ No newline at end of file diff --git a/format/testfiles/rego_v1/shadowing.rego.error b/format/testfiles/rego_v1/shadowing.rego.error deleted file mode 100644 index 0c7ebefb82..0000000000 --- a/format/testfiles/rego_v1/shadowing.rego.error +++ /dev/null @@ -1,13 +0,0 @@ -12 errors occurred: -testfiles/rego_v1/shadowing.rego:3: rego_compile_error: rules must not shadow input (use a different rule name) -testfiles/rego_v1/shadowing.rego:5: rego_compile_error: rules must not shadow input (use a different rule name) -testfiles/rego_v1/shadowing.rego:7: rego_compile_error: rules must not shadow input (use a different rule name) -testfiles/rego_v1/shadowing.rego:9: rego_compile_error: rules must not shadow data (use a different rule name) -testfiles/rego_v1/shadowing.rego:11: rego_compile_error: rules must not shadow data (use a different rule name) -testfiles/rego_v1/shadowing.rego:13: rego_compile_error: rules must not shadow data (use a different rule name) -testfiles/rego_v1/shadowing.rego:28: rego_compile_error: args must not shadow input (use a different variable name) -testfiles/rego_v1/shadowing.rego:32: rego_compile_error: args must not shadow data (use a different variable name) -testfiles/rego_v1/shadowing.rego:16: rego_compile_error: variables must not shadow input (use a different variable name) -testfiles/rego_v1/shadowing.rego:17: rego_compile_error: variables must not shadow data (use a different variable name) -testfiles/rego_v1/shadowing.rego:21: rego_compile_error: variables must not shadow input (use a different variable name) -testfiles/rego_v1/shadowing.rego:25: rego_compile_error: variables must not shadow data (use a different variable name) \ No newline at end of file diff --git a/format/testfiles/test.rego b/format/testfiles/v0/test.rego similarity index 100% rename from format/testfiles/test.rego rename to format/testfiles/v0/test.rego diff --git a/format/testfiles/test.rego.error b/format/testfiles/v0/test.rego.error similarity index 100% rename from format/testfiles/test.rego.error rename to format/testfiles/v0/test.rego.error diff --git a/format/testfiles/test.rego.formatted b/format/testfiles/v0/test.rego.formatted similarity index 100% rename from format/testfiles/test.rego.formatted rename to format/testfiles/v0/test.rego.formatted diff --git a/format/testfiles/test_assignments.rego b/format/testfiles/v0/test_assignments.rego similarity index 100% rename from format/testfiles/test_assignments.rego rename to format/testfiles/v0/test_assignments.rego diff --git a/format/testfiles/test_assignments.rego.formatted b/format/testfiles/v0/test_assignments.rego.formatted similarity index 100% rename from format/testfiles/test_assignments.rego.formatted rename to format/testfiles/v0/test_assignments.rego.formatted diff --git a/format/testfiles/test_contains.rego b/format/testfiles/v0/test_contains.rego similarity index 100% rename from format/testfiles/test_contains.rego rename to format/testfiles/v0/test_contains.rego diff --git a/format/testfiles/test_contains.rego.formatted b/format/testfiles/v0/test_contains.rego.formatted similarity index 88% rename from format/testfiles/test_contains.rego.formatted rename to format/testfiles/v0/test_contains.rego.formatted index 7b712fa9db..b372275212 100644 --- a/format/testfiles/test_contains.rego.formatted +++ b/format/testfiles/v0/test_contains.rego.formatted @@ -1,4 +1,4 @@ -package test.contains +package test["contains"] import future.keywords.contains diff --git a/format/testfiles/test_contains_if.rego b/format/testfiles/v0/test_contains_if.rego similarity index 100% rename from format/testfiles/test_contains_if.rego rename to format/testfiles/v0/test_contains_if.rego diff --git a/format/testfiles/test_contains_if.rego.formatted b/format/testfiles/v0/test_contains_if.rego.formatted similarity index 100% rename from format/testfiles/test_contains_if.rego.formatted rename to format/testfiles/v0/test_contains_if.rego.formatted diff --git a/format/testfiles/test_end_of_rule_comment.rego b/format/testfiles/v0/test_end_of_rule_comment.rego similarity index 100% rename from format/testfiles/test_end_of_rule_comment.rego rename to format/testfiles/v0/test_end_of_rule_comment.rego diff --git a/format/testfiles/test_end_of_rule_comment.rego.formatted b/format/testfiles/v0/test_end_of_rule_comment.rego.formatted similarity index 100% rename from format/testfiles/test_end_of_rule_comment.rego.formatted rename to format/testfiles/v0/test_end_of_rule_comment.rego.formatted diff --git a/format/testfiles/test_every.rego b/format/testfiles/v0/test_every.rego similarity index 100% rename from format/testfiles/test_every.rego rename to format/testfiles/v0/test_every.rego diff --git a/format/testfiles/test_every.rego.formatted b/format/testfiles/v0/test_every.rego.formatted similarity index 100% rename from format/testfiles/test_every.rego.formatted rename to format/testfiles/v0/test_every.rego.formatted diff --git a/format/testfiles/test_every_with_key.rego b/format/testfiles/v0/test_every_with_key.rego similarity index 100% rename from format/testfiles/test_every_with_key.rego rename to format/testfiles/v0/test_every_with_key.rego diff --git a/format/testfiles/test_every_with_key.rego.formatted b/format/testfiles/v0/test_every_with_key.rego.formatted similarity index 100% rename from format/testfiles/test_every_with_key.rego.formatted rename to format/testfiles/v0/test_every_with_key.rego.formatted diff --git a/format/testfiles/test_fun_args_with_linebreaks.rego b/format/testfiles/v0/test_fun_args_with_linebreaks.rego similarity index 100% rename from format/testfiles/test_fun_args_with_linebreaks.rego rename to format/testfiles/v0/test_fun_args_with_linebreaks.rego diff --git a/format/testfiles/test_fun_args_with_linebreaks.rego.formatted b/format/testfiles/v0/test_fun_args_with_linebreaks.rego.formatted similarity index 100% rename from format/testfiles/test_fun_args_with_linebreaks.rego.formatted rename to format/testfiles/v0/test_fun_args_with_linebreaks.rego.formatted diff --git a/format/testfiles/test_functions.rego b/format/testfiles/v0/test_functions.rego similarity index 100% rename from format/testfiles/test_functions.rego rename to format/testfiles/v0/test_functions.rego diff --git a/format/testfiles/test_functions.rego.formatted b/format/testfiles/v0/test_functions.rego.formatted similarity index 100% rename from format/testfiles/test_functions.rego.formatted rename to format/testfiles/v0/test_functions.rego.formatted diff --git a/format/testfiles/test_if.rego b/format/testfiles/v0/test_if.rego similarity index 100% rename from format/testfiles/test_if.rego rename to format/testfiles/v0/test_if.rego diff --git a/format/testfiles/test_if.rego.formatted b/format/testfiles/v0/test_if.rego.formatted similarity index 94% rename from format/testfiles/test_if.rego.formatted rename to format/testfiles/v0/test_if.rego.formatted index b657f105fd..cae30b526c 100644 --- a/format/testfiles/test_if.rego.formatted +++ b/format/testfiles/v0/test_if.rego.formatted @@ -1,4 +1,4 @@ -package test.if +package test["if"] import future.keywords.if diff --git a/format/testfiles/test_if_else.rego b/format/testfiles/v0/test_if_else.rego similarity index 100% rename from format/testfiles/test_if_else.rego rename to format/testfiles/v0/test_if_else.rego diff --git a/format/testfiles/test_if_else.rego.formatted b/format/testfiles/v0/test_if_else.rego.formatted similarity index 90% rename from format/testfiles/test_if_else.rego.formatted rename to format/testfiles/v0/test_if_else.rego.formatted index 76aeb5639e..12880d2cfb 100644 --- a/format/testfiles/test_if_else.rego.formatted +++ b/format/testfiles/v0/test_if_else.rego.formatted @@ -1,4 +1,4 @@ -package test.if +package test["if"] import future.keywords.if diff --git a/format/testfiles/v0/test_in.rego b/format/testfiles/v0/test_in.rego new file mode 100644 index 0000000000..63be7918b1 --- /dev/null +++ b/format/testfiles/v0/test_in.rego @@ -0,0 +1,13 @@ +package test.in + +import future.keywords.in + +a["in"] := "foo" + +b.c["in"] := "bar" + +c["in"].d := "baz" + +p { + input["in"] +} diff --git a/format/testfiles/v0/test_in.rego.formatted b/format/testfiles/v0/test_in.rego.formatted new file mode 100644 index 0000000000..8d4dec17f8 --- /dev/null +++ b/format/testfiles/v0/test_in.rego.formatted @@ -0,0 +1,13 @@ +package test["in"] + +import future.keywords.in + +a["in"] := "foo" + +b.c["in"] := "bar" + +c["in"].d := "baz" + +p { + input["in"] +} diff --git a/format/testfiles/test_in_operator_with_all_keywords_import.rego b/format/testfiles/v0/test_in_operator_with_all_keywords_import.rego similarity index 100% rename from format/testfiles/test_in_operator_with_all_keywords_import.rego rename to format/testfiles/v0/test_in_operator_with_all_keywords_import.rego diff --git a/format/testfiles/test_in_operator_with_all_keywords_import.rego.formatted b/format/testfiles/v0/test_in_operator_with_all_keywords_import.rego.formatted similarity index 100% rename from format/testfiles/test_in_operator_with_all_keywords_import.rego.formatted rename to format/testfiles/v0/test_in_operator_with_all_keywords_import.rego.formatted diff --git a/format/testfiles/test_in_operator_with_parenthesis.rego b/format/testfiles/v0/test_in_operator_with_parenthesis.rego similarity index 100% rename from format/testfiles/test_in_operator_with_parenthesis.rego rename to format/testfiles/v0/test_in_operator_with_parenthesis.rego diff --git a/format/testfiles/test_in_operator_with_parenthesis.rego.formatted b/format/testfiles/v0/test_in_operator_with_parenthesis.rego.formatted similarity index 100% rename from format/testfiles/test_in_operator_with_parenthesis.rego.formatted rename to format/testfiles/v0/test_in_operator_with_parenthesis.rego.formatted diff --git a/format/testfiles/test_in_operator_without_import.rego b/format/testfiles/v0/test_in_operator_without_import.rego similarity index 100% rename from format/testfiles/test_in_operator_without_import.rego rename to format/testfiles/v0/test_in_operator_without_import.rego diff --git a/format/testfiles/test_in_operator_without_import.rego.formatted b/format/testfiles/v0/test_in_operator_without_import.rego.formatted similarity index 100% rename from format/testfiles/test_in_operator_without_import.rego.formatted rename to format/testfiles/v0/test_in_operator_without_import.rego.formatted diff --git a/format/testfiles/test_issue_1560.rego b/format/testfiles/v0/test_issue_1560.rego similarity index 100% rename from format/testfiles/test_issue_1560.rego rename to format/testfiles/v0/test_issue_1560.rego diff --git a/format/testfiles/test_issue_1560.rego.formatted b/format/testfiles/v0/test_issue_1560.rego.formatted similarity index 100% rename from format/testfiles/test_issue_1560.rego.formatted rename to format/testfiles/v0/test_issue_1560.rego.formatted diff --git a/format/testfiles/test_issue_2299.rego b/format/testfiles/v0/test_issue_2299.rego similarity index 100% rename from format/testfiles/test_issue_2299.rego rename to format/testfiles/v0/test_issue_2299.rego diff --git a/format/testfiles/test_issue_2299.rego.formatted b/format/testfiles/v0/test_issue_2299.rego.formatted similarity index 100% rename from format/testfiles/test_issue_2299.rego.formatted rename to format/testfiles/v0/test_issue_2299.rego.formatted diff --git a/format/testfiles/test_issue_2420.rego b/format/testfiles/v0/test_issue_2420.rego similarity index 100% rename from format/testfiles/test_issue_2420.rego rename to format/testfiles/v0/test_issue_2420.rego diff --git a/format/testfiles/test_issue_2420.rego.formatted b/format/testfiles/v0/test_issue_2420.rego.formatted similarity index 100% rename from format/testfiles/test_issue_2420.rego.formatted rename to format/testfiles/v0/test_issue_2420.rego.formatted diff --git a/format/testfiles/test_issue_3836.rego b/format/testfiles/v0/test_issue_3836.rego similarity index 100% rename from format/testfiles/test_issue_3836.rego rename to format/testfiles/v0/test_issue_3836.rego diff --git a/format/testfiles/test_issue_3836.rego.formatted b/format/testfiles/v0/test_issue_3836.rego.formatted similarity index 100% rename from format/testfiles/test_issue_3836.rego.formatted rename to format/testfiles/v0/test_issue_3836.rego.formatted diff --git a/format/testfiles/test_issue_3849.rego b/format/testfiles/v0/test_issue_3849.rego similarity index 100% rename from format/testfiles/test_issue_3849.rego rename to format/testfiles/v0/test_issue_3849.rego diff --git a/format/testfiles/test_issue_3849.rego.formatted b/format/testfiles/v0/test_issue_3849.rego.formatted similarity index 100% rename from format/testfiles/test_issue_3849.rego.formatted rename to format/testfiles/v0/test_issue_3849.rego.formatted diff --git a/format/testfiles/test_issue_4606.rego b/format/testfiles/v0/test_issue_4606.rego similarity index 100% rename from format/testfiles/test_issue_4606.rego rename to format/testfiles/v0/test_issue_4606.rego diff --git a/format/testfiles/test_issue_4606.rego.formatted b/format/testfiles/v0/test_issue_4606.rego.formatted similarity index 100% rename from format/testfiles/test_issue_4606.rego.formatted rename to format/testfiles/v0/test_issue_4606.rego.formatted diff --git a/format/testfiles/test_issue_5348.rego b/format/testfiles/v0/test_issue_5348.rego similarity index 100% rename from format/testfiles/test_issue_5348.rego rename to format/testfiles/v0/test_issue_5348.rego diff --git a/format/testfiles/test_issue_5348.rego.formatted b/format/testfiles/v0/test_issue_5348.rego.formatted similarity index 100% rename from format/testfiles/test_issue_5348.rego.formatted rename to format/testfiles/v0/test_issue_5348.rego.formatted diff --git a/format/testfiles/test_issue_5449.rego b/format/testfiles/v0/test_issue_5449.rego similarity index 100% rename from format/testfiles/test_issue_5449.rego rename to format/testfiles/v0/test_issue_5449.rego diff --git a/format/testfiles/test_issue_5449.rego.formatted b/format/testfiles/v0/test_issue_5449.rego.formatted similarity index 100% rename from format/testfiles/test_issue_5449.rego.formatted rename to format/testfiles/v0/test_issue_5449.rego.formatted diff --git a/format/testfiles/test_issue_5449_with_contains_ref_rule.rego b/format/testfiles/v0/test_issue_5449_with_contains_ref_rule.rego similarity index 100% rename from format/testfiles/test_issue_5449_with_contains_ref_rule.rego rename to format/testfiles/v0/test_issue_5449_with_contains_ref_rule.rego diff --git a/format/testfiles/test_issue_5449_with_contains_ref_rule.rego.formatted b/format/testfiles/v0/test_issue_5449_with_contains_ref_rule.rego.formatted similarity index 100% rename from format/testfiles/test_issue_5449_with_contains_ref_rule.rego.formatted rename to format/testfiles/v0/test_issue_5449_with_contains_ref_rule.rego.formatted diff --git a/format/testfiles/test_issue_5449_with_ref_rule.rego b/format/testfiles/v0/test_issue_5449_with_ref_rule.rego similarity index 100% rename from format/testfiles/test_issue_5449_with_ref_rule.rego rename to format/testfiles/v0/test_issue_5449_with_ref_rule.rego diff --git a/format/testfiles/test_issue_5449_with_ref_rule.rego.formatted b/format/testfiles/v0/test_issue_5449_with_ref_rule.rego.formatted similarity index 100% rename from format/testfiles/test_issue_5449_with_ref_rule.rego.formatted rename to format/testfiles/v0/test_issue_5449_with_ref_rule.rego.formatted diff --git a/format/testfiles/test_issue_5537_with_comprehension.rego b/format/testfiles/v0/test_issue_5537_with_comprehension.rego similarity index 100% rename from format/testfiles/test_issue_5537_with_comprehension.rego rename to format/testfiles/v0/test_issue_5537_with_comprehension.rego diff --git a/format/testfiles/test_issue_5537_with_comprehension.rego.formatted b/format/testfiles/v0/test_issue_5537_with_comprehension.rego.formatted similarity index 100% rename from format/testfiles/test_issue_5537_with_comprehension.rego.formatted rename to format/testfiles/v0/test_issue_5537_with_comprehension.rego.formatted diff --git a/format/testfiles/test_issue_5537_with_ref.rego b/format/testfiles/v0/test_issue_5537_with_ref.rego similarity index 100% rename from format/testfiles/test_issue_5537_with_ref.rego rename to format/testfiles/v0/test_issue_5537_with_ref.rego diff --git a/format/testfiles/test_issue_5537_with_ref.rego.formatted b/format/testfiles/v0/test_issue_5537_with_ref.rego.formatted similarity index 100% rename from format/testfiles/test_issue_5537_with_ref.rego.formatted rename to format/testfiles/v0/test_issue_5537_with_ref.rego.formatted diff --git a/format/testfiles/test_issue_5798.rego b/format/testfiles/v0/test_issue_5798.rego similarity index 100% rename from format/testfiles/test_issue_5798.rego rename to format/testfiles/v0/test_issue_5798.rego diff --git a/format/testfiles/test_issue_5798.rego.formatted b/format/testfiles/v0/test_issue_5798.rego.formatted similarity index 100% rename from format/testfiles/test_issue_5798.rego.formatted rename to format/testfiles/v0/test_issue_5798.rego.formatted diff --git a/format/testfiles/test_issue_6161.rego b/format/testfiles/v0/test_issue_6161.rego similarity index 100% rename from format/testfiles/test_issue_6161.rego rename to format/testfiles/v0/test_issue_6161.rego diff --git a/format/testfiles/test_issue_6161.rego.formatted b/format/testfiles/v0/test_issue_6161.rego.formatted similarity index 100% rename from format/testfiles/test_issue_6161.rego.formatted rename to format/testfiles/v0/test_issue_6161.rego.formatted diff --git a/format/testfiles/test_ref_heads.rego b/format/testfiles/v0/test_ref_heads.rego similarity index 100% rename from format/testfiles/test_ref_heads.rego rename to format/testfiles/v0/test_ref_heads.rego diff --git a/format/testfiles/test_ref_heads.rego.formatted b/format/testfiles/v0/test_ref_heads.rego.formatted similarity index 100% rename from format/testfiles/test_ref_heads.rego.formatted rename to format/testfiles/v0/test_ref_heads.rego.formatted diff --git a/format/testfiles/test_rego_v1.rego b/format/testfiles/v0/test_rego_v1.rego similarity index 100% rename from format/testfiles/test_rego_v1.rego rename to format/testfiles/v0/test_rego_v1.rego diff --git a/format/testfiles/test_rego_v1.rego.formatted b/format/testfiles/v0/test_rego_v1.rego.formatted similarity index 100% rename from format/testfiles/test_rego_v1.rego.formatted rename to format/testfiles/v0/test_rego_v1.rego.formatted diff --git a/format/testfiles/test_with.rego b/format/testfiles/v0/test_with.rego similarity index 100% rename from format/testfiles/test_with.rego rename to format/testfiles/v0/test_with.rego diff --git a/format/testfiles/test_with.rego.formatted b/format/testfiles/v0/test_with.rego.formatted similarity index 100% rename from format/testfiles/test_with.rego.formatted rename to format/testfiles/v0/test_with.rego.formatted diff --git a/format/testfiles/rego_v1/constants.rego b/format/testfiles/v0_to_v1/constants.rego similarity index 100% rename from format/testfiles/rego_v1/constants.rego rename to format/testfiles/v0_to_v1/constants.rego diff --git a/format/testfiles/rego_v1/constants.rego.formatted b/format/testfiles/v0_to_v1/constants.rego.formatted similarity index 100% rename from format/testfiles/rego_v1/constants.rego.formatted rename to format/testfiles/v0_to_v1/constants.rego.formatted diff --git a/format/testfiles/rego_v1/deprecated_builtins.rego b/format/testfiles/v0_to_v1/deprecated_builtins.rego similarity index 100% rename from format/testfiles/rego_v1/deprecated_builtins.rego rename to format/testfiles/v0_to_v1/deprecated_builtins.rego diff --git a/format/testfiles/v0_to_v1/deprecated_builtins.rego.error b/format/testfiles/v0_to_v1/deprecated_builtins.rego.error new file mode 100644 index 0000000000..480d9c48ee --- /dev/null +++ b/format/testfiles/v0_to_v1/deprecated_builtins.rego.error @@ -0,0 +1,16 @@ +15 errors occurred: +testfiles/v0_to_v1/deprecated_builtins.rego:4: rego_type_error: deprecated built-in function calls in expression: any +testfiles/v0_to_v1/deprecated_builtins.rego:5: rego_type_error: deprecated built-in function calls in expression: all +testfiles/v0_to_v1/deprecated_builtins.rego:6: rego_type_error: deprecated built-in function calls in expression: cast_array +testfiles/v0_to_v1/deprecated_builtins.rego:7: rego_type_error: deprecated built-in function calls in expression: cast_boolean +testfiles/v0_to_v1/deprecated_builtins.rego:8: rego_type_error: deprecated built-in function calls in expression: cast_null +testfiles/v0_to_v1/deprecated_builtins.rego:9: rego_type_error: deprecated built-in function calls in expression: cast_object +testfiles/v0_to_v1/deprecated_builtins.rego:10: rego_type_error: deprecated built-in function calls in expression: cast_set +testfiles/v0_to_v1/deprecated_builtins.rego:11: rego_type_error: deprecated built-in function calls in expression: cast_string +testfiles/v0_to_v1/deprecated_builtins.rego:12: rego_type_error: deprecated built-in function calls in expression: net.cidr_overlap +testfiles/v0_to_v1/deprecated_builtins.rego:13: rego_type_error: deprecated built-in function calls in expression: re_match +testfiles/v0_to_v1/deprecated_builtins.rego:14: rego_type_error: deprecated built-in function calls in expression: set_diff +testfiles/v0_to_v1/deprecated_builtins.rego:17: rego_type_error: deprecated built-in function calls in expression: any +testfiles/v0_to_v1/deprecated_builtins.rego:19: rego_type_error: deprecated built-in function calls in expression: any +testfiles/v0_to_v1/deprecated_builtins.rego:21: rego_type_error: deprecated built-in function calls in expression: any +testfiles/v0_to_v1/deprecated_builtins.rego:21: rego_type_error: deprecated built-in function calls in expression: all \ No newline at end of file diff --git a/format/testfiles/rego_v1/duplicate_imports.rego b/format/testfiles/v0_to_v1/duplicate_imports.rego similarity index 100% rename from format/testfiles/rego_v1/duplicate_imports.rego rename to format/testfiles/v0_to_v1/duplicate_imports.rego diff --git a/format/testfiles/v0_to_v1/duplicate_imports.rego.error b/format/testfiles/v0_to_v1/duplicate_imports.rego.error new file mode 100644 index 0000000000..a74cd5f27a --- /dev/null +++ b/format/testfiles/v0_to_v1/duplicate_imports.rego.error @@ -0,0 +1,3 @@ +2 errors occurred: +testfiles/v0_to_v1/duplicate_imports.rego:4: rego_compile_error: import must not shadow import data.foo +testfiles/v0_to_v1/duplicate_imports.rego:5: rego_compile_error: import must not shadow import data.foo \ No newline at end of file diff --git a/format/testfiles/rego_v1/functions.rego b/format/testfiles/v0_to_v1/functions.rego similarity index 100% rename from format/testfiles/rego_v1/functions.rego rename to format/testfiles/v0_to_v1/functions.rego diff --git a/format/testfiles/rego_v1/functions.rego.formatted b/format/testfiles/v0_to_v1/functions.rego.formatted similarity index 100% rename from format/testfiles/rego_v1/functions.rego.formatted rename to format/testfiles/v0_to_v1/functions.rego.formatted diff --git a/format/testfiles/rego_v1/keywords.rego b/format/testfiles/v0_to_v1/keyword_errors.rego similarity index 61% rename from format/testfiles/rego_v1/keywords.rego rename to format/testfiles/v0_to_v1/keyword_errors.rego index b2b791436e..f77a4a4c0a 100644 --- a/format/testfiles/rego_v1/keywords.rego +++ b/format/testfiles/v0_to_v1/keyword_errors.rego @@ -7,3 +7,7 @@ contains := 2 in := 3 every := 4 + +p { + data.foo.contains.bar == 42 +} diff --git a/format/testfiles/v0_to_v1/keyword_errors.rego.error b/format/testfiles/v0_to_v1/keyword_errors.rego.error new file mode 100644 index 0000000000..cc44add905 --- /dev/null +++ b/format/testfiles/v0_to_v1/keyword_errors.rego.error @@ -0,0 +1,5 @@ +4 errors occurred: +testfiles/v0_to_v1/keyword_errors.rego:3: rego_parse_error: if keyword cannot be used for rule name +testfiles/v0_to_v1/keyword_errors.rego:5: rego_parse_error: contains keyword cannot be used for rule name +testfiles/v0_to_v1/keyword_errors.rego:7: rego_parse_error: in keyword cannot be used for rule name +testfiles/v0_to_v1/keyword_errors.rego:9: rego_parse_error: every keyword cannot be used for rule name \ No newline at end of file diff --git a/format/testfiles/v0_to_v1/keywords.rego b/format/testfiles/v0_to_v1/keywords.rego new file mode 100644 index 0000000000..5ad60d0ee2 --- /dev/null +++ b/format/testfiles/v0_to_v1/keywords.rego @@ -0,0 +1,5 @@ +package test.if.contains.in.every + +p { + data.if.contains.in.every == 42 +} diff --git a/format/testfiles/v0_to_v1/keywords.rego.formatted b/format/testfiles/v0_to_v1/keywords.rego.formatted new file mode 100644 index 0000000000..b4bdb7ac1d --- /dev/null +++ b/format/testfiles/v0_to_v1/keywords.rego.formatted @@ -0,0 +1,7 @@ +package test["if"]["contains"]["in"]["every"] + +import rego.v1 + +p if { + data["if"]["contains"]["in"]["every"] == 42 +} diff --git a/format/testfiles/rego_v1/multi_value.rego b/format/testfiles/v0_to_v1/multi_value.rego similarity index 100% rename from format/testfiles/rego_v1/multi_value.rego rename to format/testfiles/v0_to_v1/multi_value.rego diff --git a/format/testfiles/rego_v1/multi_value.rego.formatted b/format/testfiles/v0_to_v1/multi_value.rego.formatted similarity index 100% rename from format/testfiles/rego_v1/multi_value.rego.formatted rename to format/testfiles/v0_to_v1/multi_value.rego.formatted diff --git a/format/testfiles/rego_v1/multi_value_no_future_imports.rego b/format/testfiles/v0_to_v1/multi_value_no_future_imports.rego similarity index 100% rename from format/testfiles/rego_v1/multi_value_no_future_imports.rego rename to format/testfiles/v0_to_v1/multi_value_no_future_imports.rego diff --git a/format/testfiles/rego_v1/multi_value_no_future_imports.rego.formatted b/format/testfiles/v0_to_v1/multi_value_no_future_imports.rego.formatted similarity index 100% rename from format/testfiles/rego_v1/multi_value_no_future_imports.rego.formatted rename to format/testfiles/v0_to_v1/multi_value_no_future_imports.rego.formatted diff --git a/format/testfiles/rego_v1/shadowing.rego b/format/testfiles/v0_to_v1/shadowing.rego similarity index 100% rename from format/testfiles/rego_v1/shadowing.rego rename to format/testfiles/v0_to_v1/shadowing.rego diff --git a/format/testfiles/v0_to_v1/shadowing.rego.error b/format/testfiles/v0_to_v1/shadowing.rego.error new file mode 100644 index 0000000000..b050552aa2 --- /dev/null +++ b/format/testfiles/v0_to_v1/shadowing.rego.error @@ -0,0 +1,13 @@ +12 errors occurred: +testfiles/v0_to_v1/shadowing.rego:3: rego_compile_error: rules must not shadow input (use a different rule name) +testfiles/v0_to_v1/shadowing.rego:5: rego_compile_error: rules must not shadow input (use a different rule name) +testfiles/v0_to_v1/shadowing.rego:7: rego_compile_error: rules must not shadow input (use a different rule name) +testfiles/v0_to_v1/shadowing.rego:9: rego_compile_error: rules must not shadow data (use a different rule name) +testfiles/v0_to_v1/shadowing.rego:11: rego_compile_error: rules must not shadow data (use a different rule name) +testfiles/v0_to_v1/shadowing.rego:13: rego_compile_error: rules must not shadow data (use a different rule name) +testfiles/v0_to_v1/shadowing.rego:28: rego_compile_error: args must not shadow input (use a different variable name) +testfiles/v0_to_v1/shadowing.rego:32: rego_compile_error: args must not shadow data (use a different variable name) +testfiles/v0_to_v1/shadowing.rego:16: rego_compile_error: variables must not shadow input (use a different variable name) +testfiles/v0_to_v1/shadowing.rego:17: rego_compile_error: variables must not shadow data (use a different variable name) +testfiles/v0_to_v1/shadowing.rego:21: rego_compile_error: variables must not shadow input (use a different variable name) +testfiles/v0_to_v1/shadowing.rego:25: rego_compile_error: variables must not shadow data (use a different variable name) \ No newline at end of file diff --git a/format/testfiles/rego_v1/single_value.rego b/format/testfiles/v0_to_v1/single_value.rego similarity index 100% rename from format/testfiles/rego_v1/single_value.rego rename to format/testfiles/v0_to_v1/single_value.rego diff --git a/format/testfiles/rego_v1/single_value.rego.formatted b/format/testfiles/v0_to_v1/single_value.rego.formatted similarity index 100% rename from format/testfiles/rego_v1/single_value.rego.formatted rename to format/testfiles/v0_to_v1/single_value.rego.formatted diff --git a/format/testfiles/rego_v1/single_value_no_future_imports.rego b/format/testfiles/v0_to_v1/single_value_no_future_imports.rego similarity index 100% rename from format/testfiles/rego_v1/single_value_no_future_imports.rego rename to format/testfiles/v0_to_v1/single_value_no_future_imports.rego diff --git a/format/testfiles/rego_v1/single_value_no_future_imports.rego.formatted b/format/testfiles/v0_to_v1/single_value_no_future_imports.rego.formatted similarity index 100% rename from format/testfiles/rego_v1/single_value_no_future_imports.rego.formatted rename to format/testfiles/v0_to_v1/single_value_no_future_imports.rego.formatted diff --git a/format/testfiles/v1/test.rego b/format/testfiles/v1/test.rego new file mode 100644 index 0000000000..9e8eed6a97 --- /dev/null +++ b/format/testfiles/v1/test.rego @@ -0,0 +1,222 @@ +# The blank lines below me should be gone! (except one) + + +# Comment! +package a.b + +# I also, am a comment. +import data.x.y.z +import data.a.b.c # Another comment! +# I belong with data.a, there should be a newline before me. +import data.a +import data.f.g + +default foo = false +foo contains x if { +not x = g + f(x) = 1 + g( + x, "foo" + ) = z +} + +globals = {"foo": "bar", +"fizz": "buzz"} + +partial_obj["x"] = 1 +partial_obj.y = 2 + +partial_obj["z"] = 3 if { + true +} + +partial_set contains "x" + +# Latent comment. + +r = y if { + y = x + split("foo.bar", ".", input.x) with input as {"x": x} + } + +# Comment on else + else = y if { + y = ["howdy"] + x = {"x": { + "y": "z", + }} + a = {"a": { + "b": "c", + }, "b": "c", "c": [1, 2, + 3, 4]} + } + +fn(x) = y if { + y = x +} + +fn_else(x) = 1 if { + true +} # foo +else = +# bar +2 +if { + true +} else = 3 +# baz +if { + false +} + +long(x) = true if { + x = "foo %host" + } + + short(x) if { + x = "bar" + } + +raw_string = `hi\there` +raw_multiline = `this +string + is on +multiple lines` + +fn2([x, y, +z], {"foo": a}) = b if { +split(x, y, c) +trim(a, z, d) # function comment 1 +split(c[0], d, b) +x = sprintf("hello %v", +["world"]) +#function comment 2 +} # function comment 3 + +f contains x if { + x = "hi" +} { # Comment on chain + x = "bye" +} + + import data.foo.bar + import data.bar.foo # data.bar.foo should be first + +p[x] = y if { y = x + y = "foo" + z = { "a": "b", # Comment inside object 1 + "b": "c" , "c": "d", # comment on object entry line + # Comment inside object 2 +"d": "e", +# Comment before closing object brace. +} # Comment on closing object brace. +a = {"a": "b", "c": "d"} +b = [1, 2, 3, 4] +c = [1, 2, +# Comment inside array +3, 4, +5, 6, 7, +8, +# Comment before nested composite. +[ +["foo"], # Comment inside nested composite. +["bar"], # Comment after last element in nested composite. +# Comment before nested composite closing bracket. +], # Comment on nested composite closing bracket. +# Comment before closing array bracket. +] # Comment on closing array bracket. + +d = [1 | b[_]] +e = [1 | split("foo.bar", ".", x); x[_]] +f = [1 | split("foo.bar", ".", x) +x[_]] +g = [1 | +split("foo.bar", ".", x) # comment in array comprehension +x[_] +# inner comment +] + +h = {1 | b[_]} +i = {1 | split("foo.bar", ".", x); x[_]} +j = {1 | split("foo.bar", ".", x) +x[_]} +k = {1 | +split("foo.bar", ".", x) # comment in set comprehension +x[_] + +# inner comment +} + +l = {"foo":1 | b[_]} +m = {y: +x | split("foo.bar", ".", x); y = x[_]} +n = {y: x | split("foo.bar", ".", x) +y = x[_]} +o = {y: x | +split("foo.bar", ".", x) # comment in object comprehension +y = x[_] + +# inner comment +} +} # Comment on rule closing brace + +nested_infix if { + x + 1 + x = y + 2 + plus(x, 1, 2) + plus(x, 1) + y = f(x) + f(x, y) + y = x + 1 + 2 + x = y + # comment + z + x = (a + b) / 2 + f((a+b)/2) + y = q() +} + +expanded_const = true + +partial_obj["why"] = true if { false } + +empty_sets if { + set() + set() # comment at end of set +} + +vardecls if { + some v1, v2,# c1 + v3,v4, # c2 + v5 # c3 +} + +declare1 := 1 + +declare2 := 2 if { false } + +declare3 := {1,2,3} +declare4 := {4,5,6} + +union_object := {"response": (declare3|declare4)} + +union_set := {(declare3|declare4)} + +union_list := [(declare3|declare4)] + +union_set_2 := {(((declare3|declare4)))} + +union_object_multi_line := {"response": ( + declare3 | declare4 +)} + +union_object_key := {(declare3|declare4): "foo"} + +union_object_key_multi_line := {(declare3| +declare4): +"foo" +} + +# more comments! +# more comments! +# more comments! +# more comments! diff --git a/format/testfiles/v1/test.rego.error b/format/testfiles/v1/test.rego.error new file mode 100644 index 0000000000..77482680fb --- /dev/null +++ b/format/testfiles/v1/test.rego.error @@ -0,0 +1,26 @@ +# The blank lines below me should be gone! (except one) + + +# Comment! +package a.b + +# I also, am a comment. +import data.x.y.z +import data.a.b.c # Another comment! +# I belong with data.a, there should be a newline before me. +import data.a +import data.f.g + +default foo = false +foo[x] { +not x = g +} + +globals = {"foo": "bar", +"fizz": "buzz"} + +# Latent comment. + +r = y { + y = x + split("foo.bar", ".", input.x) with input as {"x": x} diff --git a/format/testfiles/v1/test.rego.formatted b/format/testfiles/v1/test.rego.formatted new file mode 100644 index 0000000000..fe72477b17 --- /dev/null +++ b/format/testfiles/v1/test.rego.formatted @@ -0,0 +1,237 @@ +# The blank lines below me should be gone! (except one) + +# Comment! +package a.b + +# I also, am a comment. +import data.a.b.c # Another comment! +import data.x.y.z + +# I belong with data.a, there should be a newline before me. +import data.a +import data.f.g + +default foo := false + +foo contains x if { + not x = g + f(x) = 1 + g(x, "foo") = z +} + +globals := { + "foo": "bar", + "fizz": "buzz", +} + +partial_obj["x"] := 1 + +partial_obj["y"] := 2 + +partial_obj["z"] := 3 + +partial_set contains "x" + +# Latent comment. + +r := y if { + y = x + split("foo.bar", ".", input.x) with input as {"x": x} +} + +# Comment on else +else := y if { + y = ["howdy"] + x = {"x": {"y": "z"}} + a = { + "a": {"b": "c"}, + "b": "c", "c": [ + 1, 2, + 3, 4, + ], + } +} + +fn(x) := y if { + y = x +} + +fn_else(x) := 1 if { + true +} # foo + +else := 2 if { + # bar + + true +} else := 3 if { + # baz + + false +} + +long(x) if { + x = "foo %host" +} + +short(x) if { + x = "bar" +} + +raw_string := `hi\there` + +raw_multiline := `this +string + is on +multiple lines` + +fn2( + [ + x, y, + z, + ], + {"foo": a}, +) := b if { + split(x, y, c) + trim(a, z, d) # function comment 1 + split(c[0], d, b) + x = sprintf( + "hello %v", + ["world"], + ) + #function comment 2 +} # function comment 3 + +f contains x if { + x = "hi" +} + +f contains x if { # Comment on chain + x = "bye" +} + +import data.bar.foo # data.bar.foo should be first +import data.foo.bar + +p[x] := y if { + y = x + y = "foo" + z = { + "a": "b", # Comment inside object 1 + "b": "c", "c": "d", # comment on object entry line + # Comment inside object 2 + "d": "e", + # Comment before closing object brace. + } # Comment on closing object brace. + a = {"a": "b", "c": "d"} + b = [1, 2, 3, 4] + c = [ + 1, 2, + # Comment inside array + 3, 4, + 5, 6, 7, + 8, + # Comment before nested composite. + [ + ["foo"], # Comment inside nested composite. + ["bar"], # Comment after last element in nested composite. + # Comment before nested composite closing bracket. + ], # Comment on nested composite closing bracket. + # Comment before closing array bracket. + ] # Comment on closing array bracket. + + d = [1 | b[_]] + e = [1 | split("foo.bar", ".", x); x[_]] + f = [1 | + split("foo.bar", ".", x) + x[_] + ] + g = [1 | + split("foo.bar", ".", x) # comment in array comprehension + x[_] + # inner comment + ] + + h = {1 | b[_]} + i = {1 | split("foo.bar", ".", x); x[_]} + j = {1 | + split("foo.bar", ".", x) + x[_] + } + k = {1 | + split("foo.bar", ".", x) # comment in set comprehension + x[_] + # inner comment + } + + l = {"foo": 1 | b[_]} + m = {y: x | + split("foo.bar", ".", x) + y = x[_] + } + n = {y: x | + split("foo.bar", ".", x) + y = x[_] + } + o = {y: x | + split("foo.bar", ".", x) # comment in object comprehension + y = x[_] + # inner comment + } +} # Comment on rule closing brace + +nested_infix if { + x + 1 + x = y + 2 + 2 = x + 1 + x + 1 + y = f(x) + f(x, y) + y = (x + 1) + 2 + x = y + z # comment + x = (a + b) / 2 + f((a + b) / 2) + y = q() +} + +expanded_const := true + +partial_obj["why"] if false + +empty_sets if { + set() + set() # comment at end of set +} + +vardecls if { + some v1, v2, # c1 + v3, v4, # c2 + v5 # c3 +} + +declare1 := 1 + +declare2 := 2 if false + +declare3 := {1, 2, 3} + +declare4 := {4, 5, 6} + +union_object := {"response": (declare3 | declare4)} + +union_set := {(declare3 | declare4)} + +union_list := [(declare3 | declare4)] + +union_set_2 := {(declare3 | declare4)} + +union_object_multi_line := {"response": (declare3 | declare4)} + +union_object_key := {(declare3 | declare4): "foo"} + +union_object_key_multi_line := {(declare3 | declare4): "foo"} + +# more comments! +# more comments! +# more comments! +# more comments! diff --git a/format/testfiles/v1/test_assignments.rego b/format/testfiles/v1/test_assignments.rego new file mode 100644 index 0000000000..838be30c89 --- /dev/null +++ b/format/testfiles/v1/test_assignments.rego @@ -0,0 +1,24 @@ +package assignments + +# default value assignment +default a := 1 + +# rule +b := 2 + +# else keyword +c := 3 if { + false +} else := 4 if { + true +} + +# partial rule +d[msg] := 5 if { + msg = [1, 2, 3][_] +} + +# function return value +e := f(6) + +f(x) := x diff --git a/format/testfiles/v1/test_assignments.rego.formatted b/format/testfiles/v1/test_assignments.rego.formatted new file mode 100644 index 0000000000..570cc6b8f4 --- /dev/null +++ b/format/testfiles/v1/test_assignments.rego.formatted @@ -0,0 +1,22 @@ +package assignments + +# default value assignment +default a := 1 + +# rule +b := 2 + +# else keyword +c := 3 if { + false +} else := 4 + +# partial rule +d[msg] := 5 if { + msg = [1, 2, 3][_] +} + +# function return value +e := f(6) + +f(x) := x diff --git a/format/testfiles/v1/test_contains.rego b/format/testfiles/v1/test_contains.rego new file mode 100644 index 0000000000..b0728edde2 --- /dev/null +++ b/format/testfiles/v1/test_contains.rego @@ -0,0 +1,13 @@ +package test["contains"] + +p contains "foo" if { true } + +deny contains msg if { + msg := "foo" +} +deny contains msg if {msg := "bar" } + +# partial objects unchanged +o[k] = v if { k := "ok"; v := "nok" } + +foo["contains"] := 42 diff --git a/format/testfiles/v1/test_contains.rego.formatted b/format/testfiles/v1/test_contains.rego.formatted new file mode 100644 index 0000000000..7f3a09ab3c --- /dev/null +++ b/format/testfiles/v1/test_contains.rego.formatted @@ -0,0 +1,17 @@ +package test["contains"] + +p contains "foo" + +deny contains msg if { + msg := "foo" +} + +deny contains msg if msg := "bar" + +# partial objects unchanged +o[k] := v if { + k := "ok" + v := "nok" +} + +foo["contains"] := 42 diff --git a/format/testfiles/v1/test_contains_if.rego b/format/testfiles/v1/test_contains_if.rego new file mode 100644 index 0000000000..04ab519107 --- /dev/null +++ b/format/testfiles/v1/test_contains_if.rego @@ -0,0 +1,10 @@ +package test["if"] + +q[x] = y if { + y := 10 + x := "ten" +} + +r contains x if { # no if here before + x := "set" +} diff --git a/format/testfiles/v1/test_contains_if.rego.formatted b/format/testfiles/v1/test_contains_if.rego.formatted new file mode 100644 index 0000000000..4375b0d3de --- /dev/null +++ b/format/testfiles/v1/test_contains_if.rego.formatted @@ -0,0 +1,10 @@ +package test["if"] + +q[x] := y if { + y := 10 + x := "ten" +} + +r contains x if { # no if here before + x := "set" +} diff --git a/format/testfiles/v1/test_end_of_rule_comment.rego b/format/testfiles/v1/test_end_of_rule_comment.rego new file mode 100644 index 0000000000..2538e118a8 --- /dev/null +++ b/format/testfiles/v1/test_end_of_rule_comment.rego @@ -0,0 +1,7 @@ +package foo + +bar if { + # before + input.bar + # after +} diff --git a/format/testfiles/v1/test_end_of_rule_comment.rego.formatted b/format/testfiles/v1/test_end_of_rule_comment.rego.formatted new file mode 100644 index 0000000000..2538e118a8 --- /dev/null +++ b/format/testfiles/v1/test_end_of_rule_comment.rego.formatted @@ -0,0 +1,7 @@ +package foo + +bar if { + # before + input.bar + # after +} diff --git a/format/testfiles/v1/test_every.rego b/format/testfiles/v1/test_every.rego new file mode 100644 index 0000000000..c84e3f125a --- /dev/null +++ b/format/testfiles/v1/test_every.rego @@ -0,0 +1,17 @@ +package p + +r if { + every x in [1,3,5] { + is_odd(x) + } + + every x in [1,3,5] { is_odd(x); true } + + every x in [1,3,5] { + is_odd(x) + true + x < 10 + } +} + +is_odd(x) = x % 2 == 0 \ No newline at end of file diff --git a/format/testfiles/v1/test_every.rego.formatted b/format/testfiles/v1/test_every.rego.formatted new file mode 100644 index 0000000000..b3dfa9447a --- /dev/null +++ b/format/testfiles/v1/test_every.rego.formatted @@ -0,0 +1,17 @@ +package p + +r if { + every x in [1, 3, 5] { + is_odd(x) + } + + every x in [1, 3, 5] { is_odd(x); true} + + every x in [1, 3, 5] { + is_odd(x) + true + x < 10 + } +} + +is_odd(x) := (x % 2) == 0 diff --git a/format/testfiles/v1/test_every_with_key.rego b/format/testfiles/v1/test_every_with_key.rego new file mode 100644 index 0000000000..7cbd319361 --- /dev/null +++ b/format/testfiles/v1/test_every_with_key.rego @@ -0,0 +1,19 @@ +package p + +r if { + every i, x in [1,3,5] { + is_odd(x) + i < 10 + } + + every i, x in {"foo": 1, "bar": 3, "baz": 5} { is_odd(x); i != 20 } + + every i, x in [1,3,5] { + is_odd(x) + true + x < 10 + x > i + } +} + +is_odd(x) := x % 2 == 0 \ No newline at end of file diff --git a/format/testfiles/v1/test_every_with_key.rego.formatted b/format/testfiles/v1/test_every_with_key.rego.formatted new file mode 100644 index 0000000000..0685b43d53 --- /dev/null +++ b/format/testfiles/v1/test_every_with_key.rego.formatted @@ -0,0 +1,19 @@ +package p + +r if { + every i, x in [1, 3, 5] { + is_odd(x) + i < 10 + } + + every i, x in {"foo": 1, "bar": 3, "baz": 5} { is_odd(x); i != 20} + + every i, x in [1, 3, 5] { + is_odd(x) + true + x < 10 + x > i + } +} + +is_odd(x) := (x % 2) == 0 diff --git a/format/testfiles/v1/test_fun_args_with_linebreaks.rego b/format/testfiles/v1/test_fun_args_with_linebreaks.rego new file mode 100644 index 0000000000..09cfeecf44 --- /dev/null +++ b/format/testfiles/v1/test_fun_args_with_linebreaks.rego @@ -0,0 +1,16 @@ +package test + +p if { + x := count( + [1, 2, 3] # four + ) + y := concat( + "/", + ["foo", "bar"], + ) + z := concat("/", + [ + "foo", + "bar", + ]) +} \ No newline at end of file diff --git a/format/testfiles/v1/test_fun_args_with_linebreaks.rego.formatted b/format/testfiles/v1/test_fun_args_with_linebreaks.rego.formatted new file mode 100644 index 0000000000..c1c2cd5535 --- /dev/null +++ b/format/testfiles/v1/test_fun_args_with_linebreaks.rego.formatted @@ -0,0 +1,16 @@ +package test + +p if { + x := count([1, 2, 3]) # four + y := concat( + "/", + ["foo", "bar"], + ) + z := concat( + "/", + [ + "foo", + "bar", + ], + ) +} diff --git a/format/testfiles/v1/test_functions.rego b/format/testfiles/v1/test_functions.rego new file mode 100644 index 0000000000..1dc44b9e34 --- /dev/null +++ b/format/testfiles/v1/test_functions.rego @@ -0,0 +1,48 @@ +package p + +f1(x) = x + +f2(x) := x + +f3(1) + +f4(1) if { + true +} + +f5(1) := x if { + x := 5 +} + +f6(x) if { + true +} else := false + +f7(x) := 1 if { + x.key1 +} else := false if { + x.key2 +} else if { + false +} + +f(x) = 1 if { + input.x == x +} { + input.x < 10 +} + +f(_) if { + input.x +} { + input.y +} + +# Non-functions +foo[x] = y if { + x = 1 + y = 2 +} { + x = 3 + y = 4 +} diff --git a/format/testfiles/v1/test_functions.rego.formatted b/format/testfiles/v1/test_functions.rego.formatted new file mode 100644 index 0000000000..529e7cac37 --- /dev/null +++ b/format/testfiles/v1/test_functions.rego.formatted @@ -0,0 +1,52 @@ +package p + +f1(x) := x + +f2(x) := x + +f3(1) + +f4(1) := true + +f5(1) := x if { + x := 5 +} + +f6(x) if { + true +} else := false + +f7(x) := 1 if { + x.key1 +} else := false if { + x.key2 +} else if { + false +} + +f(x) := 1 if { + input.x == x +} + +f(x) := 1 if { + input.x < 10 +} + +f(_) if { + input.x +} + +f(_) if { + input.y +} + +# Non-functions +foo[x] := y if { + x = 1 + y = 2 +} + +foo[x] := y if { + x = 3 + y = 4 +} diff --git a/format/testfiles/v1/test_if.rego b/format/testfiles/v1/test_if.rego new file mode 100644 index 0000000000..02482f9269 --- /dev/null +++ b/format/testfiles/v1/test_if.rego @@ -0,0 +1,25 @@ +package test["if"] + +p if 1 > 0 # shorthand + +p if { 1 > 0 } # longhand one line + +p if { + 1 > 0 # longhand two lines +} + +# same without the comment +p if { + 1 > 0 +} + +p if { # comment one + 1 > 0 # comment two +} + +q[x] = y if { + y := 10 + x := "ten" +} + +r[x] if { x := "set" } # comment diff --git a/format/testfiles/v1/test_if.rego.formatted b/format/testfiles/v1/test_if.rego.formatted new file mode 100644 index 0000000000..90f8b37d1d --- /dev/null +++ b/format/testfiles/v1/test_if.rego.formatted @@ -0,0 +1,25 @@ +package test["if"] + +p if 1 > 0 # shorthand + +p if 1 > 0 # longhand one line + +p if { + 1 > 0 # longhand two lines +} + +# same without the comment +p if { + 1 > 0 +} + +p if { # comment one + 1 > 0 # comment two +} + +q[x] := y if { + y := 10 + x := "ten" +} + +r[x] if x := "set" # comment diff --git a/format/testfiles/v1/test_if_else.rego b/format/testfiles/v1/test_if_else.rego new file mode 100644 index 0000000000..e894902873 --- /dev/null +++ b/format/testfiles/v1/test_if_else.rego @@ -0,0 +1,18 @@ +package test["if"] + +p := 1 if { 1 > 0 } +else := 2 + +q := 1 if { 1 > 0 } else := 2 if { 2 > 1 } + +q := 1 if { + 1 > 0 + 2 > 1 +} else := 2 if { 2 > 1 } + +r := 1 if { + 1 > 0 +} +else := 2 if { + 2 > 1 +} \ No newline at end of file diff --git a/format/testfiles/v1/test_if_else.rego.formatted b/format/testfiles/v1/test_if_else.rego.formatted new file mode 100644 index 0000000000..361bcd6299 --- /dev/null +++ b/format/testfiles/v1/test_if_else.rego.formatted @@ -0,0 +1,22 @@ +package test["if"] + +p := 1 if 1 > 0 + +else := 2 + +q := 1 if 1 > 0 + +else := 2 if 2 > 1 + +q := 1 if { + 1 > 0 + 2 > 1 +} else := 2 if 2 > 1 + +r := 1 if { + 1 > 0 +} + +else := 2 if { + 2 > 1 +} diff --git a/format/testfiles/v1/test_in.rego b/format/testfiles/v1/test_in.rego new file mode 100644 index 0000000000..1397087c80 --- /dev/null +++ b/format/testfiles/v1/test_in.rego @@ -0,0 +1,11 @@ +package test["in"] + +a["in"] := "foo" + +b.c["in"] := "bar" + +c["in"].d := "baz" + +p if { + input["in"] +} diff --git a/format/testfiles/v1/test_in.rego.formatted b/format/testfiles/v1/test_in.rego.formatted new file mode 100644 index 0000000000..1397087c80 --- /dev/null +++ b/format/testfiles/v1/test_in.rego.formatted @@ -0,0 +1,11 @@ +package test["in"] + +a["in"] := "foo" + +b.c["in"] := "bar" + +c["in"].d := "baz" + +p if { + input["in"] +} diff --git a/format/testfiles/v1/test_in_operator_with_all_keywords_import.rego b/format/testfiles/v1/test_in_operator_with_all_keywords_import.rego new file mode 100644 index 0000000000..6c6e6611e8 --- /dev/null +++ b/format/testfiles/v1/test_in_operator_with_all_keywords_import.rego @@ -0,0 +1,8 @@ +package p + +import input.foo + +r if { + internal.member_2(1, [1]) + internal.member_3(0, 1, [1]) +} \ No newline at end of file diff --git a/format/testfiles/v1/test_in_operator_with_all_keywords_import.rego.formatted b/format/testfiles/v1/test_in_operator_with_all_keywords_import.rego.formatted new file mode 100644 index 0000000000..17b92a9c4a --- /dev/null +++ b/format/testfiles/v1/test_in_operator_with_all_keywords_import.rego.formatted @@ -0,0 +1,8 @@ +package p + +import input.foo + +r if { + 1 in [1] + 0, 1 in [1] +} diff --git a/format/testfiles/v1/test_in_operator_with_parenthesis.rego b/format/testfiles/v1/test_in_operator_with_parenthesis.rego new file mode 100644 index 0000000000..ef3a5a342a --- /dev/null +++ b/format/testfiles/v1/test_in_operator_with_parenthesis.rego @@ -0,0 +1,17 @@ +package test + +z if { + { 1, 2 in [2, 2, 2] } + { 1, (1, 2 in [2, 2, 2]) } + { (x, x in [2]) | x := numbers.range(1,10)[_]} + { x: (x, x in [2]) | x := numbers.range(1,10)[_]} + { (x in [2]): 2 | x := numbers.range(1,10)[_]} + { (x, x in [2]): 2 | x := numbers.range(1,10)[_]} + { (1, 2 in [2, 2, 2]) } + { 1, (2 in [2, 2, 2]) } + f(1, 2 in [2, 2]) + g((1, 2 in [2, 2])) +} + +f(_, _) = true +g(_) = true \ No newline at end of file diff --git a/format/testfiles/v1/test_in_operator_with_parenthesis.rego.formatted b/format/testfiles/v1/test_in_operator_with_parenthesis.rego.formatted new file mode 100644 index 0000000000..18eeda9b5d --- /dev/null +++ b/format/testfiles/v1/test_in_operator_with_parenthesis.rego.formatted @@ -0,0 +1,18 @@ +package test + +z if { + {1, 2 in [2, 2, 2]} + {1, (1, 2 in [2, 2, 2])} + {(x, x in [2]) | x := numbers.range(1, 10)[_]} + {x: (x, x in [2]) | x := numbers.range(1, 10)[_]} + {x in [2]: 2 | x := numbers.range(1, 10)[_]} + {(x, x in [2]): 2 | x := numbers.range(1, 10)[_]} + {(1, 2 in [2, 2, 2])} + {1, 2 in [2, 2, 2]} + f(1, 2 in [2, 2]) + g((1, 2 in [2, 2])) +} + +f(_, _) := true + +g(_) := true diff --git a/format/testfiles/v1/test_in_operator_without_import.rego b/format/testfiles/v1/test_in_operator_without_import.rego new file mode 100644 index 0000000000..6c6e6611e8 --- /dev/null +++ b/format/testfiles/v1/test_in_operator_without_import.rego @@ -0,0 +1,8 @@ +package p + +import input.foo + +r if { + internal.member_2(1, [1]) + internal.member_3(0, 1, [1]) +} \ No newline at end of file diff --git a/format/testfiles/v1/test_in_operator_without_import.rego.formatted b/format/testfiles/v1/test_in_operator_without_import.rego.formatted new file mode 100644 index 0000000000..17b92a9c4a --- /dev/null +++ b/format/testfiles/v1/test_in_operator_without_import.rego.formatted @@ -0,0 +1,8 @@ +package p + +import input.foo + +r if { + 1 in [1] + 0, 1 in [1] +} diff --git a/format/testfiles/v1/test_issue_1560.rego b/format/testfiles/v1/test_issue_1560.rego new file mode 100644 index 0000000000..cf7fa9877c --- /dev/null +++ b/format/testfiles/v1/test_issue_1560.rego @@ -0,0 +1,29 @@ +package x + +p if { + symbol + + # comment + some x +} + +p if { + symbol + + # comment + f(x) +} + +p if { + symbol + + # comment + not x +} + +p if { + symbol + + # comment + not f(x) +} \ No newline at end of file diff --git a/format/testfiles/v1/test_issue_1560.rego.formatted b/format/testfiles/v1/test_issue_1560.rego.formatted new file mode 100644 index 0000000000..fb1a36af3f --- /dev/null +++ b/format/testfiles/v1/test_issue_1560.rego.formatted @@ -0,0 +1,29 @@ +package x + +p if { + symbol + + # comment + some x +} + +p if { + symbol + + # comment + f(x) +} + +p if { + symbol + + # comment + not x +} + +p if { + symbol + + # comment + not f(x) +} diff --git a/format/testfiles/v1/test_issue_2299.rego b/format/testfiles/v1/test_issue_2299.rego new file mode 100644 index 0000000000..42aeac114d --- /dev/null +++ b/format/testfiles/v1/test_issue_2299.rego @@ -0,0 +1,40 @@ +package foo + +# Compact else cases +authorize = "allow" if { + input.user == "superuser" +} else = "deny" if { + input.path[0] == "admin" + input.source_network == "external" +} + +# Newline separated else blocks +q = x if { + foo == "bar" +} + +else = y if { + foo == "baz" +} + +else = z if { + foo == "qux" +} + + +# Mixed compact and newline separated +p = x if { + foo == "bar" +} +# some special case +# with lots of comments +# describing it +else = y if { + bar == "foo" +} else = z if { + bar == "bar" +} + +else if { + bar == "baz" +} \ No newline at end of file diff --git a/format/testfiles/v1/test_issue_2299.rego.formatted b/format/testfiles/v1/test_issue_2299.rego.formatted new file mode 100644 index 0000000000..7424e3ee38 --- /dev/null +++ b/format/testfiles/v1/test_issue_2299.rego.formatted @@ -0,0 +1,40 @@ +package foo + +# Compact else cases +authorize := "allow" if { + input.user == "superuser" +} else := "deny" if { + input.path[0] == "admin" + input.source_network == "external" +} + +# Newline separated else blocks +q := x if { + foo == "bar" +} + +else := y if { + foo == "baz" +} + +else := z if { + foo == "qux" +} + +# Mixed compact and newline separated +p := x if { + foo == "bar" +} + +# some special case +# with lots of comments +# describing it +else := y if { + bar == "foo" +} else := z if { + bar == "bar" +} + +else if { + bar == "baz" +} diff --git a/format/testfiles/v1/test_issue_2420.rego b/format/testfiles/v1/test_issue_2420.rego new file mode 100644 index 0000000000..8b784eee03 --- /dev/null +++ b/format/testfiles/v1/test_issue_2420.rego @@ -0,0 +1,11 @@ +package example + +allow if { + some_condition +} + +# some comments + +else if { + another_condition +} diff --git a/format/testfiles/v1/test_issue_2420.rego.formatted b/format/testfiles/v1/test_issue_2420.rego.formatted new file mode 100644 index 0000000000..d9b298c46f --- /dev/null +++ b/format/testfiles/v1/test_issue_2420.rego.formatted @@ -0,0 +1,11 @@ +package example + +allow if { + some_condition +} + +# some comments + +else if { + another_condition +} diff --git a/format/testfiles/v1/test_issue_3836.rego b/format/testfiles/v1/test_issue_3836.rego new file mode 100644 index 0000000000..d42c88ae96 --- /dev/null +++ b/format/testfiles/v1/test_issue_3836.rego @@ -0,0 +1,6 @@ +package testcase + +rule1 = contains( + "", # first comment + "", # second comment +) \ No newline at end of file diff --git a/format/testfiles/v1/test_issue_3836.rego.formatted b/format/testfiles/v1/test_issue_3836.rego.formatted new file mode 100644 index 0000000000..532dfd4bb8 --- /dev/null +++ b/format/testfiles/v1/test_issue_3836.rego.formatted @@ -0,0 +1,6 @@ +package testcase + +rule1 := contains( + "", # first comment + "", # second comment +) diff --git a/format/testfiles/v1/test_issue_3849.rego b/format/testfiles/v1/test_issue_3849.rego new file mode 100644 index 0000000000..db7ff7ba50 --- /dev/null +++ b/format/testfiles/v1/test_issue_3849.rego @@ -0,0 +1,33 @@ +package test_issue_3849 + +test_require_context if { + require_context("monkey", "eat", "banana") with input as { + "principal": {"id": 101, "type": "monkey"}, + "action": "eat", + "entity": {"id": 102, "type": "banana"}, + } +} + +test_contrived if { + allow with input as { + "a": 101, + "b": 101, + "z": 101, + "y": 101, + "x": 101, + "w": 101, + "v": 101, + "u": 101, + "t": 101, + "s": 101, + "r": 101, + "q": 101, + "p": 101, + "o": 101, + "n": 101, + "j": 101, + "k": 101, + "l": 101, + "m": 101, + } +} \ No newline at end of file diff --git a/format/testfiles/v1/test_issue_3849.rego.formatted b/format/testfiles/v1/test_issue_3849.rego.formatted new file mode 100644 index 0000000000..0b4108fe57 --- /dev/null +++ b/format/testfiles/v1/test_issue_3849.rego.formatted @@ -0,0 +1,33 @@ +package test_issue_3849 + +test_require_context if { + require_context("monkey", "eat", "banana") with input as { + "principal": {"id": 101, "type": "monkey"}, + "action": "eat", + "entity": {"id": 102, "type": "banana"}, + } +} + +test_contrived if { + allow with input as { + "a": 101, + "b": 101, + "z": 101, + "y": 101, + "x": 101, + "w": 101, + "v": 101, + "u": 101, + "t": 101, + "s": 101, + "r": 101, + "q": 101, + "p": 101, + "o": 101, + "n": 101, + "j": 101, + "k": 101, + "l": 101, + "m": 101, + } +} diff --git a/format/testfiles/v1/test_issue_4606.rego b/format/testfiles/v1/test_issue_4606.rego new file mode 100644 index 0000000000..1c7fe57da2 --- /dev/null +++ b/format/testfiles/v1/test_issue_4606.rego @@ -0,0 +1,3 @@ +package p + +r if { 1 in [] } \ No newline at end of file diff --git a/format/testfiles/v1/test_issue_4606.rego.formatted b/format/testfiles/v1/test_issue_4606.rego.formatted new file mode 100644 index 0000000000..290a95617b --- /dev/null +++ b/format/testfiles/v1/test_issue_4606.rego.formatted @@ -0,0 +1,3 @@ +package p + +r if 1 in [] diff --git a/format/testfiles/v1/test_issue_5348.rego b/format/testfiles/v1/test_issue_5348.rego new file mode 100644 index 0000000000..4c128fefe9 --- /dev/null +++ b/format/testfiles/v1/test_issue_5348.rego @@ -0,0 +1,5 @@ +package p + +f(x) if { + true +} else := false diff --git a/format/testfiles/v1/test_issue_5348.rego.formatted b/format/testfiles/v1/test_issue_5348.rego.formatted new file mode 100644 index 0000000000..4c128fefe9 --- /dev/null +++ b/format/testfiles/v1/test_issue_5348.rego.formatted @@ -0,0 +1,5 @@ +package p + +f(x) if { + true +} else := false diff --git a/format/testfiles/v1/test_issue_5449.rego b/format/testfiles/v1/test_issue_5449.rego new file mode 100644 index 0000000000..98b842401a --- /dev/null +++ b/format/testfiles/v1/test_issue_5449.rego @@ -0,0 +1,3 @@ +package demo + +foo["bar"] = "baz" if { input } diff --git a/format/testfiles/v1/test_issue_5449.rego.formatted b/format/testfiles/v1/test_issue_5449.rego.formatted new file mode 100644 index 0000000000..8596fdab1e --- /dev/null +++ b/format/testfiles/v1/test_issue_5449.rego.formatted @@ -0,0 +1,3 @@ +package demo + +foo["bar"] := "baz" if input diff --git a/format/testfiles/v1/test_issue_5449_with_contains_ref_rule.rego b/format/testfiles/v1/test_issue_5449_with_contains_ref_rule.rego new file mode 100644 index 0000000000..e5ab28992a --- /dev/null +++ b/format/testfiles/v1/test_issue_5449_with_contains_ref_rule.rego @@ -0,0 +1,7 @@ +# This is the same as test_issue_5449.rego, but with another rule +# that gives the formatter the assurance that using ref rules is OK +package demo + +foo["bar"] = "baz" if { input } + +a.deep contains "ref" diff --git a/format/testfiles/v1/test_issue_5449_with_contains_ref_rule.rego.formatted b/format/testfiles/v1/test_issue_5449_with_contains_ref_rule.rego.formatted new file mode 100644 index 0000000000..0e46653409 --- /dev/null +++ b/format/testfiles/v1/test_issue_5449_with_contains_ref_rule.rego.formatted @@ -0,0 +1,7 @@ +# This is the same as test_issue_5449.rego, but with another rule +# that gives the formatter the assurance that using ref rules is OK +package demo + +foo.bar := "baz" if input + +a.deep contains "ref" diff --git a/format/testfiles/v1/test_issue_5449_with_ref_rule.rego b/format/testfiles/v1/test_issue_5449_with_ref_rule.rego new file mode 100644 index 0000000000..9d6eac8a69 --- /dev/null +++ b/format/testfiles/v1/test_issue_5449_with_ref_rule.rego @@ -0,0 +1,7 @@ +# This is the same as test_issue_5449.rego, but with a rule that gives +# the formatter the assurance that using ref rules is OK +package demo + +foo["bar"] = "baz" if { input } + +a.deep.ref := true diff --git a/format/testfiles/v1/test_issue_5449_with_ref_rule.rego.formatted b/format/testfiles/v1/test_issue_5449_with_ref_rule.rego.formatted new file mode 100644 index 0000000000..b0a979c993 --- /dev/null +++ b/format/testfiles/v1/test_issue_5449_with_ref_rule.rego.formatted @@ -0,0 +1,7 @@ +# This is the same as test_issue_5449.rego, but with a rule that gives +# the formatter the assurance that using ref rules is OK +package demo + +foo.bar := "baz" if input + +a.deep.ref := true diff --git a/format/testfiles/v1/test_issue_5537_with_comprehension.rego b/format/testfiles/v1/test_issue_5537_with_comprehension.rego new file mode 100644 index 0000000000..abe0330eae --- /dev/null +++ b/format/testfiles/v1/test_issue_5537_with_comprehension.rego @@ -0,0 +1,3 @@ +package p + +array := [(input.thing[i] == input.other[i]) | true] diff --git a/format/testfiles/v1/test_issue_5537_with_comprehension.rego.formatted b/format/testfiles/v1/test_issue_5537_with_comprehension.rego.formatted new file mode 100644 index 0000000000..abe0330eae --- /dev/null +++ b/format/testfiles/v1/test_issue_5537_with_comprehension.rego.formatted @@ -0,0 +1,3 @@ +package p + +array := [(input.thing[i] == input.other[i]) | true] diff --git a/format/testfiles/v1/test_issue_5537_with_ref.rego b/format/testfiles/v1/test_issue_5537_with_ref.rego new file mode 100644 index 0000000000..93d4f1934a --- /dev/null +++ b/format/testfiles/v1/test_issue_5537_with_ref.rego @@ -0,0 +1,8 @@ +package p + +first := {"one", "two"} +second := {"two", "three"} + +example contains msg if { + msg := (first | second)[_] +} diff --git a/format/testfiles/v1/test_issue_5537_with_ref.rego.formatted b/format/testfiles/v1/test_issue_5537_with_ref.rego.formatted new file mode 100644 index 0000000000..24c68846a5 --- /dev/null +++ b/format/testfiles/v1/test_issue_5537_with_ref.rego.formatted @@ -0,0 +1,9 @@ +package p + +first := {"one", "two"} + +second := {"two", "three"} + +example contains msg if { + msg := (first | second)[_] +} diff --git a/format/testfiles/v1/test_issue_5798.rego b/format/testfiles/v1/test_issue_5798.rego new file mode 100644 index 0000000000..b351384204 --- /dev/null +++ b/format/testfiles/v1/test_issue_5798.rego @@ -0,0 +1,9 @@ +package test + +rule01 = fail +if { + fail = { # this + x | # panics + set[x]; f(x) + } +} diff --git a/format/testfiles/v1/test_issue_5798.rego.formatted b/format/testfiles/v1/test_issue_5798.rego.formatted new file mode 100644 index 0000000000..df21ad6b80 --- /dev/null +++ b/format/testfiles/v1/test_issue_5798.rego.formatted @@ -0,0 +1,9 @@ +package test + +rule01 := fail if { + fail = { # this + x | # panics + set[x] + f(x) + } +} diff --git a/format/testfiles/v1/test_issue_6161.rego b/format/testfiles/v1/test_issue_6161.rego new file mode 100644 index 0000000000..e78a9bb48c --- /dev/null +++ b/format/testfiles/v1/test_issue_6161.rego @@ -0,0 +1,8 @@ +package p + +# this is a comment with trailing whitespace + +allow if { + # another comment with trailing whitespace + 1 == 1 +} diff --git a/format/testfiles/v1/test_issue_6161.rego.formatted b/format/testfiles/v1/test_issue_6161.rego.formatted new file mode 100644 index 0000000000..e0c5d68b2c --- /dev/null +++ b/format/testfiles/v1/test_issue_6161.rego.formatted @@ -0,0 +1,8 @@ +package p + +# this is a comment with trailing whitespace + +allow if { + # another comment with trailing whitespace + 1 == 1 +} diff --git a/format/testfiles/v1/test_ref_heads.rego b/format/testfiles/v1/test_ref_heads.rego new file mode 100644 index 0000000000..fbdaa8778d --- /dev/null +++ b/format/testfiles/v1/test_ref_heads.rego @@ -0,0 +1,18 @@ +package test + +a.b.c = "d" if true +a.b.e = "f" if true +a.b.g contains x if some x in numbers.range(1, 3) +a.b.h[x] = 1 if x := "one" + +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 if { 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] if { 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" } diff --git a/format/testfiles/v1/test_ref_heads.rego.formatted b/format/testfiles/v1/test_ref_heads.rego.formatted new file mode 100644 index 0000000000..acce882eb6 --- /dev/null +++ b/format/testfiles/v1/test_ref_heads.rego.formatted @@ -0,0 +1,40 @@ +package test + +a.b.c := "d" + +a.b.e := "f" + +a.b.g contains x if some x in numbers.range(1, 3) + +a.b.h[x] := 1 if x := "one" + +q[1] := y + +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" +} diff --git a/format/testfiles/v1/test_rego_v1.rego b/format/testfiles/v1/test_rego_v1.rego new file mode 100644 index 0000000000..8ebb10058a --- /dev/null +++ b/format/testfiles/v1/test_rego_v1.rego @@ -0,0 +1,26 @@ +package example + +import rego.v1 # import will be dropped + +# R1: constant +a := 1 + +# R2: set +b contains "c" + +# R3: boolean +c.d.e := true + +# R4: set +d contains x if { + x := "e" +} + +# R5: boolean +e.f[x] if { + x := "g" +} + +f if true in [true, false] + +g if every x in [1, 2, 3] { x < 4 } diff --git a/format/testfiles/v1/test_rego_v1.rego.formatted b/format/testfiles/v1/test_rego_v1.rego.formatted new file mode 100644 index 0000000000..5583e0e662 --- /dev/null +++ b/format/testfiles/v1/test_rego_v1.rego.formatted @@ -0,0 +1,26 @@ +package example + +# import will be dropped + +# R1: constant +a := 1 + +# R2: set +b contains "c" + +# R3: boolean +c.d.e := true + +# R4: set +d contains x if { + x := "e" +} + +# R5: boolean +e.f[x] if { + x := "g" +} + +f if true in [true, false] + +g if every x in [1, 2, 3] { x < 4 } diff --git a/format/testfiles/v1/test_with.rego b/format/testfiles/v1/test_with.rego new file mode 100644 index 0000000000..d7c775b583 --- /dev/null +++ b/format/testfiles/v1/test_with.rego @@ -0,0 +1,38 @@ +package p + +single_line_with if { + fn(1) with input.a as "a" +} + +multi_line_with if { + fn(1) with input.a as "a" + with input.b as "b" + with input.c as { + "foo": "bar", + } + with input.d as [ + 1, + 2, + 3] +} + +mixed_new_lines_with if { + true with input.a as "a" + with input.b as "b" with input.c as "c" + with input.d as "d" +} + +mock_f(_) = 123 + +func_replacements if { + count(array.concat(input.x, [])) with input.x as "foo" + with array.concat as true + with count as mock_f +} + +original(x) = x+1 + +more_func_replacements if { + original(1) with original as mock_f + original(1) with original as 1234 +} \ No newline at end of file diff --git a/format/testfiles/v1/test_with.rego.formatted b/format/testfiles/v1/test_with.rego.formatted new file mode 100644 index 0000000000..eb960c7019 --- /dev/null +++ b/format/testfiles/v1/test_with.rego.formatted @@ -0,0 +1,37 @@ +package p + +single_line_with if { + fn(1) with input.a as "a" +} + +multi_line_with if { + fn(1) with input.a as "a" + with input.b as "b" + with input.c as {"foo": "bar"} + with input.d as [ + 1, + 2, + 3, + ] +} + +mixed_new_lines_with if { + true with input.a as "a" + with input.b as "b" with input.c as "c" + with input.d as "d" +} + +mock_f(_) := 123 + +func_replacements if { + count(array.concat(input.x, [])) with input.x as "foo" + with array.concat as true + with count as mock_f +} + +original(x) := x + 1 + +more_func_replacements if { + original(1) with original as mock_f + original(1) with original as 1234 +} diff --git a/internal/future/filter_imports.go b/internal/future/filter_imports.go index 2863aad4e9..cf5721101a 100644 --- a/internal/future/filter_imports.go +++ b/internal/future/filter_imports.go @@ -35,3 +35,15 @@ func IsFutureKeyword(imp *ast.Import, kw string) bool { path[1].Equal(ast.StringTerm("keywords")) && path[2].Equal(ast.StringTerm(kw)) } + +func WhichFutureKeyword(imp *ast.Import) (string, bool) { + path := imp.Path.Value.(ast.Ref) + if len(path) == 3 && + ast.FutureRootDocument.Equal(path[0]) && + path[1].Equal(ast.StringTerm("keywords")) { + if str, ok := path[2].Value.(ast.String); ok { + return string(str), true + } + } + return "", false +}