Exemplo n.º 1
0
        private void AnalyzeStatement(SyntaxNodeAnalysisContext context)
        {
            if (GeneratedCodeAnalyzer?.IsGeneratedCode(context) == true)
            {
                return;
            }

            if (!context.Node.IsKind(SyntaxKind.IfStatement) ||
                IfElseAnalysis.IsIsolatedIf((IfStatementSyntax)context.Node))
            {
                BlockSyntax block = EmbeddedStatementAnalysis.GetBlockThatCanBeEmbeddedStatement(context.Node);

                if (block != null &&
                    !block.OpenBraceToken.IsMissing &&
                    !block.CloseBraceToken.IsMissing &&
                    block.OpenBraceToken.LeadingTrivia.All(f => f.IsWhitespaceOrEndOfLineTrivia()) &&
                    block.OpenBraceToken.TrailingTrivia.All(f => f.IsWhitespaceOrEndOfLineTrivia()) &&
                    block.CloseBraceToken.LeadingTrivia.All(f => f.IsWhitespaceOrEndOfLineTrivia()) &&
                    block.CloseBraceToken.TrailingTrivia.All(f => f.IsWhitespaceOrEndOfLineTrivia()))
                {
                    context.ReportDiagnostic(
                        DiagnosticDescriptors.RemoveBraces,
                        block.GetLocation(),
                        SyntaxHelper.GetNodeTitle(context.Node));

                    context.FadeOutBraces(DiagnosticDescriptors.RemoveBracesFadeOut, block);
                }
            }
        }
        public static void ComputeRefactoring(RefactoringContext context, SelectedStatementsInfo info)
        {
            if (info.IsSingleSelected)
            {
                StatementSyntax[] statements = info.SelectedNodes().ToArray();

                StatementSyntax first = statements[0];

                if (first.IsKind(SyntaxKind.IfStatement))
                {
                    var ifStatement = (IfStatementSyntax)first;

                    if (IfElseAnalysis.IsTopmostIf(ifStatement) &&
                        CanBeReplacedWithSwitch(ifStatement))
                    {
                        string title = (IfElseAnalysis.IsIsolatedIf(ifStatement))
                            ? "Replace if with switch"
                            : "Replace if-else with switch";

                        context.RegisterRefactoring(
                            title,
                            cancellationToken =>
                        {
                            return(RefactorAsync(
                                       context.Document,
                                       ifStatement,
                                       cancellationToken));
                        });
                    }
                }
            }
        }
Exemplo n.º 3
0
        private void AnalyzeStatement(SyntaxNodeAnalysisContext context)
        {
            if (GeneratedCodeAnalyzer?.IsGeneratedCode(context) == true)
            {
                return;
            }

            AnalyzeEmbeddedStatement(context);

            if (context.Node.IsKind(SyntaxKind.IfStatement) &&
                !IfElseAnalysis.IsIsolatedIf((IfStatementSyntax)context.Node))
            {
                return;
            }

            StatementSyntax statement = EmbeddedStatementAnalysis.GetEmbeddedStatementThatShouldBeInsideBlock(context.Node);

            if (statement != null)
            {
                context.ReportDiagnostic(
                    DiagnosticDescriptors.AddBraces,
                    statement.GetLocation(),
                    SyntaxHelper.GetNodeTitle(context.Node));
            }
        }
Exemplo n.º 4
0
        public static void Analyze(SyntaxNodeAnalysisContext context, IfStatementSyntax ifStatement)
        {
            if (IfElseAnalysis.IsIsolatedIf(ifStatement) &&
                ConditionAllowsMerging(ifStatement.Condition))
            {
                IfStatementSyntax nestedIf = GetNestedIfStatement(ifStatement);

                if (nestedIf != null &&
                    nestedIf.Else == null &&
                    ConditionAllowsMerging(nestedIf.Condition) &&
                    TriviaAllowsMerging(ifStatement, nestedIf))
                {
                    context.ReportDiagnostic(
                        DiagnosticDescriptors.MergeIfStatementWithNestedIfStatement,
                        ifStatement.GetLocation());

                    FadeOut(context, ifStatement, nestedIf);
                }
            }
        }