Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow ignore directive anywhere in comment #529

Merged
merged 1 commit into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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