Validate implements the ASTValidator interface method.
(e *Env, _ ValidatorConfig, a *ast.AST, iss *Issues)
| 442 | |
| 443 | // Validate implements the ASTValidator interface method. |
| 444 | func (v nestingLimitValidator) Validate(e *Env, _ ValidatorConfig, a *ast.AST, iss *Issues) { |
| 445 | root := ast.NavigateAST(a) |
| 446 | comprehensions := ast.MatchDescendants(root, ast.KindMatcher(ast.ComprehensionKind)) |
| 447 | if len(comprehensions) <= v.limit { |
| 448 | return |
| 449 | } |
| 450 | for _, comp := range comprehensions { |
| 451 | count := 0 |
| 452 | e := comp |
| 453 | hasParent := true |
| 454 | for hasParent { |
| 455 | // When the expression is not a comprehension, continue to the next ancestor. |
| 456 | if e.Kind() != ast.ComprehensionKind { |
| 457 | e, hasParent = e.Parent() |
| 458 | continue |
| 459 | } |
| 460 | // When the comprehension has an empty range, continue to the next ancestor |
| 461 | // as this comprehension does not have any associated cost. |
| 462 | if isEmptyRangeComprehension(e) { |
| 463 | e, hasParent = e.Parent() |
| 464 | continue |
| 465 | } |
| 466 | // Otherwise check the nesting limit. |
| 467 | count++ |
| 468 | if count > v.limit { |
| 469 | iss.ReportErrorAtID(comp.ID(), "comprehension exceeds nesting limit") |
| 470 | break |
| 471 | } |
| 472 | e, hasParent = e.Parent() |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | type bindNestingLimitValidator struct { |
| 478 | limit int |
nothing calls this directly
no test coverage detected