Creates a mapping from line numbers to Rego rule names
(regoSrc string)
| 387 | |
| 388 | // Creates a mapping from line numbers to Rego rule names |
| 389 | func (p *PolicyToLint) buildRegoRuleMap(regoSrc string) map[int]string { |
| 390 | ruleMap := make(map[int]string) |
| 391 | |
| 392 | // Parse the Rego source into AST |
| 393 | module, err := opaAst.ParseModule("", regoSrc) |
| 394 | if err != nil { |
| 395 | // Return empty map if parsing fails |
| 396 | return ruleMap |
| 397 | } |
| 398 | |
| 399 | // Walk through the AST to find rule definitions |
| 400 | for _, rule := range module.Rules { |
| 401 | if rule.Location != nil { |
| 402 | ruleName := string(rule.Head.Name) |
| 403 | startLine := rule.Location.Row |
| 404 | endLine := startLine |
| 405 | |
| 406 | // Try to find the end line of the rule |
| 407 | if len(rule.Body) > 0 { |
| 408 | lastExpr := rule.Body[len(rule.Body)-1] |
| 409 | if lastExpr.Location != nil { |
| 410 | endLine = lastExpr.Location.Row |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | // Map all lines within this rule to the rule name |
| 415 | for line := startLine; line <= endLine; line++ { |
| 416 | ruleMap[line] = ruleName |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | return ruleMap |
| 422 | } |
no outgoing calls