constantCallMatcher identifies strict and non-strict calls which can be folded.
(e ast.NavigableExpr)
| 575 | |
| 576 | // constantCallMatcher identifies strict and non-strict calls which can be folded. |
| 577 | func constantCallMatcher(e ast.NavigableExpr) bool { |
| 578 | call := e.AsCall() |
| 579 | children := e.Children() |
| 580 | fnName := call.FunctionName() |
| 581 | if fnName == operators.LogicalAnd { |
| 582 | for _, child := range children { |
| 583 | if child.Kind() == ast.LiteralKind { |
| 584 | return true |
| 585 | } |
| 586 | } |
| 587 | } |
| 588 | if fnName == operators.LogicalOr { |
| 589 | for _, child := range children { |
| 590 | if child.Kind() == ast.LiteralKind { |
| 591 | return true |
| 592 | } |
| 593 | } |
| 594 | } |
| 595 | if fnName == operators.Conditional { |
| 596 | cond := children[0] |
| 597 | if cond.Kind() == ast.LiteralKind && cond.AsLiteral().Type() == types.BoolType { |
| 598 | return true |
| 599 | } |
| 600 | } |
| 601 | if fnName == operators.Equals || fnName == operators.NotEquals { |
| 602 | if hasComprehensionVar(e) { |
| 603 | return false |
| 604 | } |
| 605 | if isExprConstantOfKind(children[0], types.BoolType) || isExprConstantOfKind(children[1], types.BoolType) { |
| 606 | return true |
| 607 | } |
| 608 | } |
| 609 | if fnName == operators.In { |
| 610 | if hasComprehensionVar(e) { |
| 611 | return false |
| 612 | } |
| 613 | haystack := children[1] |
| 614 | if haystack.Kind() == ast.ListKind && haystack.AsList().Size() == 0 { |
| 615 | return true |
| 616 | } |
| 617 | needle := children[0] |
| 618 | if (needle.Kind() == ast.LiteralKind || isSelfEqualIdent(needle)) && haystack.Kind() == ast.ListKind { |
| 619 | needleIsLit := needle.Kind() == ast.LiteralKind |
| 620 | needleLitVal := needle.AsLiteral() |
| 621 | needleIdentVal := needle.AsIdent() |
| 622 | list := haystack.AsList() |
| 623 | for _, elem := range list.Elements() { |
| 624 | if needleIsLit && elem.Kind() == ast.LiteralKind && elem.AsLiteral().Equal(needleLitVal) == types.True { |
| 625 | return true |
| 626 | } |
| 627 | if !needleIsLit && elem.Kind() == ast.IdentKind && elem.AsIdent() == needleIdentVal { |
| 628 | return true |
| 629 | } |
| 630 | } |
| 631 | } |
| 632 | } |
| 633 | // convert all other calls with constant arguments |
| 634 | for _, child := range children { |
no test coverage detected