private static Task <Document> RefactorAsync(
            Document document,
            IfStatementSyntax ifStatement,
            CancellationToken cancellationToken)
        {
            return(document.ReplaceNodeAsync(ifStatement, GetNewStatements(), cancellationToken));

            IEnumerable <StatementSyntax> GetNewStatements()
            {
                ElseClauseSyntax parentElse = null;

                foreach (IfStatementOrElseClause ifOrElse in ifStatement.AsCascade())
                {
                    if (ifOrElse.IsIf)
                    {
                        IfStatementSyntax newIfStatement = ifOrElse.AsIf();

                        ElseClauseSyntax elseClause = newIfStatement.Else;

                        newIfStatement = newIfStatement.WithElse(null);

                        if (parentElse != null)
                        {
                            newIfStatement = newIfStatement.PrependToLeadingTrivia(parentElse.GetLeadingTrivia());
                        }

                        if (elseClause != null)
                        {
                            newIfStatement = newIfStatement.AppendToTrailingTrivia(CSharpFactory.NewLine());
                        }

                        yield return(newIfStatement.WithFormatterAnnotation());

                        parentElse = ifStatement.Else;
                    }
                    else
                    {
                        StatementSyntax statement = ifOrElse.Statement;

                        if (statement is BlockSyntax block)
                        {
                            foreach (StatementSyntax newStatement in block.Statements.Select(f => f.WithFormatterAnnotation()))
                            {
                                yield return(newStatement);
                            }
                        }
                        else
                        {
                            yield return(statement);
                        }
                    }
                }
            }
        }
        private static Task <Document> RefactorAsync(
            Document document,
            IfStatementSyntax ifStatement,
            CancellationToken cancellationToken)
        {
            var statements = new List <StatementSyntax>();

            IfStatementSyntax topIfStatement = ifStatement;

            ElseClauseSyntax elseClause = null;

            while (true)
            {
                IfStatementSyntax newIfStatement = ifStatement.WithElse(null);

                if (elseClause != null)
                {
                    newIfStatement = newIfStatement.PrependToLeadingTrivia(elseClause.GetLeadingTrivia());
                }

                newIfStatement = newIfStatement
                                 .AppendToTrailingTrivia(CSharpFactory.NewLine())
                                 .WithFormatterAnnotation();

                statements.Add(newIfStatement);

                elseClause = ifStatement.Else;

                StatementSyntax statement = elseClause.Statement;

                SyntaxKind kind = statement.Kind();

                if (kind != SyntaxKind.IfStatement)
                {
                    if (kind == SyntaxKind.Block)
                    {
                        statements.AddRange(((BlockSyntax)statement).Statements.Select(f => f.WithFormatterAnnotation()));
                    }
                    else
                    {
                        statements.Add(statement);
                    }

                    break;
                }

                ifStatement = ((IfStatementSyntax)statement);
            }

            return(document.ReplaceNodeAsync(topIfStatement, statements, cancellationToken));
        }