private static IfStatementSyntax CreateNewIfStatement(IfStatementSyntax ifStatement)
        {
            IEnumerable <StatementSyntax> statements = ifStatement
                                                       .AsCascade()
                                                       .Select(ifOrElse =>
            {
                StatementSyntax statement = ifOrElse.Statement;

                if (statement is BlockSyntax block)
                {
                    statement = block.Statements.Last();
                }

                return(statement);
            });

            return(ifStatement.ReplaceNodes(
                       statements,
                       (statement, _) =>
            {
                if (statement.IsKind(SyntaxKind.ThrowStatement))
                {
                    return statement;
                }

                var expressionStatement = (ExpressionStatementSyntax)statement;

                var assignment = (AssignmentExpressionSyntax)expressionStatement.Expression;

                return ReturnStatement(assignment.Right).WithTriviaFrom(statement);
            }));
        }
Example #2
0
 public override SyntaxNode VisitIfStatement(IfStatementSyntax node)
 {
     return(node.ReplaceNodes(node.DescendantNodes().OfType <ElementAccessExpressionSyntax>(),
                              (o, n) =>
     {
         if (o.Parent.IsKind(SyntaxKind.EqualsExpression))
         {
             return o;
         }
         return SyntaxFactory.BinaryExpression(SyntaxKind.EqualsExpression, o,
                                               SyntaxFactory.LiteralExpression(SyntaxKind.TrueLiteralExpression));
     }));
 }
        private static IfStatementSyntax CreateNewIfStatement(IfStatementSyntax ifStatement)
        {
            IEnumerable <ExpressionStatementSyntax> expressionStatements = ifStatement
                                                                           .AsCascade()
                                                                           .Select(ifOrElse => (ExpressionStatementSyntax)GetLastStatementOrDefault(ifOrElse.Statement));

            return(ifStatement.ReplaceNodes(
                       expressionStatements,
                       (f, _) =>
            {
                var assignment = (AssignmentExpressionSyntax)f.Expression;

                return ReturnStatement(assignment.Right).WithTriviaFrom(f);
            }));
        }