Beispiel #1
0
        private static ImmutableArray <IfRefactoring> Analyze(
            IfStatementSyntax ifStatement,
            ExpressionSyntax condition,
            ExpressionSyntax expression1,
            ExpressionSyntax expression2,
            SemanticModel semanticModel,
            CancellationToken cancellationToken,
            IfAnalysisOptions options,
            bool isYield)
        {
            if (expression1?.IsMissing == false &&
                expression2?.IsMissing == false)
            {
                if (options.UseCoalesceExpression)
                {
                    NullCheckExpression nullCheck;
                    if (NullCheckExpression.TryCreate(condition, semanticModel, out nullCheck, cancellationToken))
                    {
                        IfRefactoring refactoring = CreateIfToReturnWithCoalesceExpression(
                            ifStatement,
                            (nullCheck.IsCheckingNull) ? expression2 : expression1,
                            (nullCheck.IsCheckingNull) ? expression1 : expression2,
                            nullCheck,
                            isYield,
                            semanticModel,
                            cancellationToken);

                        if (refactoring != null)
                        {
                            return(refactoring.ToImmutableArray());
                        }
                    }
                }

                IfToReturnWithBooleanExpression ifToReturnWithBooleanExpression = null;

                if (options.UseBooleanExpression &&
                    (expression1.IsBooleanLiteralExpression() || expression2.IsBooleanLiteralExpression()) &&
                    semanticModel.GetTypeSymbol(expression1, cancellationToken)?.IsBoolean() == true &&
                    semanticModel.GetTypeSymbol(expression2, cancellationToken)?.IsBoolean() == true)
                {
                    ifToReturnWithBooleanExpression = IfToReturnWithBooleanExpression.Create(ifStatement, expression1, expression2, isYield);
                }

                IfToReturnWithConditionalExpression ifToReturnWithConditionalExpression = null;

                if (options.UseConditionalExpression &&
                    (!expression1.IsBooleanLiteralExpression() || !expression2.IsBooleanLiteralExpression()))
                {
                    ifToReturnWithConditionalExpression = IfToReturnWithConditionalExpression.Create(ifStatement, expression1, expression2, isYield);
                }

                return(ToImmutableArray(ifToReturnWithBooleanExpression, ifToReturnWithConditionalExpression));
            }

            return(ImmutableArray <IfRefactoring> .Empty);
        }
Beispiel #2
0
        private static ImmutableArray <IfRefactoring> ToImmutableArray(IfRefactoring refactoring1, IfRefactoring refactoring2)
        {
            if (refactoring1 != null)
            {
                if (refactoring2 != null)
                {
                    return(ImmutableArray.Create(refactoring1, refactoring2));
                }
                else
                {
                    return(refactoring1.ToImmutableArray());
                }
            }
            else if (refactoring2 != null)
            {
                return(refactoring2.ToImmutableArray());
            }

            return(ImmutableArray <IfRefactoring> .Empty);
        }
