示例#1
0
        public static async Task <Document> RefactorAsync(
            Document document,
            IfStatementSyntax ifStatement,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            ElseClauseSyntax elseClause = ifStatement.Else;
            StatementSyntax  whenTrue   = ifStatement.Statement;
            StatementSyntax  whenFalse  = elseClause.Statement;

            SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            ElseClauseSyntax newElseClause = null;

            if ((whenFalse as BlockSyntax)?.Statements.Any() != false)
            {
                newElseClause = elseClause.WithStatement(whenTrue.WithTriviaFrom(whenFalse));
            }

            IfStatementSyntax newIfStatement = ifStatement
                                               .WithCondition(Negator.LogicallyNegate(ifStatement.Condition, semanticModel, cancellationToken))
                                               .WithStatement(whenFalse.WithTriviaFrom(whenTrue))
                                               .WithElse(newElseClause)
                                               .WithFormatterAnnotation();

            return(await document.ReplaceNodeAsync(ifStatement, newIfStatement, cancellationToken).ConfigureAwait(false));
        }
示例#2
0
 private static Task <Document> RefactorAsync(
     Document document,
     ExpressionSyntax expression,
     CancellationToken cancellationToken)
 {
     return(document.ReplaceNodeAsync(expression, Negator.LogicallyNegate(expression), cancellationToken));
 }
        private static async Task <Document> InvertIfElseAsync(
            Document document,
            IfStatementSyntax ifStatement,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();

            ElseClauseSyntax elseClause = ifStatement.Else;
            StatementSyntax  whenTrue   = ifStatement.Statement;
            StatementSyntax  whenFalse  = elseClause.Statement;

            SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            IfStatementSyntax newIfStatement = ifStatement.Update(
                ifKeyword: ifStatement.IfKeyword,
                openParenToken: ifStatement.OpenParenToken,
                condition: Negator.LogicallyNegate(ifStatement.Condition, semanticModel, cancellationToken),
                closeParenToken: ifStatement.CloseParenToken,
                statement: whenFalse.WithTriviaFrom(whenTrue),
                @else: elseClause.WithStatement(whenTrue.WithTriviaFrom(whenFalse)));

            newIfStatement = newIfStatement.WithFormatterAnnotation();

            return(await document.ReplaceNodeAsync(ifStatement, newIfStatement, cancellationToken).ConfigureAwait(false));
        }
 private static async Task <Document> RefactorAsync(
     Document document,
     ExpressionSyntax expression,
     CancellationToken cancellationToken)
 {
     return(await document.ReplaceNodeAsync(expression, Negator.LogicallyNegate(expression), cancellationToken).ConfigureAwait(false));
 }
示例#5
0
 private void EliminateViaIf(IfStatement gotoStatement, Statement labeledStatement)
 {
     V_0 = labeledStatement.get_Parent() as BlockStatement;
     V_1 = V_0.get_Statements().IndexOf(gotoStatement);
     V_2 = V_0.get_Statements().IndexOf(labeledStatement);
     if (V_1 == V_2 - 1)
     {
         V_0.get_Statements().RemoveAt(V_1);
         return;
     }
     V_3 = this.CollectStatements(V_1 + 1, V_2, V_0);
     V_4 = Negator.Negate(gotoStatement.get_Condition(), this.typeSystem);
     while (V_3.get_Statements().get_Item(0) as IfStatement != null)
     {
         V_5 = V_3.get_Statements().get_Item(0) as IfStatement;
         if (!this.AreEqual(V_5.get_Condition(), V_4) || V_5.get_Else() != null)
         {
             break;
         }
         V_3.get_Statements().RemoveAt(0);
         V_6 = 0;
         while (V_6 < V_5.get_Then().get_Statements().get_Count())
         {
             V_3.AddStatement(V_5.get_Then().get_Statements().get_Item(V_6));
             V_6 = V_6 + 1;
         }
     }
     gotoStatement.set_Then(V_3);
     gotoStatement.set_Condition(V_4);
     return;
 }
