コード例 #1
0
 public bool AreEquivalent(SyntaxTree oldTree, SyntaxTree newTree)
 {
     return(SyntaxFactory.AreEquivalent(oldTree, newTree, topLevel: false));
 }
コード例 #2
0
        internal static bool ParseIfStatement(IfStatementSyntax node, out ExpressionSyntax condition, out ExpressionSyntax target, out AssignmentExpressionSyntax whenTrue, out AssignmentExpressionSyntax whenFalse)
        {
            condition = null;
            target    = null;
            whenTrue  = null;
            whenFalse = null;

            if (node == null || node.Else == null || node.Parent is IfStatementSyntax || node.Else.Statement is IfStatementSyntax)
            {
                return(false);
            }

            condition = node.Condition;
            //make sure to check for multiple statements
            ExpressionStatementSyntax whenTrueExprStatement, whenFalseExprStatement;
            var embeddedBlock = node.Statement as BlockSyntax;

            if (embeddedBlock != null)
            {
                if (embeddedBlock.Statements.Count > 1)
                {
                    return(false);
                }
                var childNodes = embeddedBlock.ChildNodes();
                if (childNodes.Count() > 1)
                {
                    return(false);
                }
                whenTrueExprStatement = childNodes.OfType <ExpressionStatementSyntax>().FirstOrDefault();
            }
            else
            {
                whenTrueExprStatement = node.Statement as ExpressionStatementSyntax;
            }

            var elseBlock = node.Else.Statement as BlockSyntax;

            if (elseBlock != null)
            {
                if (elseBlock.Statements.Count > 1)
                {
                    return(false);
                }
                var childNodes = elseBlock.ChildNodes();
                if (childNodes.Count() > 1)
                {
                    return(false);
                }
                whenFalseExprStatement = childNodes.OfType <ExpressionStatementSyntax>().FirstOrDefault();
            }
            else
            {
                whenFalseExprStatement = node.Else.Statement as ExpressionStatementSyntax;
            }

            if (whenTrueExprStatement == null || whenFalseExprStatement == null)
            {
                return(false);
            }

            whenTrue  = whenTrueExprStatement.Expression as AssignmentExpressionSyntax;
            whenFalse = whenFalseExprStatement.Expression as AssignmentExpressionSyntax;
            if (whenTrue == null || whenFalse == null || whenTrue.Kind() != whenFalse.Kind() ||
                !SyntaxFactory.AreEquivalent(whenTrue.Left, whenFalse.Left))
            {
                return(false);
            }

            return(true);
        }
コード例 #3
0
 private bool Equal(SwitchSectionSyntax left, SwitchSectionSyntax right)
 {
     return(SyntaxFactory.AreEquivalent(left.Labels, right.Labels, null) &&
            SyntaxFactory.AreEquivalent(left.Statements, right.Statements, ignoreChildNode: IgnoreLabeledChild));
 }
 protected override bool AreEquivalent(CaseBlockSyntax section1, CaseBlockSyntax section2) =>
 SyntaxFactory.AreEquivalent(section1.Statements, section2.Statements);
コード例 #5
0
 private static bool IsMidLevelExpression(BinaryExpressionSyntax binaryExpression)
 {
     return(binaryExpression.Parent is BinaryExpressionSyntax binaryParent &&
            SyntaxFactory.AreEquivalent(binaryExpression.OperatorToken, binaryParent.OperatorToken));
 }
 protected override bool AreEquivalent(SwitchSectionSyntax section1, SwitchSectionSyntax section2) =>
 SyntaxFactory.AreEquivalent(section1.Statements, section2.Statements);
コード例 #7
0
 protected override bool Equals(SyntaxNode oldElement, SyntaxNode newElement)
 => SyntaxFactory.AreEquivalent(oldElement, newElement);
コード例 #8
0
 protected override bool AreEquivalent(SyntaxNode node1, SyntaxNode node2) =>
 SyntaxFactory.AreEquivalent(node1, node2);
コード例 #9
0
        public bool TryGetPatternPieces(
            BinaryExpressionSyntax isExpression,
            out IfStatementSyntax ifStatement,
            out LocalDeclarationStatementSyntax localDeclarationStatement,
            out VariableDeclaratorSyntax declarator,
            out CastExpressionSyntax castExpression)
        {
            ifStatement = null;
            localDeclarationStatement = null;
            declarator     = null;
            castExpression = null;

            // The is check has to be in an if check: "if (x is Type)
            if (!isExpression.Parent.IsKind(SyntaxKind.IfStatement, out ifStatement))
            {
                return(false);
            }

            if (!ifStatement.Statement.IsKind(SyntaxKind.Block, out BlockSyntax ifBlock))
            {
                return(false);
            }

            if (ifBlock.Statements.Count == 0)
            {
                return(false);
            }

            var firstStatement = ifBlock.Statements[0];

            if (!firstStatement.IsKind(SyntaxKind.LocalDeclarationStatement, out localDeclarationStatement))
            {
                return(false);
            }

            if (localDeclarationStatement.Declaration.Variables.Count != 1)
            {
                return(false);
            }

            declarator = localDeclarationStatement.Declaration.Variables[0];
            if (declarator.Initializer == null)
            {
                return(false);
            }

            var declaratorValue = declarator.Initializer.Value.WalkDownParentheses();

            if (!declaratorValue.IsKind(SyntaxKind.CastExpression, out castExpression))
            {
                return(false);
            }

            if (!SyntaxFactory.AreEquivalent(isExpression.Left.WalkDownParentheses(), castExpression.Expression.WalkDownParentheses(), topLevel: false) ||
                !SyntaxFactory.AreEquivalent(isExpression.Right.WalkDownParentheses(), castExpression.Type, topLevel: false))
            {
                return(false);
            }

            return(true);
        }
コード例 #10
0
 public static bool AreEquivalent(SyntaxList <SyntaxNode> nodeList1, SyntaxList <SyntaxNode> nodeList2)
 {
     return(Common.EquivalenceChecker.AreEquivalent(nodeList1, nodeList2,
                                                    (n1, n2) => SyntaxFactory.AreEquivalent(n1, n2)));
 }