Beispiel #3
0
        private static ImmutableArray <IfRefactoring> Analyze(
            IfStatementSyntax ifStatement,
            ExpressionSyntax condition,
            ExpressionStatementSyntax expressionStatement1,
            ExpressionStatementSyntax expressionStatement2,
            SemanticModel semanticModel,
            CancellationToken cancellationToken,
            IfAnalysisOptions options)
        {
            ExpressionSyntax expression1 = expressionStatement1.Expression;

            if (IsSimpleAssignment(expression1))
            {
                ExpressionSyntax expression2 = expressionStatement2.Expression;

                if (IsSimpleAssignment(expression2))
                {
                    var assignment1 = (AssignmentExpressionSyntax)expression1;
                    var assignment2 = (AssignmentExpressionSyntax)expression2;

                    ExpressionSyntax left1  = assignment1.Left;
                    ExpressionSyntax right1 = assignment1.Right;

                    if (left1?.IsMissing == false &&
                        right1?.IsMissing == false)
                    {
                        ExpressionSyntax left2  = assignment2.Left;
                        ExpressionSyntax right2 = assignment2.Right;

                        if (left2?.IsMissing == false &&
                            right2?.IsMissing == false &&
                            IsEquivalent(left1, left2))
                        {
                            if (options.UseCoalesceExpression)
                            {
                                NullCheckExpression nullCheck;
                                if (NullCheckExpression.TryCreate(condition, semanticModel, out nullCheck, cancellationToken))
                                {
                                    IfRefactoring refactoring = CreateIfToAssignmentWithWithCoalesceExpression(
                                        ifStatement,
                                        left1,
                                        (nullCheck.IsCheckingNull) ? right2 : right1,
                                        (nullCheck.IsCheckingNull) ? right1 : right2,
                                        nullCheck,
                                        semanticModel,
                                        cancellationToken);

                                    if (refactoring != null)
                                    {
                                        return(refactoring.ToImmutableArray());
                                    }
                                }
                            }

                            if (options.UseConditionalExpression)
                            {
                                return(new IfElseToAssignmentWithConditionalExpression(ifStatement, left1, right1, right2).ToImmutableArray());
                            }
                        }
                    }
                }
            }

            return(ImmutableArray <IfRefactoring> .Empty);
        }
        private static ImmutableArray <IfRefactoring> Analyze(
            IfStatementSyntax ifStatement,
            ExpressionSyntax condition,
            ExpressionStatementSyntax expressionStatement1,
            ExpressionStatementSyntax expressionStatement2,
            IfAnalysisOptions options,
            SemanticModel semanticModel,
            CancellationToken cancellationToken)
        {
            SimpleAssignmentStatementInfo assignment1 = SyntaxInfo.SimpleAssignmentStatementInfo(expressionStatement1);

            if (!assignment1.Success)
            {
                return(Empty);
            }

            SimpleAssignmentStatementInfo assignment2 = SyntaxInfo.SimpleAssignmentStatementInfo(expressionStatement2);

            if (!assignment2.Success)
            {
                return(Empty);
            }

            ExpressionSyntax left1  = assignment1.Left;
            ExpressionSyntax left2  = assignment2.Left;
            ExpressionSyntax right1 = assignment1.Right;
            ExpressionSyntax right2 = assignment2.Right;

            if (!SyntaxComparer.AreEquivalent(left1, left2))
            {
                return(Empty);
            }

            if (options.UseCoalesceExpression ||
                options.UseExpression)
            {
                SyntaxKind kind1 = right1.Kind();
                SyntaxKind kind2 = right2.Kind();

                if (kind1.IsBooleanLiteralExpression() &&
                    kind2.IsBooleanLiteralExpression() &&
                    kind1 != kind2)
                {
                    if (options.UseExpression)
                    {
                        return(new IfElseToAssignmentWithCondition(ifStatement, left1, condition, negate: kind1 == SyntaxKind.FalseLiteralExpression).ToImmutableArray());
                    }

                    return(Empty);
                }

                NullCheckExpressionInfo nullCheck = SyntaxInfo.NullCheckExpressionInfo(condition, semanticModel: semanticModel, cancellationToken: cancellationToken);

                if (nullCheck.Success)
                {
                    IfRefactoring refactoring = CreateIfToAssignment(
                        ifStatement,
                        left1,
                        (nullCheck.IsCheckingNull) ? right2 : right1,
                        (nullCheck.IsCheckingNull) ? right1 : right2,
                        nullCheck,
                        options,
                        semanticModel,
                        cancellationToken);

                    if (refactoring != null)
                    {
                        return(refactoring.ToImmutableArray());
                    }
                }
            }

            if (options.UseConditionalExpression)
            {
                return(new IfElseToAssignmentWithConditionalExpression(ifStatement, left1, right1, right2).ToImmutableArray());
            }

            return(Empty);
        }
        private static ImmutableArray <IfRefactoring> Analyze(
            IfStatementSyntax ifStatement,
            ExpressionSyntax condition,
            ExpressionSyntax expression1,
            ExpressionSyntax expression2,
            IfAnalysisOptions options,
            bool isYield,
            SemanticModel semanticModel,
            CancellationToken cancellationToken)
        {
            if (expression1?.IsMissing != false)
            {
                return(Empty);
            }

            if (expression2?.IsMissing != false)
            {
                return(Empty);
            }

            if (options.UseCoalesceExpression ||
                options.UseExpression)
            {
                SyntaxKind kind1 = expression1.Kind();
                SyntaxKind kind2 = expression2.Kind();

                if (kind1.IsBooleanLiteralExpression() &&
                    kind2.IsBooleanLiteralExpression() &&
                    kind1 != kind2)
                {
                    if (options.UseExpression)
                    {
                        if (ifStatement.IsSimpleIf() &&
                            (ifStatement.PreviousStatementOrDefault() is IfStatementSyntax previousIf) &&
                            previousIf.IsSimpleIf() &&
                            (previousIf.GetSingleStatementOrDefault() is ReturnStatementSyntax returnStatement) &&
                            returnStatement.Expression?.WalkDownParentheses().Kind() == kind1)
                        {
                            return(Empty);
                        }

                        return(new IfToReturnWithExpression(ifStatement, condition, isYield, negate: kind1 == SyntaxKind.FalseLiteralExpression).ToImmutableArray());
                    }

                    return(Empty);
                }

                NullCheckExpressionInfo nullCheck = SyntaxInfo.NullCheckExpressionInfo(condition, semanticModel: semanticModel, cancellationToken: cancellationToken);

                if (nullCheck.Success)
                {
                    IfRefactoring refactoring = CreateIfToReturnStatement(
                        ifStatement,
                        (nullCheck.IsCheckingNull) ? expression2 : expression1,
                        (nullCheck.IsCheckingNull) ? expression1 : expression2,
                        nullCheck,
                        options,
                        isYield,
                        semanticModel,
                        cancellationToken);

                    if (refactoring != null)
                    {
                        return(refactoring.ToImmutableArray());
                    }
                }
            }

            IfToReturnWithBooleanExpression ifToReturnWithBooleanExpression = null;

            if (options.UseBooleanExpression &&
                (expression1.Kind().IsBooleanLiteralExpression() || expression2.Kind().IsBooleanLiteralExpression()) &&
                semanticModel.GetTypeSymbol(expression1, cancellationToken)?.IsBoolean() == true &&
                semanticModel.GetTypeSymbol(expression2, cancellationToken)?.IsBoolean() == true)
            {
                ifToReturnWithBooleanExpression = IfToReturnWithBooleanExpression.Create(ifStatement, expression1, expression2, isYield);
            }

            IfToReturnWithConditionalExpression ifToReturnWithConditionalExpression = null;

            if (options.UseConditionalExpression &&
                (!expression1.Kind().IsBooleanLiteralExpression() || !expression2.Kind().IsBooleanLiteralExpression()))
            {
                ifToReturnWithConditionalExpression = IfToReturnWithConditionalExpression.Create(ifStatement, expression1, expression2, isYield);
            }

            return(ToImmutableArray(ifToReturnWithBooleanExpression, ifToReturnWithConditionalExpression));
        }