示例#6
0
        public static async Task <Document> RefactorAsync(
            Document document,
            ConditionalExpressionSyntax conditionalExpression,
            CancellationToken cancellationToken)
        {
            ExpressionSyntax condition = conditionalExpression.Condition;

            ExpressionSyntax newNode = (conditionalExpression.WhenTrue.IsKind(SyntaxKind.TrueLiteralExpression))
                ? condition
                : Negator.LogicallyNegate(condition);

            TextSpan span = TextSpan.FromBounds(
                conditionalExpression.Condition.Span.End,
                conditionalExpression.FullSpan.End);

            IEnumerable <SyntaxTrivia> trivia = conditionalExpression.DescendantTrivia(span);

            if (trivia.Any(f => !f.IsWhitespaceOrEndOfLineTrivia()))
            {
                newNode = newNode.WithTrailingTrivia(trivia);
            }
            else
            {
                newNode = newNode.WithoutTrailingTrivia();
            }

            return(await document.ReplaceNodeAsync(conditionalExpression, newNode, cancellationToken).ConfigureAwait(false));
        }
示例#7
0
            public override SyntaxNode VisitBlock(BlockSyntax node)
            {
                node = (BlockSyntax)base.VisitBlock(node);

                SyntaxList <StatementSyntax> statements = node.Statements;

                var ifStatement = statements.LastOrDefault(f => !f.IsKind(SyntaxKind.LocalFunctionStatement)) as IfStatementSyntax;

                if (ifStatement != null &&
                    IsFixable(ifStatement) &&
                    ((BlockSyntax)ifStatement.Parent).Statements.IsLastStatement(ifStatement, skipLocalFunction: true))
                {
                    var block = (BlockSyntax)ifStatement.Statement;

                    ExpressionSyntax newCondition = Negator.LogicallyNegate(ifStatement.Condition);

                    IfStatementSyntax newIfStatement = ifStatement
                                                       .WithCondition(newCondition)
                                                       .WithStatement(block.WithStatements(SingletonList(_jumpStatement)))
                                                       .WithFormatterAnnotation();

                    int index = statements.IndexOf(ifStatement);

                    SyntaxList <StatementSyntax> newStatements = statements
                                                                 .ReplaceAt(index, newIfStatement)
                                                                 .InsertRange(index + 1, block.Statements.Select(f => f.WithFormatterAnnotation()));

                    node = node.WithStatements(newStatements);
                }

                return(node);
            }
示例#8
0
 private void CleanupEmptyIfs(BlockStatement body)
 {
     do
     {
         V_0 = this.emptyThenIfs.GetEnumerator();
         try
         {
             while (V_0.MoveNext())
             {
                 V_1 = V_0.get_Current();
                 if (V_1.get_Else() == null || V_1.get_Else().get_Statements().get_Count() == 0)
                 {
                     dummyVar0 = (V_1.get_Parent() as BlockStatement).get_Statements().Remove(V_1);
                 }
                 else
                 {
                     V_1.set_Then(V_1.get_Else());
                     V_1.set_Else(null);
                     dummyVar1 = Negator.Negate(V_1.get_Condition(), this.typeSystem);
                 }
             }
         }
         finally
         {
             ((IDisposable)V_0).Dispose();
         }
         this.emptyThenIfs = new List <IfStatement>();
         this.Visit(body);
     }while (this.emptyThenIfs.get_Count() != 0);
     return;
 }
