constantCallMatcher identifies strict and non-strict calls which can be folded.
(e ast.NavigableExpr)
| 613 | |
| 614 | // constantCallMatcher identifies strict and non-strict calls which can be folded. |
| 615 | func constantCallMatcher(e ast.NavigableExpr) bool { |
| 616 | call := e.AsCall() |
| 617 | children := e.Children() |
| 618 | fnName := call.FunctionName() |
| 619 | if fnName == operators.LogicalAnd { |
| 620 | for _, child := range children { |
| 621 | if child.Kind() == ast.LiteralKind { |
| 622 | return true |
| 623 | } |
| 624 | } |
| 625 | } |
| 626 | if fnName == operators.LogicalOr { |
| 627 | for _, child := range children { |
| 628 | if child.Kind() == ast.LiteralKind { |
| 629 | return true |
| 630 | } |
| 631 | } |
| 632 | } |
| 633 | if fnName == operators.Conditional { |
| 634 | cond := children[0] |
| 635 | if cond.Kind() == ast.LiteralKind && cond.AsLiteral().Type() == types.BoolType { |
| 636 | return true |
| 637 | } |
| 638 | } |
| 639 | if fnName == operators.Equals || fnName == operators.NotEquals { |
| 640 | if hasComprehensionVar(e) { |
| 641 | return false |
| 642 | } |
| 643 | if isExprConstantOfKind(children[0], types.BoolType) || isExprConstantOfKind(children[1], types.BoolType) { |
| 644 | return true |
| 645 | } |
| 646 | } |
| 647 | if fnName == operators.In { |
| 648 | if hasComprehensionVar(e) { |
| 649 | return false |
| 650 | } |
| 651 | haystack := children[1] |
| 652 | if isCollectionEmpty(haystack) || collectionContainsNeedle(children[0], haystack) { |
| 653 | return true |
| 654 | } |
| 655 | } |
| 656 | if fnName == operators.Add { |
| 657 | if len(children) == 2 && children[0].Kind() == ast.ListKind && children[1].Kind() == ast.ListKind { |
| 658 | return true |
| 659 | } |
| 660 | } |
| 661 | // convert all other calls with constant arguments |
| 662 | for _, child := range children { |
| 663 | if !constantMatcher(child) { |
| 664 | return false |
| 665 | } |
| 666 | } |
| 667 | return true |
| 668 | } |
| 669 | |
| 670 | func isCollectionEmpty(e ast.Expr) bool { |
| 671 | switch e.Kind() { |
no test coverage detected