Skip to content

Commit 58ec50b

Browse files
authored
Fix location for multivalue rules with generated bodies (#7129)
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]>
1 parent 555fe84 commit 58ec50b

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

ast/parser.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,8 @@ func (p *Parser) parseRules() []*Rule {
780780
case usesContains:
781781
rule.Body = NewBody(NewExpr(BooleanTerm(true).SetLocation(rule.Location)).SetLocation(rule.Location))
782782
rule.generatedBody = true
783+
rule.Location = rule.Head.Location
784+
783785
return []*Rule{&rule}
784786

785787
default:

ast/parser_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4112,6 +4112,29 @@ func TestWildcards(t *testing.T) {
41124112
})
41134113
}
41144114

4115+
// https://github.com/open-policy-agent/opa/issues/7128
4116+
func TestParseMultiValueRuleGeneratedBodyLocationText(t *testing.T) {
4117+
t.Parallel()
4118+
4119+
mod := `package test
4120+
4121+
import rego.v1
4122+
4123+
foo contains "bar"
4124+
`
4125+
4126+
parsed, err := ParseModule("test.rego", mod)
4127+
if err != nil {
4128+
t.Fatal(err)
4129+
}
4130+
4131+
text := string(parsed.Rules[0].Location.Text)
4132+
4133+
if text != `foo contains "bar"` {
4134+
t.Errorf("Expected rule location text to be %q but got %q", `foo contains "bar"`, text)
4135+
}
4136+
}
4137+
41154138
func TestRuleFromBodyJSONOptions(t *testing.T) {
41164139
tests := []string{
41174140
`pi = 3.14159`,

0 commit comments

Comments
 (0)