示例#9
0
        public static async Task <Document> RefactorAsync(
            Document document,
            ConditionalExpressionSyntax conditionalExpression,
            CancellationToken cancellationToken)
        {
            SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            ExpressionSyntax condition = conditionalExpression.Condition;

            ExpressionSyntax newNode = (conditionalExpression.WhenTrue.WalkDownParentheses().IsKind(SyntaxKind.TrueLiteralExpression))
                ? condition
                : Negator.LogicallyNegate(condition, semanticModel, cancellationToken);

            SyntaxTriviaList trailingTrivia = conditionalExpression
                                              .DescendantTrivia(TextSpan.FromBounds(condition.Span.End, conditionalExpression.Span.End))
                                              .ToSyntaxTriviaList()
                                              .EmptyIfWhitespace()
                                              .AddRange(conditionalExpression.GetTrailingTrivia());

            newNode = newNode
                      .WithLeadingTrivia(conditionalExpression.GetLeadingTrivia())
                      .WithTrailingTrivia(trailingTrivia);

            return(await document.ReplaceNodeAsync(conditionalExpression, newNode, cancellationToken).ConfigureAwait(false));
        }
            public override SyntaxNode VisitBlock(BlockSyntax node)
            {
                _block = node;

                if (node.LastStatementOrDefault(skipLocalFunction: true) is IfStatementSyntax ifStatement &&
                    IsFixable(ifStatement))
                {
                    SyntaxList <StatementSyntax> statements = node.Statements;

                    int index = statements.IndexOf(ifStatement);

                    ifStatement = (IfStatementSyntax)VisitIfStatement(ifStatement);

                    var block = (BlockSyntax)ifStatement.Statement;

                    ExpressionSyntax newCondition = Negator.LogicallyNegate(ifStatement.Condition);

                    IfStatementSyntax newIfStatement = ifStatement
                                                       .WithCondition(newCondition)
                                                       .WithStatement(block.WithStatements(SingletonList(_jumpStatement)))
                                                       .WithFormatterAnnotation();

                    SyntaxList <StatementSyntax> newStatements = statements
                                                                 .ReplaceAt(index, newIfStatement)
                                                                 .InsertRange(index + 1, block.Statements.Select(f => f.WithFormatterAnnotation()));

                    node = node.WithStatements(newStatements);
                }

                return(node);
            }
示例#11
0
 public void Negate(TypeSystem typeSystem)
 {
     V_0 = this.get_TrueCFGSuccessor();
     this.set_TrueCFGSuccessor(this.get_FalseCFGSuccessor());
     this.set_FalseCFGSuccessor(V_0);
     this.set_ConditionExpression(Negator.Negate(this.get_ConditionExpression(), typeSystem));
     return;
 }
        /// <summary>
        /// Negates the expression of the construct and switches the true and false successors.
        /// </summary>
        public void Negate(TypeSystem typeSystem)
        {
            CFGBlockLogicalConstruct successorHolder = TrueCFGSuccessor;

            TrueCFGSuccessor  = FalseCFGSuccessor;
            FalseCFGSuccessor = successorHolder;

            ConditionExpression = Negator.Negate(ConditionExpression, typeSystem);
        }
示例#13
0
        public static Task <Document> RefactorAsync(
            Document document,
            BinaryExpressionSyntax binaryExpression,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            ExpressionSyntax newNode = Negator.LogicallyNegate(binaryExpression)
                                       .WithFormatterAnnotation();

            return(document.ReplaceNodeAsync(binaryExpression, newNode, cancellationToken));
        }
        private static async Task <Document> RefactorAsync(
            Document document,
            ExpressionSyntax expression,
            CancellationToken cancellationToken)
        {
            SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            ExpressionSyntax newNode = Negator.LogicallyNegate(expression, semanticModel, cancellationToken);

            return(await document.ReplaceNodeAsync(expression, newNode, cancellationToken).ConfigureAwait(false));
        }
