Validate implements the ASTValidator interface method.
(e *Env, _ ValidatorConfig, a *ast.AST, iss *Issues)
| 413 | |
| 414 | // Validate implements the ASTValidator interface method. |
| 415 | func (v nestingLimitValidator) Validate(e *Env, _ ValidatorConfig, a *ast.AST, iss *Issues) { |
| 416 | root := ast.NavigateAST(a) |
| 417 | comprehensions := ast.MatchDescendants(root, ast.KindMatcher(ast.ComprehensionKind)) |
| 418 | if len(comprehensions) <= v.limit { |
| 419 | return |
| 420 | } |
| 421 | for _, comp := range comprehensions { |
| 422 | count := 0 |
| 423 | e := comp |
| 424 | hasParent := true |
| 425 | for hasParent { |
| 426 | // When the expression is not a comprehension, continue to the next ancestor. |
| 427 | if e.Kind() != ast.ComprehensionKind { |
| 428 | e, hasParent = e.Parent() |
| 429 | continue |
| 430 | } |
| 431 | // When the comprehension has an empty range, continue to the next ancestor |
| 432 | // as this comprehension does not have any associated cost. |
| 433 | iterRange := e.AsComprehension().IterRange() |
| 434 | if iterRange.Kind() == ast.ListKind && iterRange.AsList().Size() == 0 { |
| 435 | e, hasParent = e.Parent() |
| 436 | continue |
| 437 | } |
| 438 | // Otherwise check the nesting limit. |
| 439 | count++ |
| 440 | if count > v.limit { |
| 441 | iss.ReportErrorAtID(comp.ID(), "comprehension exceeds nesting limit") |
| 442 | break |
| 443 | } |
| 444 | e, hasParent = e.Parent() |
| 445 | } |
| 446 | } |
| 447 | } |
nothing calls this directly
no test coverage detected