constantCallMatcher identifies strict and non-strict calls which can be folded.
(e ast.NavigableExpr)
| 595 | |
| 596 | // constantCallMatcher identifies strict and non-strict calls which can be folded. |
| 597 | func constantCallMatcher(e ast.NavigableExpr) bool { |
| 598 | call := e.AsCall() |
| 599 | children := e.Children() |
| 600 | fnName := call.FunctionName() |
| 601 | if fnName == operators.LogicalAnd { |
| 602 | for _, child := range children { |
| 603 | if child.Kind() == ast.LiteralKind { |
| 604 | return true |
| 605 | } |
| 606 | } |
| 607 | } |
| 608 | if fnName == operators.LogicalOr { |
| 609 | for _, child := range children { |
| 610 | if child.Kind() == ast.LiteralKind { |
| 611 | return true |
| 612 | } |
| 613 | } |
| 614 | } |
| 615 | if fnName == operators.Conditional { |
| 616 | cond := children[0] |
| 617 | if cond.Kind() == ast.LiteralKind && cond.AsLiteral().Type() == types.BoolType { |
| 618 | return true |
| 619 | } |
| 620 | } |
| 621 | if fnName == operators.Equals || fnName == operators.NotEquals { |
| 622 | if hasComprehensionVar(e) { |
| 623 | return false |
| 624 | } |
| 625 | if isExprConstantOfKind(children[0], types.BoolType) || isExprConstantOfKind(children[1], types.BoolType) { |
| 626 | return true |
| 627 | } |
| 628 | } |
| 629 | if fnName == operators.In { |
| 630 | if hasComprehensionVar(e) { |
| 631 | return false |
| 632 | } |
| 633 | haystack := children[1] |
| 634 | if haystack.Kind() == ast.ListKind && haystack.AsList().Size() == 0 { |
| 635 | return true |
| 636 | } |
| 637 | needle := children[0] |
| 638 | if (needle.Kind() == ast.LiteralKind || isSelfEqualIdent(needle)) && haystack.Kind() == ast.ListKind { |
| 639 | needleIsLit := needle.Kind() == ast.LiteralKind |
| 640 | needleLitVal := needle.AsLiteral() |
| 641 | needleIdentVal := needle.AsIdent() |
| 642 | list := haystack.AsList() |
| 643 | for _, elem := range list.Elements() { |
| 644 | if needleIsLit && elem.Kind() == ast.LiteralKind && elem.AsLiteral().Equal(needleLitVal) == types.True { |
| 645 | return true |
| 646 | } |
| 647 | if !needleIsLit && elem.Kind() == ast.IdentKind && elem.AsIdent() == needleIdentVal { |
| 648 | return true |
| 649 | } |
| 650 | } |
| 651 | } |
| 652 | } |
| 653 | if fnName == operators.Add { |
| 654 | if len(children) == 2 && children[0].Kind() == ast.ListKind && children[1].Kind() == ast.ListKind { |
no test coverage detected