public static async Task ComputeRefactoringsAsync(RefactoringContext context, IfStatementSyntax ifStatement)
        {
            if (context.IsAnyRefactoringEnabled(
                    RefactoringIdentifiers.SwapStatementsInIfElse,
                    RefactoringIdentifiers.ReplaceIfElseWithAssignment,
                    RefactoringIdentifiers.ReplaceIfStatementWithReturnStatement,
                    RefactoringIdentifiers.ReplaceIfElseWithSwitch) &&
                IfElseChain.IsTopmostIf(ifStatement) &&
                context.Span.IsBetweenSpans(ifStatement))
            {
                if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceIfStatementWithReturnStatement))
                {
                    await ReplaceIfWithStatementRefactoring.ComputeRefactoringAsync(context, ifStatement).ConfigureAwait(false);
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceIfElseWithAssignment))
                {
                    ReplaceIfElseWithAssignmentRefactoring.ComputeRefactoring(context, ifStatement);
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.SwapStatementsInIfElse))
                {
                    SwapStatementInIfElseRefactoring.ComputeRefactoring(context, ifStatement);
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceIfElseWithSwitch))
                {
                    await ReplaceIfElseWithSwitchRefactoring.ComputeRefactoringAsync(context, ifStatement).ConfigureAwait(false);
                }
            }
        }
