From b107be6dcdfbb63636454463af9a75b6026abcd6 Mon Sep 17 00:00:00 2001 From: Anders Eknert Date: Sat, 6 Jul 2024 00:27:17 +0200 Subject: [PATCH] Fix some `prefer-snake-case` violations not getting reported If ref part is missing loction, default to location of rule. Fixes #899 Signed-off-by: Anders Eknert --- bundle/regal/rules/style/prefer_snake_case.rego | 12 +++++++++--- bundle/regal/rules/style/prefer_snake_case_test.rego | 6 ++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/bundle/regal/rules/style/prefer_snake_case.rego b/bundle/regal/rules/style/prefer_snake_case.rego index c38794ff..b27fb313 100644 --- a/bundle/regal/rules/style/prefer_snake_case.rego +++ b/bundle/regal/rules/style/prefer_snake_case.rego @@ -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 { @@ -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 diff --git a/bundle/regal/rules/style/prefer_snake_case_test.rego b/bundle/regal/rules/style/prefer_snake_case_test.rego index 28de8480..5dd91070 100644 --- a/bundle/regal/rules/style/prefer_snake_case_test.rego +++ b/bundle/regal/rules/style/prefer_snake_case_test.rego @@ -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",