Skip to content

Commit 69cce1e

Browse files
committed
ast: remove 'parse if id key is in metadata' heuristic
Since we process annotations by default now, this is no longer relevant. Users that opt-out of annotations processing will not benefit from rule IDs _in metadata_, but that's consistent. Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
1 parent bc6b981 commit 69cce1e

3 files changed

Lines changed: 0 additions & 73 deletions

File tree

CHANGELOG.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ Duplicate IDs from functions called multiple times are suppressed.
3232
allow if input.role == "admin"
3333
```
3434

35-
Modules containing `id` annotations will have metadata parsing enabled automatically.
3635
When external rule sources are registered, rule tracking is always enabled so that
3736
externally-provided rules with `id` annotations are recorded.
3837

v1/ast/parser.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -545,30 +545,13 @@ func (p *Parser) Parse() ([]Statement, []*Comment, Errors) {
545545
break
546546
}
547547

548-
if !p.po.ProcessAnnotation && hasMetadataWithID(p.s.comments) {
549-
p.po.ProcessAnnotation = true
550-
}
551-
552548
if p.po.ProcessAnnotation {
553549
stmts = p.parseAnnotations(stmts)
554550
}
555551

556552
return stmts, p.s.comments, p.s.errors
557553
}
558554

559-
func hasMetadataWithID(comments []*Comment) bool {
560-
for i := range comments {
561-
if IsMetadataComment(comments[i]) {
562-
for i++; i < len(comments) && !blockBuster(comments[i], comments[i-1]); i++ {
563-
if bytes.HasPrefix(bytes.TrimSpace(comments[i].Text), []byte("id:")) {
564-
return true
565-
}
566-
}
567-
}
568-
}
569-
return false
570-
}
571-
572555
func (p *Parser) parseAnnotations(stmts []Statement) []Statement {
573556
annotStmts, errs := parseAnnotations(p.s.comments)
574557
for _, err := range errs {

v1/ast/parser_test.go

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -8197,61 +8197,6 @@ allow if {
81978197
assertLocationText(t, "# METADATA\n# title: rule", m.Rules[0].Annotations[0].Location)
81988198
}
81998199

8200-
func TestAnnotationsAutoEnableWithID(t *testing.T) {
8201-
tests := []struct {
8202-
note string
8203-
module string
8204-
processAnnotation bool
8205-
expAnnotationsCount int
8206-
}{
8207-
{
8208-
note: "auto-enable when metadata has id field",
8209-
module: `package test
8210-
# METADATA
8211-
# id: test-rule
8212-
allow if true
8213-
`,
8214-
processAnnotation: false,
8215-
expAnnotationsCount: 1,
8216-
},
8217-
{
8218-
note: "no auto-enable when metadata lacks id field",
8219-
module: `# METADATA
8220-
# title: Test Rule
8221-
package test
8222-
allow if true
8223-
`,
8224-
processAnnotation: false,
8225-
expAnnotationsCount: 0,
8226-
},
8227-
{
8228-
note: "explicit ProcessAnnotation still works",
8229-
module: `# METADATA
8230-
# title: Test Rule
8231-
package test
8232-
allow if true
8233-
`,
8234-
processAnnotation: true,
8235-
expAnnotationsCount: 1,
8236-
},
8237-
}
8238-
8239-
for _, tc := range tests {
8240-
t.Run(tc.note, func(t *testing.T) {
8241-
m, err := ParseModuleWithOpts("test.rego", tc.module, ParserOptions{
8242-
ProcessAnnotation: tc.processAnnotation,
8243-
})
8244-
if err != nil {
8245-
t.Fatal(err)
8246-
}
8247-
8248-
if len(m.Annotations) != tc.expAnnotationsCount {
8249-
t.Errorf("expected %d annotations but got %d", tc.expAnnotationsCount, len(m.Annotations))
8250-
}
8251-
})
8252-
}
8253-
}
8254-
82558200
func TestMaxParsingRecursionDepth(t *testing.T) {
82568201
tests := []struct {
82578202
name string

0 commit comments

Comments
 (0)