Beispiel #1
0
        private static ConditionalExpressionSyntax InvertConditionalExpression(
            ConditionalExpressionSyntax conditionalExpression,
            SemanticModel semanticModel,
            CancellationToken cancellationToken)
        {
            ExpressionSyntax whenTrue  = conditionalExpression.WhenTrue;
            ExpressionSyntax whenFalse = conditionalExpression.WhenFalse;

            if (whenTrue?.IsKind(SyntaxKind.ThrowExpression) == false)
            {
                whenTrue = LogicallyNegateAndParenthesize(whenTrue, semanticModel, cancellationToken);
            }

            if (whenFalse?.IsKind(SyntaxKind.ThrowExpression) == false)
            {
                whenFalse = LogicallyNegateAndParenthesize(whenFalse, semanticModel, cancellationToken);
            }

            ConditionalExpressionSyntax newConditionalExpression = conditionalExpression.Update(
                conditionalExpression.Condition,
                conditionalExpression.QuestionToken,
                whenTrue,
                conditionalExpression.ColonToken,
                whenFalse);

            return(newConditionalExpression.WithTriviaFrom(conditionalExpression));
        }
Beispiel #2
0
        private static ExpressionSyntax NegateConditionalExpression(ConditionalExpressionSyntax conditionalExpression)
        {
            ExpressionSyntax whenTrue  = conditionalExpression.WhenTrue;
            ExpressionSyntax whenFalse = conditionalExpression.WhenFalse;

            if (whenTrue?.IsKind(SyntaxKind.ThrowExpression) == false)
            {
                whenTrue = whenTrue
                           .LogicallyNegate()
                           .ParenthesizeIfNecessary(SyntaxKind.ConditionalExpression)
                           .WithTriviaFrom(whenTrue);
            }

            if (whenFalse?.IsKind(SyntaxKind.ThrowExpression) == false)
            {
                whenFalse = whenFalse
                            .LogicallyNegate()
                            .ParenthesizeIfNecessary(SyntaxKind.ConditionalExpression)
                            .WithTriviaFrom(whenFalse);
            }

            ConditionalExpressionSyntax newConditionalExpression = conditionalExpression.Update(
                conditionalExpression.Condition,
                conditionalExpression.QuestionToken,
                whenTrue,
                conditionalExpression.ColonToken,
                whenFalse);

            return(newConditionalExpression.WithTriviaFrom(conditionalExpression));
        }
Beispiel #3
0
        public static Task <Document> RefactorAsync(
            Document document,
            BinaryExpressionSyntax binaryExpression,
            CancellationToken cancellationToken = default)
        {
            ExpressionSyntax left  = binaryExpression.Left.WithoutTrivia();
            ExpressionSyntax right = binaryExpression.Right.WithoutTrivia();

            ExpressionSyntax expression = left.WalkDownParentheses();

            ExpressionSyntax condition;
            ExpressionSyntax whenTrue;

            if (expression.IsKind(SyntaxKind.AsExpression))
            {
                var asExpression = (BinaryExpressionSyntax)expression;

                condition = IsExpression(
                    asExpression.Left,
                    Token(SyntaxKind.IsKeyword).WithTriviaFrom(asExpression.OperatorToken),
                    asExpression.Right);

                whenTrue = CastExpression((TypeSyntax)asExpression.Right, asExpression.Left.Parenthesize());
            }
            else
            {
                condition = NotEqualsExpression(left, NullLiteralExpression());
                whenTrue  = left;
            }

            ConditionalExpressionSyntax conditionalExpression = ConditionalExpression(
                condition.ParenthesizeIf(!CSharpFacts.IsSingleTokenExpression(condition.Kind()), simplifiable: false),
                whenTrue.Parenthesize(),
                right.Parenthesize());

            conditionalExpression = conditionalExpression
                                    .WithTriviaFrom(binaryExpression)
                                    .WithFormatterAnnotation();

            return(document.ReplaceNodeAsync(binaryExpression, conditionalExpression, cancellationToken));
        }