Skip to content

Commit

Permalink
Allow ignore directive anywhere in comment (#529)
Browse files Browse the repository at this point in the history
Fixes #527

Signed-off-by: Anders Eknert <[email protected]>
  • Loading branch information
anderseknert authored Jan 5, 2024
1 parent 47f6cc2 commit 7d3fdd3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
6 changes: 2 additions & 4 deletions bundle/regal/main.rego
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,10 @@ ignore_directives[row] := rules if {
some comment in ast.comments_decoded
text := trim_space(comment.Text)

startswith(text, "regal")

i := indexof(text, "ignore:")
i := indexof(text, "regal ignore:")
i != -1

list := regex.replace(substring(text, i + 7, -1), `\s`, "")
list := regex.replace(substring(text, i + 13, -1), `\s`, "")

row := comment.Location.row + 1
rules := split(list, ",")
Expand Down
22 changes: 22 additions & 0 deletions bundle/regal/main_test.rego
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,28 @@ test_main_ignore_directive_success_same_line if {
count(report) == 0
}

test_main_ignore_directive_success_same_line_trailing_directive if {
policy := `package p
camelCase := "yes" # camelCase is nice! # regal ignore:prefer-snake-case
`
report := main.report with input as regal.parse_module("p.rego", policy)
with config.merged_config as {"rules": {"style": {"prefer-snake-case": {"level": "error"}}}}

count(report) == 0
}

test_main_ignore_directive_success_same_line_todo_comment if {
policy := `package p
camelCase := "yes" # TODO! camelCase isn't nice! # regal ignore:todo-comment
`
report := main.report with input as regal.parse_module("p.rego", policy)
with config.merged_config as {"rules": {"style": {"todo-comment": {"level": "error"}}}}

count(report) == 0
}

test_main_ignore_directive_multiple_success if {
policy := `package p
Expand Down
2 changes: 1 addition & 1 deletion bundle/regal/rules/style/todo_comment.rego
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import rego.v1
import data.regal.ast
import data.regal.result

# For comments, OPA uses capital-cases Text and Location rather
# For comments, OPA uses capital-cased Text and Location rather
# than text and location. As fixing this would potentially break
# things, we need to take it into consideration here.

Expand Down

0 comments on commit 7d3fdd3

Please sign in to comment.