public void TestGetSyntaxNodesFromIdentifierObjectCreation() { string code1 = "public class TestCLass{public void Main(){ClassA test = new ClassA(0);}} public class ClassA{ public ClassA(){} public ClassA(int param){}}"; SyntaxTree tree1 = CSharpSyntaxTree.ParseText(code1); ObjectCreationExpressionSyntax node1 = tree1.GetRootAsync().Result.DescendantNodesAndSelf().OfType <ObjectCreationExpressionSyntax>().First(); ClassDeclarationSyntax node2 = tree1.GetRootAsync().Result.DescendantNodesAndSelf().OfType <ClassDeclarationSyntax>().Last(); ConstructorDeclarationSyntax node3 = node2.DescendantNodes().OfType <ConstructorDeclarationSyntax>().First(); ConstructorDeclarationSyntax node4 = node2.DescendantNodes().OfType <ConstructorDeclarationSyntax>().Last(); IdentifierNameSyntax node5 = node1.DescendantNodes().OfType <IdentifierNameSyntax>().Last(); List <SyntaxTree> trees1 = new List <SyntaxTree> { tree1 }; Compilation comp1 = CSharpCompilation.Create("TestCompilation1", trees1); ScriptAnalyzerSymbolHelper sHelper = new ScriptAnalyzerSymbolHelper(new Compilation[] { comp1 }); ScriptAnalyzerResourceHelper rHelper = new ScriptAnalyzerResourceHelper(sHelper); List <SyntaxNode> result = rHelper.GetSyntaxNodes(node5); Assert.IsNotNull(result, "Returns a list"); Assert.AreEqual(1, result.Count(), "List has one node"); CollectionAssert.Contains(result, node2, "Node is class"); }
private VariableState VisitObjectCreation(ObjectCreationExpressionSyntax node, ExecutionState state) { VariableState finalState = VisitInvocationAndCreation(node, node.ArgumentList, state); foreach (SyntaxNode child in node.DescendantNodes()) { if (child is NamedFieldInitializerSyntax) { finalState = finalState.merge(VisitNamedFieldInitializer((NamedFieldInitializerSyntax)child, state)); } else { SGLogging.Log(child.GetText().ToString().Trim() + " -> " + finalState); } } return(finalState); }
private VariableState VisitObjectCreation(ObjectCreationExpressionSyntax node, ExecutionState state) { VariableState finalState = VisitInvocationAndCreation(node, node.ArgumentList, state); foreach (SyntaxNode child in node.DescendantNodes()) { if (child is AssignmentExpressionSyntax assignmentExpressionSyntax) { finalState = finalState.Merge(VisitAssignment(assignmentExpressionSyntax, state)); } else { Logger.Log(child.GetText().ToString().Trim() + " -> " + finalState); } } return(finalState); }