exprLinesFromPolicy returns a set of line numbers within a policy where expressions (variables, conditions, etc.) are defined.
(policy *Policy)
| 580 | // exprLinesFromPolicy returns a set of line numbers within a policy where expressions (variables, |
| 581 | // conditions, etc.) are defined. |
| 582 | func exprLinesFromPolicy(policy *Policy) map[int]bool { |
| 583 | lines := make(map[int]bool) |
| 584 | addExpectedLines := func(vs ValueString) { |
| 585 | if offset, found := policy.SourceInfo().GetOffsetRange(vs.ID); found { |
| 586 | startLoc, foundStart := policy.Source().OffsetLocation(offset.Start) |
| 587 | // Multiline strings can span multiple lines, but the SourceInfo will only contain the start |
| 588 | // position of the expression. So just skip the check if the expression contains a multiline |
| 589 | // string literal. |
| 590 | hasMultiline := strings.Contains(vs.Value, "'''") || strings.Contains(vs.Value, "\"\"\"") |
| 591 | if foundStart && !hasMultiline { |
| 592 | numLines := strings.Count(vs.Value, "\n") |
| 593 | for i := 0; i <= numLines; i++ { |
| 594 | lines[startLoc.Line()+i] = true |
| 595 | } |
| 596 | } |
| 597 | } |
| 598 | } |
| 599 | var traverseRule func(r *Rule) |
| 600 | traverseRule = func(r *Rule) { |
| 601 | for _, v := range r.Variables() { |
| 602 | addExpectedLines(v.Expression()) |
| 603 | } |
| 604 | for _, m := range r.Matches() { |
| 605 | if !strings.HasPrefix(m.Condition().Value, "true") { |
| 606 | addExpectedLines(m.Condition()) |
| 607 | } |
| 608 | if m.HasOutput() { |
| 609 | addExpectedLines(m.Output()) |
| 610 | } |
| 611 | if m.HasRule() { |
| 612 | traverseRule(m.Rule()) |
| 613 | } |
| 614 | } |
| 615 | } |
| 616 | traverseRule(policy.Rule()) |
| 617 | return lines |
| 618 | } |
| 619 | |
| 620 | func TestCompileYAMLPolicy_Aggregate(t *testing.T) { |
| 621 | type testEval struct { |
no test coverage detected