Skip to content

Commit

Permalink
Add end location to non-raw-regex-pattern violations (StyraInc#980)
Browse files Browse the repository at this point in the history
This was another one that annoyed me recently, so fixed it :)

Signed-off-by: Anders Eknert <anders@styra.com>
  • Loading branch information
anderseknert authored and srenatus committed Oct 1, 2024
1 parent 77c3440 commit bab1f91
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
2 changes: 1 addition & 1 deletion bundle/regal/rules/idiomatic/non_raw_regex_pattern.rego
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,5 @@ report contains violation if {

chr == `"`

violation := result.fail(rego.metadata.chain(), result.location(value[pos]))
violation := result.fail(rego.metadata.chain(), result.ranged_location_from_text(value[pos]))
}
24 changes: 21 additions & 3 deletions bundle/regal/rules/idiomatic/non_raw_regex_pattern_test.rego
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ test_fail_non_raw_rule_head if {
"category": "idiomatic",
"description": "Use raw strings for regex patterns",
"level": "error",
"location": {"col": 18, "file": "policy.rego", "row": 3, "text": "x := regex.match(\"[0-9]+\", \"1\")"},
"location": {
"col": 18,
"file": "policy.rego",
"row": 3,
"text": "x := regex.match(\"[0-9]+\", \"1\")",
"end": {"col": 26, "row": 3},
},
"related_resources": [{
"description": "documentation",
"ref": config.docs.resolve_url("$baseUrl/$category/non-raw-regex-pattern", "idiomatic"),
Expand All @@ -33,7 +39,13 @@ test_fail_non_raw_rule_body if {
"category": "idiomatic",
"description": "Use raw strings for regex patterns",
"level": "error",
"location": {"col": 18, "file": "policy.rego", "row": 4, "text": "\t\tregex.is_valid(\"[0-9]+\")"},
"location": {
"col": 18,
"file": "policy.rego",
"row": 4,
"text": "\t\tregex.is_valid(\"[0-9]+\")",
"end": {"col": 26, "row": 4},
},
"related_resources": [{
"description": "documentation",
"ref": config.docs.resolve_url("$baseUrl/$category/non-raw-regex-pattern", "idiomatic"),
Expand All @@ -49,7 +61,13 @@ test_fail_pattern_in_second_arg if {
"category": "idiomatic",
"description": "Use raw strings for regex patterns",
"level": "error",
"location": {"col": 25, "file": "policy.rego", "row": 3, "text": "r := regex.replace(\"a\", \"[a]\", \"b\")"},
"location": {
"col": 25,
"file": "policy.rego",
"row": 3,
"text": "r := regex.replace(\"a\", \"[a]\", \"b\")",
"end": {"col": 30, "row": 3},
},
"related_resources": [{
"description": "documentation",
"ref": config.docs.resolve_url("$baseUrl/$category/non-raw-regex-pattern", "idiomatic"),
Expand Down

0 comments on commit bab1f91

Please sign in to comment.