Skip to content

Commit ad0834f

Browse files
authored
Various small fixes (#1562)
- Fix issue with determining Rego version for some paths - Some Rego improvements Signed-off-by: Anders Eknert <anders@styra.com>
1 parent ba2e2a1 commit ad0834f

7 files changed

Lines changed: 33 additions & 45 deletions

File tree

bundle/regal/ast/search.rego

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ find_ref_vars(value) := [var | # regal ignore:narrow-argument
5050
]
5151

5252
# one or two vars declared via `every`, i.e. `every x in y {}`
53-
# or `every`, i.e. `every x, y in y {}`
53+
# or `every`, i.e. `every x, y in z {}`
5454
_find_every_vars(value) := array.concat(key_var, val_var) if {
5555
key_var := [value.key |
5656
value.key.type == "var"
@@ -256,7 +256,7 @@ found.comprehensions[rule_index] contains value if {
256256
}
257257

258258
# METADATA
259-
# description: set containing all negated expressions in input AST
259+
# description: set containing all expressions in input AST
260260
found.expressions[rule_index] contains value if {
261261
some i, rule_index in rule_index_strings
262262
some node in ["head", "body", "else"]

bundle/regal/lsp/completion/providers/locals/locals.rego

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,15 @@ items contains item if {
4343
_excluded(line, position) if _function_args_position(substring(line, 0, position.character))
4444

4545
_function_args_position(text) if {
46-
text == trim_left(text, " \t")
4746
contains(text, "(")
4847
not contains(text, "=")
48+
text == trim_left(text, " \t")
4949
}
5050

5151
default _same_line_loop_vars(_) := []
5252

53-
_same_line_loop_vars(line) := d if {
54-
expr := trim_space(line)
55-
56-
strings.any_prefix_match(expr, {"some", "every"})
53+
_same_line_loop_vars(line) := vars if {
54+
regex.match(`^\s*(some|every)`, line)
5755

58-
# ---------------------------------------------------> "some k, v in coll"
59-
a := substring(expr, 0, indexof(expr, " in")) # -----> "some k, v"
60-
b := trim_prefix(trim_prefix(a, "some"), "every") # -> "k, v"
61-
c := replace(b, " ", "") # --------------------------> "k,v"
62-
d := split(c, ",") # --------------------------------> [k, v] or [v]
56+
vars := split(regex.replace(line, `(?:\s*(?:some|every)\s+)(\w+)(?:,?\s*)(\w+)?(?:\s+.*)in.*`, "$1,$2"), ",")
6357
}

bundle/regal/rules/idiomatic/directory-package-mismatch/directory_package_mismatch.rego

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,30 @@ import data.regal.util
1414
notices contains result.notice(rego.metadata.chain()) if "no_filename" in config.capabilities.special
1515

1616
report contains violation if {
17+
parts := split(input.regal.file.abs, input.regal.environment.path_separator)
18+
file_path_values := array.slice(parts, 0, count(parts) - 1)
19+
1720
# get the last n components from file path, where n == count(_pkg_path_values)
1821
file_path_length_matched := array.slice(
19-
_file_path_values,
20-
count(_file_path_values) - count(_pkg_path_values),
21-
count(_file_path_values),
22+
file_path_values,
23+
count(file_path_values) - count(_pkg_path_values),
24+
count(file_path_values),
2225
)
2326

2427
file_path_length_matched != _pkg_path_values
2528

26-
violation := result.fail(
27-
rego.metadata.chain(),
28-
# skip the "data" part of the path, as it has no location
29-
result.ranged_from_ref(util.rest(input["package"].path)),
30-
)
29+
# skip the "data" part of the path, as it has no location
30+
violation := result.fail(rego.metadata.chain(), result.ranged_from_ref(util.rest(input["package"].path)))
3131
}
3232

33-
_pkg_path_values := ast.package_path if not config.rules.idiomatic["directory-package-mismatch"]["exclude-test-suffix"]
34-
3533
_pkg_path_values := without_test_suffix if {
3634
config.rules.idiomatic["directory-package-mismatch"]["exclude-test-suffix"] == true
3735

36+
name := regal.last(ast.package_path)
37+
endswith(name, "_test")
38+
3839
without_test_suffix := array.concat(
3940
array.slice(ast.package_path, 0, count(ast.package_path) - 1),
40-
[trim_suffix(regal.last(ast.package_path), "_test")],
41+
[trim_suffix(name, "_test")],
4142
)
42-
}
43-
44-
_file_path_values := array.slice(parts, 0, count(parts) - 1) if {
45-
parts := split(input.regal.file.abs, input.regal.environment.path_separator)
46-
}
43+
} else := ast.package_path

e2e/cli_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ func TestLintAggregateIgnoreDirective(t *testing.T) {
577577
}
578578

579579
if rep.Summary.NumViolations != 2 {
580-
t.Errorf("expected 2 violations, got %d", rep.Summary.NumViolations)
580+
t.Fatalf("expected 2 violations, got %d", rep.Summary.NumViolations)
581581
}
582582

583583
if rep.Summary.NumViolations == 0 {

pkg/linter/linter_test.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ import data.unresolved`,
728728
}
729729
}
730730

731-
// 968805333 ns/op 2970674744 B/op 56876788 allocs/op
731+
// 968805333 ns/op 2970674744 B/op 56876788 allocs/op - main
732732
// ...
733733
func BenchmarkRegalLintingItself(b *testing.B) {
734734
conf, err := config.FromPath(filepath.Join("..", "..", ".regal", "config.yaml"))
@@ -741,9 +741,6 @@ func BenchmarkRegalLintingItself(b *testing.B) {
741741
WithBaseCache(cache.NewBaseCache()).
742742
WithUserConfig(conf)
743743

744-
b.ResetTimer()
745-
b.ReportAllocs()
746-
747744
var rep report.Report
748745

749746
for range b.N {
@@ -773,9 +770,6 @@ func BenchmarkRegalLintingItselfPrepareOnce(b *testing.B) {
773770
WithUserConfig(conf).
774771
MustPrepare(ctx)
775772

776-
b.ResetTimer()
777-
b.ReportAllocs()
778-
779773
var rep report.Report
780774

781775
for range b.N {
@@ -790,17 +784,14 @@ func BenchmarkRegalLintingItselfPrepareOnce(b *testing.B) {
790784
}
791785
}
792786

793-
// 164353357 ns/op 455624941 B/op 8560088 allocs/op
787+
// 6 168875708 ns/op 455586606 B/op 8537889 allocs/op
794788
// ...
795789
func BenchmarkRegalNoEnabledRules(b *testing.B) {
796790
linter := NewLinter().
797791
WithInputPaths([]string{"../../bundle"}).
798792
WithBaseCache(cache.NewBaseCache()).
799793
WithDisableAll(true)
800794

801-
b.ResetTimer()
802-
b.ReportAllocs()
803-
804795
var err error
805796

806797
var rep report.Report
@@ -826,9 +817,6 @@ func BenchmarkRegalNoEnabledRulesPrepareOnce(b *testing.B) {
826817
WithDisableAll(true).
827818
MustPrepare(context.Background())
828819

829-
b.ResetTimer()
830-
b.ReportAllocs()
831-
832820
var err error
833821

834822
var rep report.Report

pkg/rules/rules.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,12 @@ func RegoVersionFromVersionsMap(
152152

153153
var longestMatch int
154154

155-
dir := filepath.Dir(filename)
155+
dir := path.Join("/", filepath.Dir(filename))
156156

157157
for versionedDir := range versionsMap {
158158
matchingVersionedDir := path.Join("/", versionedDir, "/")
159159

160-
if strings.HasPrefix(dir+"/", matchingVersionedDir) {
160+
if strings.HasPrefix(dir, matchingVersionedDir) {
161161
// >= as the versioned dir might be "" for the project root
162162
if len(versionedDir) >= longestMatch {
163163
longestMatch = len(versionedDir)

pkg/rules/rules_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ func TestRegoVersionFromVersionsMap(t *testing.T) {
7777
Filename: "/foo/bar.rego",
7878
ExpectedVersion: ast.RegoV1,
7979
},
80+
"file has version from current dir (no leading slash)": {
81+
VersionsMap: map[string]ast.RegoVersion{
82+
"foo": ast.RegoV1,
83+
"bar": ast.RegoV0,
84+
"unknown": ast.RegoUndefined,
85+
},
86+
Filename: "/foo/bar.rego",
87+
ExpectedVersion: ast.RegoV1,
88+
},
8089
"file has version from parent dir": {
8190
VersionsMap: map[string]ast.RegoVersion{
8291
"foo": ast.RegoV1,

0 commit comments

Comments
 (0)