private bool ValuePatternMatches(TExpressionSyntax expression)
 {
     if (_valuePattern.IsToken)
     {
         return(_syntaxFacts.IsIdentifierName(expression) &&
                _syntaxFacts.AreEquivalent(
                    _valuePattern.AsToken(),
                    _syntaxFacts.GetIdentifierOfSimpleName(expression)));
     }
     else
     {
         return(_syntaxFacts.AreEquivalent(
                    _valuePattern.AsNode(), expression));
     }
 }
Beispiel #2
0
        internal static SyntaxNode GetWhenPartMatch(
            ISyntaxFactsService syntaxFacts, SyntaxNode expressionToMatch, SyntaxNode whenPart)
        {
            var current = whenPart;

            while (true)
            {
                var unwrapped = Unwrap(syntaxFacts, current);
                if (unwrapped == null)
                {
                    return(null);
                }

                if ((current is TMemberAccessExpression) ||
                    (current is TElementAccessExpression))
                {
                    if (syntaxFacts.AreEquivalent(unwrapped, expressionToMatch))
                    {
                        return(unwrapped);
                    }
                }

                current = unwrapped;
            }
        }
        private static bool IsElseIfOrElseClauseEquivalent(
            ISyntaxFactsService syntaxFacts,
            IIfLikeStatementGenerator ifGenerator,
            SyntaxNode elseIfOrElseClause1,
            SyntaxNode elseIfOrElseClause2)
        {
            // Compare Else/ElseIf clauses for equality.

            var isIfStatement = ifGenerator.IsIfOrElseIf(elseIfOrElseClause1);

            if (isIfStatement != ifGenerator.IsIfOrElseIf(elseIfOrElseClause2))
            {
                // If we have one Else and one ElseIf, they're not equal.
                return(false);
            }

            if (isIfStatement)
            {
                // If we have two ElseIf blocks, their conditions have to match.
                var condition1 = ifGenerator.GetCondition(elseIfOrElseClause1);
                var condition2 = ifGenerator.GetCondition(elseIfOrElseClause2);

                if (!syntaxFacts.AreEquivalent(condition1, condition2))
                {
                    return(false);
                }
            }

            var statements1 = WalkDownScopeBlocks(syntaxFacts, syntaxFacts.GetStatementContainerStatements(elseIfOrElseClause1));
            var statements2 = WalkDownScopeBlocks(syntaxFacts, syntaxFacts.GetStatementContainerStatements(elseIfOrElseClause2));

            return(statements1.SequenceEqual(statements2, syntaxFacts.AreEquivalent));
        }
        internal static SyntaxNode?GetWhenPartMatch(
            ISyntaxFactsService syntaxFacts, ISemanticFactsService semanticFacts, SemanticModel semanticModel, SyntaxNode expressionToMatch, SyntaxNode whenPart)
        {
            expressionToMatch = RemoveObjectCastIfAny(syntaxFacts, semanticModel, expressionToMatch);
            var current = whenPart;

            while (true)
            {
                var unwrapped = Unwrap(syntaxFacts, current);
                if (unwrapped == null)
                {
                    return(null);
                }

                if (current is TMemberAccessExpression ||
                    current is TElementAccessExpression)
                {
                    if (syntaxFacts.AreEquivalent(unwrapped, expressionToMatch))
                    {
                        return(unwrapped);
                    }
                }

                current = unwrapped;
            }
        }
Beispiel #5
0
        private static async Task <bool> VerifySyntaxEquivalenceAsync(
            Document oldDocument,
            Document newDocument,
            ISyntaxFactsService syntaxFacts,
            CancellationToken cancellationToken = default)
        {
            if (!string.Equals(
                    (await newDocument.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false)).NormalizeWhitespace("", false).ToFullString(),
                    (await oldDocument.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false)).NormalizeWhitespace("", false).ToFullString(),
                    StringComparison.Ordinal))
            {
                WriteLine($"Syntax roots with normalized white-space are not equivalent '{oldDocument.FilePath}'", ConsoleColors.Magenta);
                return(false);
            }

            if (!syntaxFacts.AreEquivalent(
                    await newDocument.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false),
                    await oldDocument.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false)))
            {
                WriteLine($"Syntax trees are not equivalent '{oldDocument.FilePath}'", ConsoleColors.Magenta);
                return(false);
            }

            return(true);
        }
Beispiel #6
0
        public static bool TryMatchPattern(
            ISyntaxFactsService syntaxFacts,
            IConditionalOperation ifOperation,
            out ISimpleAssignmentOperation trueAssignment,
            out ISimpleAssignmentOperation falseAssignment)
        {
            trueAssignment  = null;
            falseAssignment = null;

            var trueStatement  = ifOperation.WhenTrue;
            var falseStatement = ifOperation.WhenFalse;

            trueStatement  = UseConditionalExpressionHelpers.UnwrapSingleStatementBlock(trueStatement);
            falseStatement = UseConditionalExpressionHelpers.UnwrapSingleStatementBlock(falseStatement);

            if (!TryGetAssignment(trueStatement, out trueAssignment) ||
                !TryGetAssignment(falseStatement, out falseAssignment))
            {
                return(false);
            }

            // The left side of both assignment statements has to be syntactically identical (modulo
            // trivia differences).
            if (!syntaxFacts.AreEquivalent(trueAssignment.Target.Syntax, falseAssignment.Target.Syntax))
            {
                return(false);
            }

            return(UseConditionalExpressionHelpers.CanConvert(
                       syntaxFacts, ifOperation, trueAssignment, falseAssignment));
        }
            protected bool SetInitialOrIsEquivalentToSwitchExpression(TExpressionSyntax expression)
            {
                // If we have not figured the switch expression yet,
                // we will assume that the first expression is the one.
                if (_switchExpression == null)
                {
                    _switchExpression = UnwrapCast(expression);
                    return(true);
                }

                return(_syntaxFacts.AreEquivalent(UnwrapCast(expression), _switchExpression));
            }