Skip to content

Commit

Permalink
refactor: one rule, one file
Browse files Browse the repository at this point in the history
Having all rules lumped into a single file only per
category got to be a bit messy eventually. This change
uses one file per rule, making it easier to both find and
to work with rules in isolation — hopefully making it easier
to contribute new ones too.

There is still a few things I would like to fix, like having
unit tests actually be unit tests, and not as it currently stands
that they test an input against *all* rules in the category...
but this change is big enough as it is, so I'm deferring that to
a future PR.

Fixes #144

Signed-off-by: Anders Eknert <anders@styra.com>
  • Loading branch information
anderseknert committed Jun 21, 2023
1 parent aab76ff commit 509c415
Show file tree
Hide file tree
Showing 64 changed files with 2,257 additions and 1,870 deletions.
30 changes: 22 additions & 8 deletions bundle/regal/ast.rego
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,21 @@ import future.keywords.in

import data.regal.opa

_builtin_names := object.keys(opa.builtins)
builtin_names := object.keys(opa.builtins)

# METADATA
# description: |
# provide the package name / path as originally declared in the
# input policy, so "package foo.bar" would return "foo.bar"
package_name := concat(".", [path.value |
some i, path in input["package"].path
i > 0
])

tests := [rule |
some rule in input.rules
startswith(rule.head.name, "test_")
]

# METADATA
# description: parse provided snippet with a generic package declaration added
Expand Down Expand Up @@ -40,7 +54,7 @@ _find_nested_vars(obj) := [value |
# simple assignment, i.e. `x := 100` returns `x`
# always returns a single var, but wrapped in an
# array for consistency
_find_assign_vars(path, value) := var if {
_find_assign_vars(_, value) := var if {
value[1].type == "var"
var := [value[1]]
}
Expand All @@ -49,27 +63,27 @@ _find_assign_vars(path, value) := var if {
# [a, b, c] := [1, 2, 3]
# or
# {a: b} := {"foo": "bar"}
_find_assign_vars(path, value) := var if {
_find_assign_vars(_, value) := var if {
value[1].type in {"array", "object"}
var := _find_nested_vars(value[1])
}

# var declared via `some`, i.e. `some x` or `some x, y`
_find_some_decl_vars(path, value) := [v |
_find_some_decl_vars(_, value) := [v |
some v in value
v.type == "var"
]

# single var declared via `some in`, i.e. `some x in y`
_find_some_in_decl_vars(path, value) := var if {
_find_some_in_decl_vars(_, value) := var if {
arr := value[0].value
count(arr) == 3

var := _find_nested_vars(arr[1])
}

# two vars declared via `some in`, i.e. `some x, y in z`
_find_some_in_decl_vars(path, value) := var if {
_find_some_in_decl_vars(_, value) := var if {
arr := value[0].value
count(arr) == 4

Expand All @@ -81,7 +95,7 @@ _find_some_in_decl_vars(path, value) := var if {

# one or two vars declared via `every`, i.e. `every x in y {}`
# or `every`, i.e. `every x, y in y {}`
_find_every_vars(path, value) := var if {
_find_every_vars(_, value) := var if {
key_var := [v | v := value.key; v.type == "var"; indexof(v.value, "$") == -1]
val_var := [v | v := value.value; v.type == "var"; indexof(v.value, "$") == -1]

Expand Down Expand Up @@ -133,7 +147,7 @@ find_builtin_calls(node) := [value |

value[0].type == "ref"
value[0].value[0].type == "var"
value[0].value[0].value in _builtin_names
value[0].value[0].value in builtin_names
]

# METADATA
Expand Down
197 changes: 0 additions & 197 deletions bundle/regal/rules/bugs/bugs.rego

This file was deleted.

Loading

0 comments on commit 509c415

Please sign in to comment.