Skip to content

Commit

Permalink
Bump roast to v0.3.0 and live free from annotations on module
Browse files Browse the repository at this point in the history
Signed-off-by: Anders Eknert <anders@styra.com>
  • Loading branch information
anderseknert committed Sep 25, 2024
1 parent 7cd8744 commit e053a47
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 22 deletions.
7 changes: 7 additions & 0 deletions bundle/regal/ast/comments.rego
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ comments["annotation_match"](str) if regex.match(
str,
)

# METADATA
# description: array containing all annotations from the module
annotations := array.concat(
[annotation | some annotation in input["package"].annotations],
[annotation | annotation := input.rules[_].annotations[_]],
)

# METADATA
# description: |
# map of all ignore directive comments, like ("# regal ignore:line-length")
Expand Down
14 changes: 4 additions & 10 deletions bundle/regal/rules/custom/missing-metadata/missing_metadata.rego
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,15 @@ aggregate contains result.aggregate(rego.metadata.chain(), {

default _package_annotated := false

_package_annotated if {
some annotation in input.annotations
annotation.scope in {"package", "subpackages"}
}
_package_annotated if input["package"].annotations

_rule_annotations[path] contains annotated if {
_rule_annotations[rule_path] contains annotated if {
some rule in ast.public_rules_and_functions
every part in rule.head.ref {
not startswith(part.value, "_")
}

path := concat(".", [ast.package_name, ast.ref_static_to_string(rule.head.ref)])

rule_path := concat(".", [ast.package_name, ast.ref_static_to_string(rule.head.ref)])
annotated := count(object.get(rule, "annotations", [])) > 0
}

Expand Down Expand Up @@ -116,14 +112,12 @@ _package_path_aggs[pkg_path] contains item if {
# - input: schema.regal.aggregate
_rule_path_aggs[rule_path] contains agg if {
some item in input.aggregate

some rule_path, annotations in item.aggregate_data.rule_annotations
annotated := annotations != {false}

agg := {
"file": item.aggregate_source.file,
"location": item.aggregate_data.rule_locations[rule_path],
"annotated": annotated,
"annotated": true in annotations,
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ package regal.rules.idiomatic["no-defined-entrypoint"]

import rego.v1

import data.regal.ast
import data.regal.result
import data.regal.util

# METADATA
# description: |
# collects `entrypoint: true` annotations from any given module
aggregate contains entry if {
some annotation in input.annotations
some annotation in ast.annotations
annotation.entrypoint == true

entry := result.aggregate(rego.metadata.chain(), {"entrypoint": util.to_location_object(annotation.location)})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ report contains violation if {
}

_annotation_at_row(row) := annotation if {
some annotation in input.annotations
some annotation in ast.annotations

util.to_location_object(annotation.location).row == row
}
2 changes: 1 addition & 1 deletion e2e/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ func TestTestRegalTestWithExtendedASTTypeChecking(t *testing.T) {

expStart := "1 error occurred: "
expEnd := "rego_type_error: undefined ref: input.foo\n\tinput.foo\n\t ^\n\t " +
"have: \"foo\"\n\t want (one of): [\"annotations\" \"comments\" \"imports\" \"package\" \"regal\" \"rules\"]\n"
"have: \"foo\"\n\t want (one of): [\"comments\" \"imports\" \"package\" \"regal\" \"rules\"]\n"

if !strings.HasPrefix(stderr.String(), expStart) {
t.Errorf("expected stdout error message starting with %q, got %q", expStart, stderr.String())
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.22.5

require (
dario.cat/mergo v1.0.1
github.com/anderseknert/roast v0.2.0
github.com/anderseknert/roast v0.3.0
github.com/coreos/go-semver v0.3.1
github.com/fatih/color v1.17.0
github.com/fsnotify/fsnotify v1.7.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ github.com/ProtonMail/go-crypto v1.0.0 h1:LRuvITjQWX+WIfr930YHG2HNfjR1uOfyf5vE0k
github.com/ProtonMail/go-crypto v1.0.0/go.mod h1:EjAoLdwvbIOoOQr3ihjnSoLZRtE8azugULFRteWMNc0=
github.com/agnivade/levenshtein v1.1.1 h1:QY8M92nrzkmr798gCo3kmMyqXFzdQVpxLlGPRBij0P8=
github.com/agnivade/levenshtein v1.1.1/go.mod h1:veldBMzWxcCG2ZvUTKD2kJNRdCk5hVbJomOvKkmgYbo=
github.com/anderseknert/roast v0.2.0 h1:Hi+noJBe+rLhCzApy8Xo5IE6pkAPBGVFLMJ8EecoghA=
github.com/anderseknert/roast v0.2.0/go.mod h1:+VbTe/Fj1AUhtafMwz9JBdoPqFCS4rkNwsAhJwMgr1Y=
github.com/anderseknert/roast v0.3.0 h1:LJ9zT0yB9xpS01QzR8g2FkP9x8TBPJVvEY08zTypHRs=
github.com/anderseknert/roast v0.3.0/go.mod h1:+VbTe/Fj1AUhtafMwz9JBdoPqFCS4rkNwsAhJwMgr1Y=
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8=
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuWl6zY27l47sB3qLNK6tF2fkHG55UZxx8oIVo4=
github.com/apparentlymart/go-textseg/v13 v13.0.0/go.mod h1:ZK2fH7c4NqDTLtiYLvIkEghdlcqw7yxLeM89kiTRPUo=
Expand Down
12 changes: 6 additions & 6 deletions internal/embeds/schemas/regal-ast.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@
},
"type": "array"
},
"annotations": {
"items": {
"$ref": "#/$defs/annotations"
},
"type": "array"
},
"rules": {
"items": {
"$ref": "#/$defs/rule"
Expand Down Expand Up @@ -273,6 +267,12 @@
},
"location": {
"$ref": "#/$defs/location"
},
"annotations": {
"items": {
"$ref": "#/$defs/annotations"
},
"type": "array"
}
},
"additionalProperties": false,
Expand Down

0 comments on commit e053a47

Please sign in to comment.