public static Task <Document> RefactorAsync(
            Document document,
            BlockSyntax block,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            StatementSyntax statement = block.Statements[0];

            if (block.IsParentKind(SyntaxKind.ElseClause) &&
                statement.IsKind(SyntaxKind.IfStatement))
            {
                var elseClause = (ElseClauseSyntax)block.Parent;

                ElseClauseSyntax newElseClause = elseClause
                                                 .WithStatement(statement)
                                                 .WithElseKeyword(elseClause.ElseKeyword.WithoutTrailingTrivia())
                                                 .WithFormatterAnnotation();

                return(document.ReplaceNodeAsync(elseClause, newElseClause, cancellationToken));
            }
            else
            {
                StatementSyntax newNode = statement.TrimLeadingTrivia()
                                          .WithFormatterAnnotation();

                return(document.ReplaceNodeAsync(block, newNode, cancellationToken));
            }
        }
Esempio n. 2
0
        public static void Analyze(SyntaxNodeAnalysisContext context, BlockSyntax block)
        {
            if (block.IsParentKind(SyntaxKind.Block) &&
                block.Statements.Any())
            {
                context.ReportDiagnostic(DiagnosticDescriptors.RemoveRedundantBraces, block);

                context.ReportBraces(DiagnosticDescriptors.RemoveRedundantBracesFadeOut, block);
            }
        }
Esempio n. 3
0
            public override SyntaxNode VisitBlock(BlockSyntax node)
            {
                if (_statementsInfo.Parent == null &&
                    node.IsParentKind(SyntaxKind.SwitchSection))
                {
                    return(Rewrite(new StatementListInfo(node)));
                }

                _statementsInfo = new StatementListInfo(node);

                IfStatementSyntax ifStatement = FindFixableIfStatement(_statementsInfo.Statements, _jumpKind);

                if (ReduceIfNestingAnalysis.IsFixable(ifStatement))
                {
                    return(Rewrite(_statementsInfo, ifStatement));
                }

                return(node);
            }
Esempio n. 4
0
            public override SyntaxNode VisitBlock(BlockSyntax node)
            {
                if (_container.Node == null &&
                    node.IsParentKind(SyntaxKind.SwitchSection))
                {
                    return(Rewrite(new StatementContainer(node)));
                }

                _container = new StatementContainer(node);

                IfStatementSyntax ifStatement = FindFixableIfStatement(_container.Statements, _jumpKind);

                if (IsFixable(ifStatement))
                {
                    return(Rewrite(_container, ifStatement));
                }

                return(node);
            }