Skip to content

Commit

Permalink
Add end location to metasyntactic-variable violations (#977)
Browse files Browse the repository at this point in the history
Signed-off-by: Anders Eknert <[email protected]>
  • Loading branch information
anderseknert committed Aug 12, 2024
1 parent f2ac449 commit 3236efd
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 10 deletions.
8 changes: 4 additions & 4 deletions bundle/regal/rules/testing/metasyntactic_variable.rego
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ metasyntactic := {

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

lower(ref.value) in metasyntactic
lower(part.value) in metasyntactic

# In case we have chained rule bodies — only flag the location where we have an actual name:
# foo {
Expand All @@ -38,7 +38,7 @@ report contains violation if {
# }
not ast.is_chained_rule_body(rule, input.regal.file.lines)

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

report contains violation if {
Expand All @@ -49,5 +49,5 @@ report contains violation if {

ast.is_output_var(input.rules[to_number(i)], var, var.location)

violation := result.fail(rego.metadata.chain(), result.location(var))
violation := result.fail(rego.metadata.chain(), result.ranged_location_from_text(var))
}
48 changes: 42 additions & 6 deletions bundle/regal/rules/testing/metasyntactic_variable_test.rego
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ test_fail_rule_named_foo if {
module := ast.policy("foo := true")

r := rule.report with input as module
r == {expected_with_location({"col": 1, "file": "policy.rego", "row": 3, "text": "foo := true"})}
r == {expected_with_location({
"col": 1,
"file": "policy.rego",
"row": 3,
"text": "foo := true",
"end": {"col": 4, "row": 3},
})}
}

test_fail_metasyntactic_vars if {
Expand All @@ -22,8 +28,20 @@ test_fail_metasyntactic_vars if {

r := rule.report with input as module
r == {
expected_with_location({"col": 3, "file": "policy.rego", "row": 4, "text": "\t\tfooBar := true"}),
expected_with_location({"col": 9, "file": "policy.rego", "row": 5, "text": "\t\tinput[baz]"}),
expected_with_location({
"col": 3,
"file": "policy.rego",
"row": 4,
"text": "\t\tfooBar := true",
"end": {"col": 9, "row": 4},
}),
expected_with_location({
"col": 9,
"file": "policy.rego",
"row": 5,
"text": "\t\tinput[baz]",
"end": {"col": 12, "row": 5},
}),
}
}

Expand All @@ -32,9 +50,27 @@ test_fail_metasyntactic_vars_ref_head_strings if {

r := rule.report with input as module
r == {
expected_with_location({"col": 1, "file": "policy.rego", "row": 3, "text": "foo.a.BAR.b.C.baz := true"}),
expected_with_location({"col": 7, "file": "policy.rego", "row": 3, "text": "foo.a.BAR.b.C.baz := true"}),
expected_with_location({"col": 15, "file": "policy.rego", "row": 3, "text": "foo.a.BAR.b.C.baz := true"}),
expected_with_location({
"col": 1,
"file": "policy.rego",
"row": 3,
"text": "foo.a.BAR.b.C.baz := true",
"end": {"col": 4, "row": 3},
}),
expected_with_location({
"col": 7,
"file": "policy.rego",
"row": 3,
"text": "foo.a.BAR.b.C.baz := true",
"end": {"col": 10, "row": 3},
}),
expected_with_location({
"col": 15,
"file": "policy.rego",
"row": 3,
"text": "foo.a.BAR.b.C.baz := true",
"end": {"col": 18, "row": 3},
}),
}
}

Expand Down

0 comments on commit 3236efd

Please sign in to comment.