示例#15
0
        private static Task <Document> IfToReturnWithExpressionAsync(
            Document document,
            IfToReturnWithExpressionAnalysis analysis,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            ExpressionSyntax expression = analysis.Expression;

            if (analysis.Negate)
            {
                expression = Negator.LogicallyNegate(expression, analysis.SemanticModel, cancellationToken);
            }

            StatementSyntax statement;

            if (analysis.IsYield)
            {
                statement = YieldReturnStatement(expression);
            }
            else
            {
                statement = ReturnStatement(expression);
            }

            IfStatementSyntax ifStatement = analysis.IfStatement;

            if (ifStatement.IsSimpleIf())
            {
                StatementListInfo statementsInfo = SyntaxInfo.StatementListInfo(ifStatement);

                SyntaxList <StatementSyntax> statements = statementsInfo.Statements;

                int index = statements.IndexOf(ifStatement);

                StatementSyntax newNode = statement
                                          .WithLeadingTrivia(ifStatement.GetLeadingTrivia())
                                          .WithTrailingTrivia(statements[index + 1].GetTrailingTrivia())
                                          .WithFormatterAnnotation();

                SyntaxList <StatementSyntax> newStatements = statements
                                                             .RemoveAt(index)
                                                             .ReplaceAt(index, newNode);

                return(document.ReplaceStatementsAsync(statementsInfo, newStatements, cancellationToken));
            }
            else
            {
                StatementSyntax newNode = statement
                                          .WithTriviaFrom(ifStatement)
                                          .WithFormatterAnnotation();

                return(document.ReplaceNodeAsync(ifStatement, newNode, cancellationToken));
            }
        }
示例#16
0
        public static async Task <Document> RefactorAsync(
            Document document,
            BinaryExpressionSyntax binaryExpression,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            ExpressionSyntax newNode = Negator.LogicallyNegate(binaryExpression, semanticModel, cancellationToken);

            newNode = newNode.WithFormatterAnnotation();

            return(await document.ReplaceNodeAsync(binaryExpression, newNode, cancellationToken).ConfigureAwait(false));
        }
        public static Task <Document> RefactorAsync(
            Document document,
            ConditionalExpressionSyntax conditionalExpression,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            ConditionalExpressionSyntax newConditionalExpression = conditionalExpression
                                                                   .WithCondition(Negator.LogicallyNegate(conditionalExpression.Condition))
                                                                   .WithWhenTrue(conditionalExpression.WhenFalse.WithTriviaFrom(conditionalExpression.WhenTrue))
                                                                   .WithWhenFalse(conditionalExpression.WhenTrue.WithTriviaFrom(conditionalExpression.WhenFalse))
                                                                   .WithFormatterAnnotation();

            return(document.ReplaceNodeAsync(conditionalExpression, newConditionalExpression, cancellationToken));
        }