Exemple #2
0
        private static bool CheckContainingNode(SyntaxNode node)
        {
            switch (node.Kind())
            {
            case SyntaxKind.ForEachStatement:
            case SyntaxKind.ForStatement:
            case SyntaxKind.UsingStatement:
            case SyntaxKind.WhileStatement:
            case SyntaxKind.DoStatement:
            case SyntaxKind.LockStatement:
            case SyntaxKind.FixedStatement:
            case SyntaxKind.UnsafeStatement:
            case SyntaxKind.TryStatement:
            case SyntaxKind.CheckedStatement:
            case SyntaxKind.UncheckedStatement:
                return(true);

            case SyntaxKind.IfStatement:
                return(IfElseChain.IsTopmostIf((IfStatementSyntax)node));

            case SyntaxKind.ElseClause:
                return(IfElseChain.IsEndOfChain((ElseClauseSyntax)node));
            }

            return(false);
        }
        public static bool CanRefactor(
            IfStatementSyntax ifStatement,
            SemanticModel semanticModel,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            if (IfElseChain.IsTopmostIf(ifStatement))
            {
                ElseClauseSyntax elseClause = ifStatement.Else;

                if (elseClause != null)
                {
                    ExpressionSyntax condition = ifStatement.Condition;

                    if (condition != null)
                    {
                        ITypeSymbol typeSymbol = semanticModel.GetTypeSymbol(condition, cancellationToken);

                        if (typeSymbol?.IsBoolean() == true)
                        {
                            AssignmentExpressionSyntax trueExpression = GetSimpleAssignmentExpression(ifStatement.Statement);

                            ExpressionSyntax trueRight = trueExpression?.Right;

                            if (trueRight?.IsBooleanLiteralExpression() == true)
                            {
                                AssignmentExpressionSyntax falseExpression = GetSimpleAssignmentExpression(elseClause.Statement);

                                ExpressionSyntax falseRight = falseExpression?.Right;

                                if (falseRight?.IsBooleanLiteralExpression() == true)
                                {
                                    var trueBooleanLiteral  = (LiteralExpressionSyntax)trueRight;
                                    var falseBooleanLiteral = (LiteralExpressionSyntax)falseRight;

                                    if (trueBooleanLiteral.IsKind(SyntaxKind.TrueLiteralExpression) != falseBooleanLiteral.IsKind(SyntaxKind.TrueLiteralExpression) &&
                                        trueExpression.Left?.IsEquivalentTo(falseExpression.Left, topLevel: false) == true)
                                    {
                                        return(true);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(false);
        }
        public static async Task ComputeRefactoringAsync(RefactoringContext context, IfStatementSyntax ifStatement)
        {
            if (IfElseChain.IsTopmostIf(ifStatement))
            {
                SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                if (GetIfStatements(ifStatement)
                    .All(f => IsValidIf(f, semanticModel, context.CancellationToken)))
                {
                    string title = (ifStatement.IsSimpleIf())
                        ? "Replace if with switch"
                        : "Replace if-else with switch";

                    context.RegisterRefactoring(
                        title,
                        cancellationToken => RefactorAsync(context.Document, ifStatement, cancellationToken));
                }
            }
        }
        public static async Task ComputeRefactoringsAsync(RefactoringContext context, IfStatementSyntax ifStatement)
        {
            if (context.IsAnyRefactoringEnabled(
                    RefactoringIdentifiers.UseCoalesceExpressionInsteadOfIf,
                    RefactoringIdentifiers.UseConditionalExpressionInsteadOfIf,
                    RefactoringIdentifiers.SimplifyIf,
                    RefactoringIdentifiers.SwapStatementsInIfElse,
                    RefactoringIdentifiers.ReplaceIfElseWithSwitch) &&
                IfElseChain.IsTopmostIf(ifStatement) &&
                context.Span.IsBetweenSpans(ifStatement))
            {
                if (context.IsAnyRefactoringEnabled(
                        RefactoringIdentifiers.UseCoalesceExpressionInsteadOfIf,
                        RefactoringIdentifiers.UseConditionalExpressionInsteadOfIf,
                        RefactoringIdentifiers.SimplifyIf))
                {
                    SemanticModel semanticModel = await context.GetSemanticModelAsync().ConfigureAwait(false);

                    var options = new IfAnalysisOptions(
                        useCoalesceExpression: context.IsRefactoringEnabled(RefactoringIdentifiers.UseCoalesceExpressionInsteadOfIf),
                        useConditionalExpression: context.IsRefactoringEnabled(RefactoringIdentifiers.UseConditionalExpressionInsteadOfIf),
                        useBooleanExpression: context.IsRefactoringEnabled(RefactoringIdentifiers.SimplifyIf));

                    foreach (IfRefactoring refactoring in IfRefactoring.Analyze(ifStatement, options, semanticModel, context.CancellationToken))
                    {
                        context.RegisterRefactoring(
                            refactoring.Title,
                            cancellationToken => refactoring.RefactorAsync(context.Document, cancellationToken));
                    }
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.SwapStatementsInIfElse))
                {
                    SwapStatementInIfElseRefactoring.ComputeRefactoring(context, ifStatement);
                }

                if (context.IsRefactoringEnabled(RefactoringIdentifiers.ReplaceIfElseWithSwitch))
                {
                    await ReplaceIfElseWithSwitchRefactoring.ComputeRefactoringAsync(context, ifStatement).ConfigureAwait(false);
                }
            }
        }
        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));
                                            });
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Exemple #7
0
        private static StatementSyntax GetStatement(
            RefactoringContext context,
            BlockSyntax block,
            SyntaxNode parent)
        {
            switch (parent?.Kind())
            {
            case SyntaxKind.WhileStatement:
            case SyntaxKind.DoStatement:
            case SyntaxKind.ForStatement:
            case SyntaxKind.ForEachStatement:
            case SyntaxKind.FixedStatement:
            case SyntaxKind.CheckedStatement:
            case SyntaxKind.UncheckedStatement:
            case SyntaxKind.UnsafeStatement:
            case SyntaxKind.UsingStatement:
            case SyntaxKind.LockStatement:
            {
                if (block.OpenBraceToken.Span.Contains(context.Span) ||
                    block.CloseBraceToken.Span.Contains(context.Span))
                {
                    if (parent.IsKind(SyntaxKind.UsingStatement))
                    {
                        var usingStatement = (UsingStatementSyntax)parent;

                        while (usingStatement.IsParentKind(SyntaxKind.UsingStatement))
                        {
                            usingStatement = (UsingStatementSyntax)usingStatement.Parent;
                        }

                        return(usingStatement);
                    }

                    return((StatementSyntax)parent);
                }

                break;
            }

            case SyntaxKind.TryStatement:
            {
                var tryStatement = (TryStatementSyntax)parent;

                if (tryStatement.Block?.OpenBraceToken.Span.Contains(context.Span) == true)
                {
                    return((StatementSyntax)parent);
                }

                break;
            }

            case SyntaxKind.IfStatement:
            {
                var ifStatement = (IfStatementSyntax)parent;

                if (IfElseChain.IsTopmostIf(ifStatement) &&
                    block.OpenBraceToken.Span.Contains(context.Span))
                {
                    return(ifStatement);
                }

                if (ifStatement.Else == null &&
                    block.CloseBraceToken.Span.Contains(context.Span))
                {
                    return(ifStatement);
                }

                break;
            }

            case SyntaxKind.ElseClause:
            {
                var elseClause = (ElseClauseSyntax)parent;

                if (block.CloseBraceToken.Span.Contains(context.Span))
                {
                    return(IfElseChain.GetTopmostIf(elseClause));
                }

                break;
            }

            case SyntaxKind.CatchClause:
            {
                var catchClause = (CatchClauseSyntax)parent;

                if (catchClause.IsParentKind(SyntaxKind.TryStatement))
                {
                    var tryStatement = (TryStatementSyntax)catchClause.Parent;

                    if (tryStatement.Finally == null &&
                        catchClause.Block?.CloseBraceToken.Span.Contains(context.Span) == true)
                    {
                        return(tryStatement);
                    }
                }

                break;
            }

            case SyntaxKind.FinallyClause:
            {
                var finallyClause = (FinallyClauseSyntax)parent;

                if (finallyClause.IsParentKind(SyntaxKind.TryStatement))
                {
                    var tryStatement = (TryStatementSyntax)finallyClause.Parent;

                    if (finallyClause.Block?.CloseBraceToken.Span.Contains(context.Span) == true)
                    {
                        return(tryStatement);
                    }
                }

                break;
            }
            }

            return(null);
        }
        public static ImmutableArray <IfRefactoring> Analyze(
            IfStatementSyntax ifStatement,
            IfAnalysisOptions options,
            SemanticModel semanticModel,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            if (IfElseChain.IsTopmostIf(ifStatement))
            {
                ExpressionSyntax condition = ifStatement.Condition;

                if (condition != null)
                {
                    ElseClauseSyntax elseClause = ifStatement.Else;

                    if (elseClause != null)
                    {
                        if (options.CheckSpanDirectives(ifStatement))
                        {
                            StatementSyntax statement1 = ifStatement.GetSingleStatementOrDefault();

                            if (statement1 != null)
                            {
                                SyntaxKind kind1 = statement1.Kind();

                                if (kind1 == SyntaxKind.ExpressionStatement ||
                                    kind1 == SyntaxKind.ReturnStatement ||
                                    kind1 == SyntaxKind.YieldReturnStatement)
                                {
                                    StatementSyntax statement2 = elseClause.GetSingleStatementOrDefault();

                                    if (statement2?.IsKind(kind1) == true)
                                    {
                                        switch (kind1)
                                        {
                                        case SyntaxKind.ExpressionStatement:
                                        {
                                            return(Analyze(ifStatement, condition, (ExpressionStatementSyntax)statement1, (ExpressionStatementSyntax)statement2, options));
                                        }

                                        case SyntaxKind.ReturnStatement:
                                        {
                                            return(Analyze(ifStatement, condition, ((ReturnStatementSyntax)statement1).Expression, ((ReturnStatementSyntax)statement2).Expression, semanticModel, cancellationToken, options, isYield: false));
                                        }

                                        case SyntaxKind.YieldReturnStatement:
                                        {
                                            return(Analyze(ifStatement, condition, ((YieldStatementSyntax)statement1).Expression, ((YieldStatementSyntax)statement2).Expression, semanticModel, cancellationToken, options, isYield: true));
                                        }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        StatementSyntax nextStatement = ifStatement.NextStatement();

                        if (nextStatement?.IsKind(SyntaxKind.ReturnStatement) == true)
                        {
                            return(Analyze(ifStatement, (ReturnStatementSyntax)nextStatement, options, semanticModel, cancellationToken));
                        }
                    }
                }
            }

            return(ImmutableArray <IfRefactoring> .Empty);
        }