Skip to content

Commit

Permalink
Fix some prefer-snake-case violations not getting reported (StyraIn…
Browse files Browse the repository at this point in the history
…c#900)

If ref part is missing loction, default to location of rule.

Fixes StyraInc#899

Signed-off-by: Anders Eknert <anders@styra.com>
  • Loading branch information
anderseknert authored and srenatus committed Oct 1, 2024
1 parent 1c8e86a commit 5d2cfbe
Show file tree
Hide file tree
Showing 2 changed files with 15 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
6 changes: 6 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,12 @@ test_success_snake_cased_every if {
r == set()
}

# https://github.com/open-policy-agent/opa/issues/6860
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 5d2cfbe

Please sign in to comment.