Exemplo n.º 1
0
        public static bool TryGetAction(
            StatementSyntax statement,
            out Func <SyntaxNode, SyntaxNode> action)
        {
            InvocationExpressionSyntax   whereInvocation;
            SimpleLambdaExpressionSyntax filter;

            if (!LinqHelper.TryFindMethodInvocation(
                    statement,
                    LinqHelper.WhereMethodName,
                    lambda => lambda.Body.IsKind(SyntaxKind.LogicalAndExpression),
                    out whereInvocation,
                    out filter))
            {
                action = null;
                return(false);
            }

            action = syntaxRoot =>
            {
                var newWhereInvocation = SplitLinqWhereInvocation(whereInvocation, filter);

                syntaxRoot = syntaxRoot.ReplaceNode(whereInvocation, newWhereInvocation);

                return(syntaxRoot.Format());
            };

            return(true);
        }
Exemplo n.º 2
0
        public static bool TryGetAction(
            StatementSyntax statement,
            SemanticModel semanticModel,
            out Func <SyntaxNode, SyntaxNode> action)
        {
            InvocationExpressionSyntax         selectInvocation;
            SimpleLambdaExpressionSyntax       projection;
            Stack <InvocationExpressionSyntax> invocationStack = null;

            if (!LinqHelper.TryFindMethodInvocation(
                    statement,
                    LinqHelper.SelectMethodName,
                    selectArgument => IsFunctionComposition(
                        selectArgument,
                        semanticModel,
                        out invocationStack),
                    out selectInvocation,
                    out projection))
            {
                action = null;
                return(false);
            }

            var invocations = invocationStack.ToArray();

            action = syntaxRoot =>
            {
                var newSelectInvocation = SplitFunctionComposition(
                    selectInvocation,
                    projection,
                    invocations,
                    semanticModel);

                syntaxRoot = syntaxRoot.ReplaceNode(selectInvocation, newSelectInvocation);

                return(syntaxRoot.Format());
            };

            return(true);
        }
Exemplo n.º 3
0
        public static bool TryGetAction(
            StatementSyntax statement,
            out bool isAllToAny,
            out Func <SyntaxNode, SyntaxNode> action)
        {
            isAllToAny = false;
            action     = null;

            InvocationExpressionSyntax   invocation;
            SimpleLambdaExpressionSyntax lambda;

            //
            // Find LINQ All/Any invocation where predicate is invertible.
            //

            int methodIndex;

            if (!LinqHelper.TryFindMethodInvocation(
                    statement,
                    ImmutableArray.Create(LinqHelper.AllMethodName, LinqHelper.AnyMethodName),
                    IsInvertible,
                    out invocation,
                    out lambda,
                    out methodIndex))
            {
                return(false);
            }

            isAllToAny = methodIndex == 0;

            //
            // Found invocation must be last in a chain.
            //

            if (invocation.Parent.IsKind(SyntaxKind.SimpleMemberAccessExpression) ||
                invocation.Parent.IsKind(SyntaxKind.ConditionalAccessExpression))
            {
                return(false);
            }

            bool isNegationRequired = !invocation.Parent.IsKind(SyntaxKind.LogicalNotExpression);

            bool allToAny = isAllToAny;

            action = syntaxRoot =>
            {
                var newInvocation = Invert(allToAny, isNegationRequired, invocation, lambda);

                SyntaxNode newRoot;

                if (isNegationRequired)
                {
                    var finalNegation = SyntaxFactory.PrefixUnaryExpression(
                        SyntaxKind.LogicalNotExpression,
                        newInvocation);

                    newRoot = syntaxRoot.ReplaceNode((SyntaxNode)invocation, finalNegation);
                }
                else
                {
                    newRoot = syntaxRoot.ReplaceNode((SyntaxNode)invocation.Parent, newInvocation);
                }

                return(newRoot.Format());
            };

            return(true);
        }