示例#18
0
        public static ExpressionSyntax GetBooleanExpression(ExpressionSyntax condition, ExpressionSyntax expression1, ExpressionSyntax expression2)
        {
            switch (expression1.Kind())
            {
            case SyntaxKind.TrueLiteralExpression:
            {
                switch (expression2.Kind())
                {
                case SyntaxKind.TrueLiteralExpression:
                    return(expression2);

                case SyntaxKind.FalseLiteralExpression:
                    return(condition);

                default:
                    return(LogicalOrExpression(condition, expression2));
                }
            }

            case SyntaxKind.FalseLiteralExpression:
            {
                switch (expression2.Kind())
                {
                case SyntaxKind.TrueLiteralExpression:
                    return(Negator.LogicallyNegate(condition));

                case SyntaxKind.FalseLiteralExpression:
                    return(expression2);

                default:
                    return(LogicalOrExpression(Negator.LogicallyNegate(condition), expression2));
                }
            }

            default:
            {
                switch (expression2.Kind())
                {
                case SyntaxKind.TrueLiteralExpression:
                    return(LogicalOrExpression(Negator.LogicallyNegate(condition), expression1));

                case SyntaxKind.FalseLiteralExpression:
                    return(LogicalAndExpression(condition.Parenthesize().WithSimplifierAnnotation(), expression1.Parenthesize().WithSimplifierAnnotation()));

                default:
                    return(LogicalOrExpression(ParenthesizedExpression(LogicalAndExpression(condition, expression1)), expression2));
                }
            }
            }
        }
        public static async Task <Document> RefactorAsync(
            Document document,
            BinaryExpressionSyntax binaryExpression,
            CancellationToken cancellationToken)
        {
            ExpressionSyntax left          = binaryExpression.Left;
            ExpressionSyntax right         = binaryExpression.Right;
            SyntaxToken      operatorToken = binaryExpression.OperatorToken;

            ExpressionSyntax newNode = binaryExpression;

            TextSpan span = TextSpan.FromBounds(left.Span.End, right.Span.Start);

            IEnumerable <SyntaxTrivia> trivia = binaryExpression.DescendantTrivia(span);

            bool isWhiteSpaceOrEndOfLine = trivia.All(f => f.IsWhitespaceOrEndOfLineTrivia());

            if (left.IsBooleanLiteralExpression())
            {
                SyntaxTriviaList leadingTrivia = binaryExpression.GetLeadingTrivia();

                if (!isWhiteSpaceOrEndOfLine)
                {
                    leadingTrivia = leadingTrivia.AddRange(trivia);
                }

                newNode = Negator.LogicallyNegate(right)
                          .WithLeadingTrivia(leadingTrivia);
            }
            else if (right.IsBooleanLiteralExpression())
            {
                SyntaxTriviaList trailingTrivia = binaryExpression.GetTrailingTrivia();

                if (!isWhiteSpaceOrEndOfLine)
                {
                    trailingTrivia = trailingTrivia.InsertRange(0, trivia);
                }

                newNode = Negator.LogicallyNegate(left)
                          .WithTrailingTrivia(trailingTrivia);
            }
#if DEBUG
            else
            {
                Debug.Assert(false, binaryExpression.ToString());
            }
#endif

            return(await document.ReplaceNodeAsync(binaryExpression, newNode.WithFormatterAnnotation(), cancellationToken).ConfigureAwait(false));
        }
 public override ICodeNode VisitUnaryExpression(UnaryExpression node)
 {
     if (this.status == 2)
     {
         throw new Exception("Invalid state");
     }
     V_0 = node.get_Operand();
     node.set_Operand((Expression)this.Visit(node.get_Operand()));
     if (node.get_Operator() != 1 || V_0 == node.get_Operand())
     {
         return(node);
     }
     return(Negator.Negate(node.get_Operand(), this.typeSystem));
 }
        private static ReturnStatementSyntax CreateReturnStatement(IfStatementSyntax ifStatement)
        {
            ExpressionSyntax expression = ifStatement.Condition;

            if (GetBooleanLiteral(ifStatement.Statement).IsKind(SyntaxKind.FalseLiteralExpression))
            {
                expression = Negator.LogicallyNegate(expression);
            }

            return(ReturnStatement(
                       ReturnKeyword().WithTrailingTrivia(Space),
                       expression,
                       SemicolonToken()));
        }
        public static ExpressionSyntax GetExpression(ExpressionSyntax condition, ExpressionSyntax expression1, ExpressionSyntax expression2)
        {
            switch (expression1.Kind())
            {
            case SyntaxKind.TrueLiteralExpression:
            {
                switch (expression2.Kind())
                {
                case SyntaxKind.TrueLiteralExpression:
                    return(expression2);

                case SyntaxKind.FalseLiteralExpression:
                    return(condition);

                default:
                    return(LogicalOrExpression(condition, expression2));
                }
            }

            case SyntaxKind.FalseLiteralExpression:
            {
                switch (expression2.Kind())
                {
                case SyntaxKind.TrueLiteralExpression:
                    return(Negator.LogicallyNegate(condition));

                case SyntaxKind.FalseLiteralExpression:
                    return(expression2);

                default:
                    return(LogicalOrExpression(Negator.LogicallyNegate(condition), expression2));
                }
            }

            default:
            {
                switch (expression2.Kind())
                {
                case SyntaxKind.TrueLiteralExpression:
                    return(LogicalOrExpression(Negator.LogicallyNegate(condition), expression1));

                case SyntaxKind.FalseLiteralExpression:
                    return(LogicalAndExpression(condition, expression1, addParenthesesIfNecessary: true));

                default:
                    return(LogicalOrExpression(ParenthesizedExpression(LogicalAndExpression(condition, expression1)), expression2));
                }
            }
            }
        }
