private static SyntaxNode?Unwrap(ISyntaxFacts syntaxFacts, SyntaxNode node)
        {
            node = syntaxFacts.WalkDownParentheses(node);

            if (node is TInvocationExpression invocation)
            {
                return(syntaxFacts.GetExpressionOfInvocationExpression(invocation));
            }

            if (node is TMemberAccessExpression memberAccess)
            {
                return(syntaxFacts.GetExpressionOfMemberAccessExpression(memberAccess));
            }

            if (node is TConditionalAccessExpression conditionalAccess)
            {
                return(syntaxFacts.GetExpressionOfConditionalAccessExpression(conditionalAccess));
            }

            if (node is TElementAccessExpression elementAccess)
            {
                return(syntaxFacts.GetExpressionOfElementAccessExpression(elementAccess));
            }

            return(null);
        }
        private static TExpressionSyntax?Unwrap(ISyntaxFacts syntaxFacts, TExpressionSyntax node)
        {
            node = (TExpressionSyntax)syntaxFacts.WalkDownParentheses(node);

            if (node is TInvocationExpressionSyntax invocation)
            {
                return((TExpressionSyntax)syntaxFacts.GetExpressionOfInvocationExpression(invocation));
            }

            if (syntaxFacts.IsSimpleMemberAccessExpression(node))
            {
                return((TExpressionSyntax?)syntaxFacts.GetExpressionOfMemberAccessExpression(node));
            }

            if (node is TConditionalAccessExpressionSyntax conditionalAccess)
            {
                return((TExpressionSyntax)syntaxFacts.GetExpressionOfConditionalAccessExpression(conditionalAccess));
            }

            if (node is TElementAccessExpressionSyntax elementAccess)
            {
                return((TExpressionSyntax?)syntaxFacts.GetExpressionOfElementAccessExpression(elementAccess));
            }

            return(null);
        }
        private static bool TryAnalyzeInvocationCondition(
            SyntaxNodeAnalysisContext context,
            ISyntaxFacts syntaxFacts,
            IMethodSymbol?referenceEqualsMethodOpt,
            TInvocationExpression invocation,
            [NotNullWhen(true)] out SyntaxNode?conditionPartToCheck,
            out bool isEquals)
        {
            conditionPartToCheck = null;
            isEquals             = true;

            var expression = syntaxFacts.GetExpressionOfInvocationExpression(invocation);
            var nameNode   = syntaxFacts.IsIdentifierName(expression)
                ? expression
                : syntaxFacts.IsSimpleMemberAccessExpression(expression)
                    ? syntaxFacts.GetNameOfMemberAccessExpression(expression)
                    : null;

            if (!syntaxFacts.IsIdentifierName(nameNode))
            {
                return(false);
            }

            syntaxFacts.GetNameAndArityOfSimpleName(nameNode, out var name, out _);
            if (!syntaxFacts.StringComparer.Equals(name, nameof(ReferenceEquals)))
            {
                return(false);
            }

            var arguments = syntaxFacts.GetArgumentsOfInvocationExpression(invocation);

            if (arguments.Count != 2)
            {
                return(false);
            }

            var conditionLeft  = syntaxFacts.GetExpressionOfArgument(arguments[0]);
            var conditionRight = syntaxFacts.GetExpressionOfArgument(arguments[1]);

            if (conditionLeft == null || conditionRight == null)
            {
                return(false);
            }

            conditionPartToCheck = GetConditionPartToCheck(syntaxFacts, conditionLeft, conditionRight);
            if (conditionPartToCheck == null)
            {
                return(false);
            }

            var semanticModel     = context.SemanticModel;
            var cancellationToken = context.CancellationToken;
            var symbol            = semanticModel.GetSymbolInfo(invocation, cancellationToken).Symbol;

            return(referenceEqualsMethodOpt != null && referenceEqualsMethodOpt.Equals(symbol));
        }
Ejemplo n.º 4
0
        public static bool IsPossiblyDateAndTimeArgumentToken(
            SyntaxToken token, ISyntaxFacts syntaxFacts,
            [NotNullWhen(true)] out SyntaxNode?argumentNode,
            [NotNullWhen(true)] out SyntaxNode?invocationExpression)
        {
            // Has to be a string literal passed to a method.
            argumentNode         = null;
            invocationExpression = null;

            if (!syntaxFacts.IsStringLiteral(token))
            {
                return(false);
            }

            if (!IsMethodArgument(token, syntaxFacts))
            {
                return(false);
            }

            if (!syntaxFacts.IsLiteralExpression(token.Parent))
            {
                return(false);
            }

            if (!syntaxFacts.IsArgument(token.Parent.Parent))
            {
                return(false);
            }

            var argumentList         = token.Parent.Parent.Parent;
            var invocationOrCreation = argumentList?.Parent;

            if (!syntaxFacts.IsInvocationExpression(invocationOrCreation))
            {
                return(false);
            }

            var invokedExpression = syntaxFacts.GetExpressionOfInvocationExpression(invocationOrCreation);
            var name = GetNameOfInvokedExpression(syntaxFacts, invokedExpression);

            if (name != nameof(ToString) && name != nameof(System.DateTime.ParseExact) && name != nameof(System.DateTime.TryParseExact))
            {
                return(false);
            }

            // We have a string literal passed to a method called ToString/ParseExact/TryParseExact.
            // Have to do a more expensive semantic check now.
            argumentNode         = token.Parent.Parent;
            invocationExpression = invocationOrCreation;

            return(true);
        }