-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lsp: Workspace eval, return rule head locations (#985)
* lsp: Workspace eval, return rule head locations This allows for more reliable highlighting in the UI. * Address PR comments Correct rule_heads name, tidy rego test Signed-off-by: Charlie Egan <charlie@styra.com> * lsp: Fix linter Signed-off-by: Charlie Egan <charlie@styra.com> --------- Signed-off-by: Charlie Egan <charlie@styra.com>
- Loading branch information
1 parent
fc0dc04
commit c5aa188
Showing
4 changed files
with
156 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package regal.ast | ||
|
||
import rego.v1 | ||
|
||
# METADATA | ||
# description: | | ||
# For a given rule head name, this rule contains a list of locations where | ||
# there is a rule head with that name. | ||
rule_head_locations[name] contains info if { | ||
some rule in input.rules | ||
|
||
name := concat(".", [ | ||
"data", | ||
package_name, | ||
ref_static_to_string(rule.head.ref), | ||
]) | ||
|
||
info := { | ||
"row": rule.head.location.row, | ||
"col": rule.head.location.col, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package regal.ast_test | ||
|
||
import rego.v1 | ||
|
||
import data.regal.ast | ||
|
||
test_rule_head_locations if { | ||
policy := `package policy | ||
import rego.v1 | ||
default allow := false | ||
allow if true | ||
reasons contains "foo" | ||
reasons contains "bar" | ||
default my_func(_) := false | ||
my_func(1) := true | ||
ref_rule[foo] := true if { | ||
some foo in [1,2,3] | ||
} | ||
` | ||
|
||
result := ast.rule_head_locations with input as regal.parse_module("p.rego", policy) | ||
|
||
result == { | ||
"data.policy.allow": {{"col": 9, "row": 5}, {"col": 1, "row": 7}}, | ||
"data.policy.reasons": {{"col": 1, "row": 9}, {"col": 1, "row": 10}}, | ||
"data.policy.my_func": {{"col": 9, "row": 12}, {"col": 1, "row": 13}}, | ||
"data.policy.ref_rule": {{"col": 1, "row": 15}}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters