Exemplo n.º 1
0
        public static bool TryCreate(
            SyntaxNode node,
            SemanticModel semanticModel,
            out NullCheckExpression result,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            if (node != null)
            {
                var expression = node as ExpressionSyntax;

                if (expression != null)
                {
                    expression = expression.WalkDownParentheses();

                    SyntaxKind kind = expression.Kind();

                    switch (kind)
                    {
                    case SyntaxKind.EqualsExpression:
                    case SyntaxKind.NotEqualsExpression:
                    {
                        var binaryExpression = (BinaryExpressionSyntax)expression;

                        ExpressionSyntax left  = binaryExpression.Left?.WalkDownParentheses();
                        ExpressionSyntax right = binaryExpression.Right?.WalkDownParentheses();

                        return(TryCreate(binaryExpression, kind, left, right, semanticModel, cancellationToken, out result) ||
                               TryCreate(binaryExpression, kind, right, left, semanticModel, cancellationToken, out result));
                    }

                    case SyntaxKind.SimpleMemberAccessExpression:
                    {
                        if (SemanticUtilities.IsPropertyOfNullableOfT(expression, "HasValue", semanticModel, cancellationToken))
                        {
                            var memberAccessExpression = (MemberAccessExpressionSyntax)expression;

                            result = new NullCheckExpression(expression, memberAccessExpression.Expression, NullCheckKind.HasValue);
                            return(true);
                        }

                        break;
                    }

                    case SyntaxKind.LogicalNotExpression:
                    {
                        var logicalNotExpression = (PrefixUnaryExpressionSyntax)expression;

                        ExpressionSyntax operand = logicalNotExpression.Operand?.WalkDownParentheses();

                        if (operand?.IsKind(SyntaxKind.SimpleMemberAccessExpression) == true &&
                            SemanticUtilities.IsPropertyOfNullableOfT(operand, "HasValue", semanticModel, cancellationToken))
                        {
                            var memberAccessExpression = (MemberAccessExpressionSyntax)operand;

                            result = new NullCheckExpression(expression, memberAccessExpression.Expression, NullCheckKind.NotHasValue);
                            return(true);
                        }

                        break;
                    }
                    }
                }
            }

            result = default(NullCheckExpression);
            return(false);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Returns the string representation of the underlying syntax, not including its leading and trailing trivia.
 /// </summary>
 /// <returns></returns>
 public override string ToString()
 {
     return(NullCheckExpression?.ToString() ?? "");
 }
Exemplo n.º 3
0
        private static bool TryCreate(
            BinaryExpressionSyntax binaryExpression,
            SyntaxKind kind,
            ExpressionSyntax expression1,
            ExpressionSyntax expression2,
            SemanticModel semanticModel,
            CancellationToken cancellationToken,
            out NullCheckExpression result)
        {
            if (expression1 != null &&
                expression2 != null)
            {
                switch (expression1.Kind())
                {
                case SyntaxKind.NullLiteralExpression:
                {
                    result = new NullCheckExpression(
                        binaryExpression,
                        expression2,
                        (kind == SyntaxKind.EqualsExpression) ? NullCheckKind.EqualsToNull : NullCheckKind.NotEqualsToNull);

                    return(true);
                }

                case SyntaxKind.TrueLiteralExpression:
                {
                    if (expression2?.IsKind(SyntaxKind.SimpleMemberAccessExpression) == true &&
                        SemanticUtilities.IsPropertyOfNullableOfT(expression2, "HasValue", semanticModel, cancellationToken))
                    {
                        result = new NullCheckExpression(
                            binaryExpression,
                            ((MemberAccessExpressionSyntax)expression2).Expression,
                            (kind == SyntaxKind.EqualsExpression) ? NullCheckKind.HasValue : NullCheckKind.NotHasValue);

                        return(true);
                    }

                    break;
                }

                case SyntaxKind.FalseLiteralExpression:
                {
                    if (expression2?.IsKind(SyntaxKind.SimpleMemberAccessExpression) == true &&
                        SemanticUtilities.IsPropertyOfNullableOfT(expression2, "HasValue", semanticModel, cancellationToken))
                    {
                        result = new NullCheckExpression(
                            binaryExpression,
                            ((MemberAccessExpressionSyntax)expression2).Expression,
                            (kind == SyntaxKind.EqualsExpression) ? NullCheckKind.NotHasValue : NullCheckKind.HasValue);

                        return(true);
                    }

                    break;
                }
                }
            }

            result = default(NullCheckExpression);
            return(false);
        }