示例#23
0
        public static ExpressionSyntax GetBooleanExpression(ExpressionSyntax condition, ExpressionSyntax expression1, ExpressionSyntax expression2)
        {
            switch (expression1.Kind())
            {
            case SyntaxKind.TrueLiteralExpression:
            {
                switch (expression2.Kind())
                {
                case SyntaxKind.TrueLiteralExpression:
                    return(expression2);

                case SyntaxKind.FalseLiteralExpression:
                    return(condition);

                default:
                    return(LogicalOrExpression(condition, expression2));
                }
            }

            case SyntaxKind.FalseLiteralExpression:
            {
                switch (expression2.Kind())
                {
                case SyntaxKind.TrueLiteralExpression:
                    return(Negator.LogicallyNegate(condition));

                case SyntaxKind.FalseLiteralExpression:
                    return(expression2);

                default:
                    return(LogicalAndExpression(Negator.LogicallyNegate(condition), expression2));
                }
            }

            default:
            {
                switch (expression2.Kind())
                {
                case SyntaxKind.TrueLiteralExpression:
                    return(LogicalOrExpression(Negator.LogicallyNegate(condition), expression1));

                case SyntaxKind.FalseLiteralExpression:
                    return(LogicalAndExpression(condition, expression1));

                default:
                    throw new InvalidOperationException();
                }
            }
            }
        }
示例#24
0
        public static Task <Document> RefactorAsync(
            Document document,
            ConditionalExpressionSyntax conditionalExpression,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            ConditionalExpressionSyntax newNode = conditionalExpression.Update(
                condition: Negator.LogicallyNegate(conditionalExpression.Condition),
                questionToken: conditionalExpression.QuestionToken,
                whenTrue: conditionalExpression.WhenFalse.WithTriviaFrom(conditionalExpression.WhenTrue),
                colonToken: conditionalExpression.ColonToken,
                whenFalse: conditionalExpression.WhenTrue.WithTriviaFrom(conditionalExpression.WhenFalse));

            newNode = newNode.WithFormatterAnnotation();

            return(document.ReplaceNodeAsync(conditionalExpression, newNode, cancellationToken));
        }
示例#25
0
        public static Task <Document> RefactorAsync(
            Document document,
            IfStatementSyntax ifStatement,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            StatementSyntax trueStatement = ifStatement.Statement;

            StatementSyntax falseStatement = ifStatement.Else.Statement;

            IfStatementSyntax newIfStatement = ifStatement
                                               .WithCondition(Negator.LogicallyNegate(ifStatement.Condition))
                                               .WithStatement(falseStatement.WithTriviaFrom(trueStatement))
                                               .WithElse(ifStatement.Else.WithStatement(trueStatement.WithTriviaFrom(falseStatement)))
                                               .WithFormatterAnnotation();

            return(document.ReplaceNodeAsync(ifStatement, newIfStatement, cancellationToken));
        }
