Exemplo n.º 1
0
        private TStatement GetStatement(ElseClauseSyntax elseClause)
        {
            StatementSyntax statement = ReplaceIfWithStatementRefactoring.GetStatement(elseClause);

            if (statement?.IsKind(StatementKind) == true)
            {
                return((TStatement)statement);
            }

            return(null);
        }
Exemplo n.º 2
0
        private TStatement GetStatement(IfStatementSyntax ifStatement)
        {
            StatementSyntax statement = ReplaceIfWithStatementRefactoring.GetStatement(ifStatement);

            if (statement?.IsKind(StatementKind) == true)
            {
                return((TStatement)statement);
            }

            return(null);
        }
        public static void ComputeRefactoring(RefactoringContext context, IfStatementSyntax ifStatement)
        {
            if (IfElseChain.IsTopmostIf(ifStatement))
            {
                ElseClauseSyntax elseClause = ifStatement.Else;

                if (elseClause != null)
                {
                    StatementSyntax statement1 = ReplaceIfWithStatementRefactoring.GetStatement(ifStatement);

                    if (statement1?.IsKind(SyntaxKind.ExpressionStatement) == true)
                    {
                        StatementSyntax statement2 = ReplaceIfWithStatementRefactoring.GetStatement(elseClause);

                        if (statement2?.IsKind(SyntaxKind.ExpressionStatement) == true)
                        {
                            var expressionStatement1 = (ExpressionStatementSyntax)statement1;
                            var expressionStatement2 = (ExpressionStatementSyntax)statement2;

                            ExpressionSyntax expression1 = expressionStatement1.Expression;

                            if (expression1?.IsKind(SyntaxKind.SimpleAssignmentExpression) == true)
                            {
                                ExpressionSyntax expression2 = expressionStatement2.Expression;

                                if (expression2?.IsKind(SyntaxKind.SimpleAssignmentExpression) == true)
                                {
                                    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 &&
                                            left1.IsEquivalentTo(left2, topLevel: false))
                                        {
                                            context.RegisterRefactoring(
                                                "Replace if-else with assignment",
                                                cancellationToken =>
                                            {
                                                return(RefactorAsync(
                                                           context.Document,
                                                           ifStatement,
                                                           left1,
                                                           right1,
                                                           right2,
                                                           cancellationToken));
                                            });
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }