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

Fix: block comments should take column into account #1090

Merged
merged 1 commit into from
Sep 10, 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
42 changes: 29 additions & 13 deletions bundle/regal/ast/comments.rego
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,37 @@ ignore_directives[row] := rules if {
# METADATA
# description: |
# returns an array of partitions, i.e. arrays containing all comments
# grouped by their "blocks".
comment_blocks(comments) := [partition |
rows := [row |
some comment in comments
row := util.to_location_object(comment.location).row
# grouped by their "blocks". only comments on the same column as the
# one before is considered to be part of a block.
comment_blocks(comments) := blocks if {
row_partitions := [partition |
rows := [row |
some comment in comments
row := util.to_location_object(comment.location).row
]
breaks := _splits(rows)

some j, k in breaks
partition := array.slice(
comments,
breaks[j - 1] + 1,
k + 1,
)
]
breaks := _splits(rows)

some j, k in breaks
partition := array.slice(
comments,
breaks[j - 1] + 1,
k + 1,
)
]
blocks := [block |
some row_partition in row_partitions
some block in {col: partition |
some comment in row_partition
col := util.to_location_object(comment.location).col

partition := [c |
some c in row_partition
util.to_location_object(c.location).col == col
]
}
]
}

_splits(xs) := array.concat(
array.concat(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ allow := true
}

test_success_attached_metadata if {
r := rule.report with input as ast.policy(`
r := rule.report with input as ast.with_rego_v1(`
# METADATA
# title: valid
allow := true
Expand All @@ -62,9 +62,7 @@ allow := true
}

test_success_detached_document_scope_ok if {
r := rule.report with input as regal.parse_module("p.rego", `
package p

r := rule.report with input as ast.with_rego_v1(`
# METADATA
# scope: document
# description: allow allows
Expand All @@ -75,3 +73,12 @@ allow := true
`)
r == set()
}

test_success_not_detached_by_comment_in_different_column if {
r := rule.report with input as ast.with_rego_v1(`
# METADATA
# title: allow
allow := true # not in block
`)
r == set()
}