Skip to content

Commit

Permalink
Fix some prefer-snake-case violations not getting reported
Browse files Browse the repository at this point in the history
If ref part is missing loction, default to location of rule.

Fixes #899

Signed-off-by: Anders Eknert <[email protected]>
  • Loading branch information
anderseknert committed Jul 5, 2024
1 parent bb790c5 commit b595aff
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
12 changes: 9 additions & 3 deletions bundle/regal/rules/style/prefer_snake_case.rego
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import data.regal.util

report contains violation if {
some rule in input.rules
some ref in ast.named_refs(rule.head.ref)
not util.is_snake_case(ref.value)
some part in ast.named_refs(rule.head.ref)
not util.is_snake_case(part.value)

violation := result.fail(rego.metadata.chain(), result.location(ref))
violation := result.fail(rego.metadata.chain(), _location(rule, part))
}

report contains violation if {
Expand All @@ -22,3 +22,9 @@ report contains violation if {

violation := result.fail(rego.metadata.chain(), result.location(var))
}

_location(_, part) := result.location(part) if part.location

# workaround until https://github.com/open-policy-agent/opa/issues/6860
# is fixed and we can trust that location is included for all ref parts
_location(rule, part) := result.location(rule.head) if not part.location
5 changes: 5 additions & 0 deletions bundle/regal/rules/style/prefer_snake_case_test.rego
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ test_success_snake_cased_every if {
r == set()
}

test_fail_location_provided_even_when_not_in_ref if {
r := rule.report with input as ast.with_rego_v1(`foo.Bar := true`)
r == expected_with_locations([{"col": 5, "file": "policy.rego", "row": 5, "text": "foo.Bar := true"}])
}

expected_with_locations(locations) := {with_location |
expected := {
"category": "style",
Expand Down

0 comments on commit b595aff

Please sign in to comment.