Skip to content

Commit

Permalink
Fix location for multivalue rules with generated bodies (#7129)
Browse files Browse the repository at this point in the history
Since there is no body, the location of the head is a better option
than simply using the fist scanned token for location.

Fixes #7128

Signed-off-by: Anders Eknert <[email protected]>
  • Loading branch information
anderseknert authored Oct 21, 2024
1 parent 555fe84 commit 58ec50b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ast/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,8 @@ func (p *Parser) parseRules() []*Rule {
case usesContains:
rule.Body = NewBody(NewExpr(BooleanTerm(true).SetLocation(rule.Location)).SetLocation(rule.Location))
rule.generatedBody = true
rule.Location = rule.Head.Location

return []*Rule{&rule}

default:
Expand Down
23 changes: 23 additions & 0 deletions ast/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4112,6 +4112,29 @@ func TestWildcards(t *testing.T) {
})
}

// https://github.com/open-policy-agent/opa/issues/7128
func TestParseMultiValueRuleGeneratedBodyLocationText(t *testing.T) {
t.Parallel()

mod := `package test
import rego.v1
foo contains "bar"
`

parsed, err := ParseModule("test.rego", mod)
if err != nil {
t.Fatal(err)
}

text := string(parsed.Rules[0].Location.Text)

if text != `foo contains "bar"` {
t.Errorf("Expected rule location text to be %q but got %q", `foo contains "bar"`, text)
}
}

func TestRuleFromBodyJSONOptions(t *testing.T) {
tests := []string{
`pi = 3.14159`,
Expand Down

0 comments on commit 58ec50b

Please sign in to comment.