示例#26
0
        public override ICodeNode VisitUnaryExpression(UnaryExpression node)
        {
            if (status == InliningResult.Abort)
            {
                //sanity check
                throw new Exception("Invalid state");
            }

            Expression originalOperand = node.Operand;

            node.Operand = (Expression)Visit(node.Operand);
            if (node.Operator == UnaryOperator.LogicalNot && originalOperand != node.Operand)
            {
                return(Negator.Negate(node.Operand, typeSystem));
            }
            return(node);
        }
            public override SyntaxNode VisitBlock(BlockSyntax node)
            {
                _block = node;

                if (node.LastStatementOrDefault(skipLocalFunction: true) is IfStatementSyntax ifStatement &&
                    IsFixable(ifStatement))
                {
                    SyntaxList <StatementSyntax> statements = node.Statements;

                    int index = statements.IndexOf(ifStatement);

                    if (_recursive)
                    {
                        ifStatement = (IfStatementSyntax)VisitIfStatement(ifStatement);
                    }

                    var block = (BlockSyntax)ifStatement.Statement;

                    ExpressionSyntax newCondition = Negator.LogicallyNegate(ifStatement.Condition);

                    BlockSyntax newBlock = block.WithStatements(SingletonList(_jumpStatement));

                    if (!block
                        .Statements
                        .First()
                        .GetLeadingTrivia()
                        .Any(f => f.IsEndOfLineTrivia()))
                    {
                        newBlock = newBlock.WithCloseBraceToken(newBlock.CloseBraceToken.AppendToTrailingTrivia(NewLine()));
                    }

                    IfStatementSyntax newIfStatement = ifStatement
                                                       .WithCondition(newCondition)
                                                       .WithStatement(newBlock)
                                                       .WithFormatterAnnotation();

                    SyntaxList <StatementSyntax> newStatements = statements
                                                                 .ReplaceAt(index, newIfStatement)
                                                                 .InsertRange(index + 1, block.Statements.Select(f => f.WithFormatterAnnotation()));

                    node = node.WithStatements(newStatements);
                }

                return(node);
            }
        public static Task <Document> RefactorAsync(
            Document document,
            InvocationExpressionSyntax invocationExpression,
            string memberName,
            ExpressionSyntax expression,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            var memberAccessExpression = (MemberAccessExpressionSyntax)invocationExpression.Expression;

            MemberAccessExpressionSyntax newMemberAccessExpression = memberAccessExpression
                                                                     .WithName(SyntaxFactory.IdentifierName(memberName).WithTriviaFrom(memberAccessExpression.Name));

            InvocationExpressionSyntax newNode = invocationExpression
                                                 .ReplaceNode(expression, Negator.LogicallyNegate(expression))
                                                 .WithExpression(newMemberAccessExpression);

            return(document.ReplaceNodeAsync(invocationExpression, newNode, cancellationToken));
        }
示例#29
0
        private static Task <Document> IfElseToAssignmentWithConditionAsync(
            Document document,
            IfElseToAssignmentWithConditionAnalysis analysis,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            ExpressionSyntax right = analysis.Right.WithoutTrivia();

            if (analysis.Negate)
            {
                right = Negator.LogicallyNegate(right, analysis.SemanticModel, cancellationToken);
            }

            ExpressionStatementSyntax newNode = SimpleAssignmentStatement(analysis.Left.WithoutTrivia(), right)
                                                .WithTriviaFrom(analysis.IfStatement)
                                                .WithFormatterAnnotation();

            return(document.ReplaceNodeAsync(analysis.IfStatement, newNode, cancellationToken));
        }
示例#30
0
            private SyntaxNode Rewrite(StatementListInfo statementsInfo, IfStatementSyntax ifStatement)
            {
                SyntaxList <StatementSyntax> statements = statementsInfo.Statements;

                int index = statements.IndexOf(ifStatement);

                ExpressionSyntax newCondition = Negator.LogicallyNegate(ifStatement.Condition, _semanticModel, _cancellationToken);

                if (_recursive)
                {
                    ifStatement = (IfStatementSyntax)VisitIfStatement(ifStatement);
                }

                var block = (BlockSyntax)ifStatement.Statement;

                BlockSyntax newBlock = block.WithStatements(SingletonList(_jumpStatement));

                if (!block
                    .Statements
                    .First()
                    .GetLeadingTrivia()
                    .Any(f => f.IsEndOfLineTrivia()))
                {
                    newBlock = newBlock.WithCloseBraceToken(newBlock.CloseBraceToken.AppendToTrailingTrivia(NewLine()));
                }

                IfStatementSyntax newIfStatement = ifStatement
                                                   .WithCondition(newCondition)
                                                   .WithStatement(newBlock)
                                                   .WithFormatterAnnotation();

                if (CSharpFacts.IsJumpStatementOrYieldBreakStatement(statements.Last().Kind()) &&
                    CSharpFacts.IsJumpStatementOrYieldBreakStatement(block.Statements.Last().Kind()))
                {
                    statements = statements.RemoveAt(statements.Count - 1);
                }

                SyntaxList <StatementSyntax> newStatements = statements
                                                             .ReplaceAt(index, newIfStatement)
                                                             .InsertRange(index + 1, block.Statements.Select(f => f.WithFormatterAnnotation()));

                return(statementsInfo.WithStatements(newStatements).Parent);
            }