private MemberDeclarationSyntax CreateProperty(VariableDeclarationSyntax node)
            {
                var type         = semanticModel.GetTypeInfo(node.DescendantNodes().OfType <IdentifierNameSyntax>().FirstOrDefault());
                var propertyName = node.DescendantNodes().OfType <IdentifierNameSyntax>().LastOrDefault().Identifier.ValueText;

                return(CodeElementFactory.CreateProperty(CodeElementFactory.CreateType(type), propertyName));
            }
            private MemberDeclarationSyntax CreateMethod(VariableDeclarationSyntax node)
            {
                var invocationExpression = node.DescendantNodes().OfType <InvocationExpressionSyntax>().FirstOrDefault();
                var methodName           = invocationExpression.DescendantNodes().OfType <MemberAccessExpressionSyntax>().FirstOrDefault().GetLastToken().ValueText;
                var parameters           = GetParametersTypesByArguments(invocationExpression.ArgumentList);
                var type = semanticModel.GetTypeInfo(node.DescendantNodes().OfType <IdentifierNameSyntax>().FirstOrDefault());

                return(CodeElementFactory.CreareMethodDeclaration(CodeElementFactory.CreateType(type), methodName, parameters));
            }
コード例 #3
0
 public override IStmt VisitVariableDeclaration(VariableDeclarationSyntax node)
 {
     return(new CompoundLocalDeclStmt
     {
         Declarations = node.DescendantNodes().OfType <VariableDeclaratorSyntax>().Select(Visit).ToArray()
     });
 }
コード例 #4
0
        public void TestAnalyzeVarDeclarator()
        {
            string     code1 = @"class TestClass { public void TestMethod(){int test = 0; test = 1;}}";
            SyntaxTree tree1 = CSharpSyntaxTree.ParseText(code1);

            VariableDeclaratorSyntax  varDecNode        = tree1.GetRootAsync().Result.DescendantNodes().OfType <VariableDeclaratorSyntax>().First();
            VariableDeclarationSyntax varDeclartionNode = tree1.GetRootAsync().Result.DescendantNodes().OfType <VariableDeclarationSyntax>().First();

            List <SyntaxTree> trees1 = new List <SyntaxTree> {
                tree1
            };
            Compilation comp1 = CSharpCompilation.Create("TestCompilation1", trees1);

            ScriptAnalyzer analyzer = this.createAnalyzer(comp1);

            TaggedSyntaxLibrary lib = analyzer.AnalyzeNode(varDecNode);

            Assert.IsNotNull(lib, "Library defined");
            Assert.AreEqual(1, lib.TaggedSyntaxTrees.Count(), "Has one tree");

            TaggedSyntaxTree tree = lib.TaggedSyntaxTrees.First();

            CollectionAssert.Contains(tree.TaggedNodes, varDeclartionNode, "Tree contains variable declaration node");

            IEnumerable <SyntaxNode> nodes = varDeclartionNode.DescendantNodes();

            foreach (SyntaxNode node in nodes)
            {
                CollectionAssert.Contains(tree.TaggedNodes, node, "SubNode is added to tree: " + node.ToString());
            }
        }
            private MemberDeclarationSyntax CreateConstructor(VariableDeclarationSyntax node)
            {
                var objectCreationExpression = node.DescendantNodes().OfType <ObjectCreationExpressionSyntax>().FirstOrDefault();
                var className = objectCreationExpression.DescendantNodes().OfType <IdentifierNameSyntax>().FirstOrDefault().Identifier.ValueText;

                return(CodeElementFactory.CreateConstructor(className, GetParametersTypesByArguments(objectCreationExpression.ArgumentList)));
            }
コード例 #6
0
 public static string GetVariableType(VariableDeclarationSyntax variable)
 {
     return(variable
            .DescendantNodes()
            .First(node => node is PredefinedTypeSyntax || node is IdentifierNameSyntax)
            .GetFirstToken()
            .ValueText);
 }
コード例 #7
0
        public static IEnumerable <EffectProperty> Parse(VariableDeclarationSyntax syntax)
        {
            var type = syntax.DescendantNodes().OfType <TypeSyntax>().First().DescendantNodes().OfType <SyntaxToken>().First().ValueText;

            foreach (var variable in syntax.Variables)
            {
                var isArrayType = variable.ArrayRankSpecifiers.Any();
                var name        = variable.Identifier.ValueText;
                var register    = variable.Qualifiers.OfType <RegisterLocation>().Select(r => r.Register.ValueText).FirstOrDefault();

                yield return(new EffectProperty(type, name, isArrayType, register));
            }
        }
            private Boolean IsObjectCreation(VariableDeclarationSyntax node)
            {
                var variableDeclarator = node.DescendantNodes().OfType <VariableDeclaratorSyntax>().FirstOrDefault();

                if (variableDeclarator == null)
                {
                    return(false);
                }
                if (variableDeclarator.GetFirstToken().ValueText != localVaribleName)
                {
                    return(false);
                }
                return(variableDeclarator.DescendantNodes().Any(n => n is ObjectCreationExpressionSyntax));
            }
            private Boolean IsSimpleMemberAccessExpression(VariableDeclarationSyntax node)
            {
                var memberAccessExpression = node.DescendantNodes().OfType <MemberAccessExpressionSyntax>().FirstOrDefault();

                if (memberAccessExpression.DescendantNodes().Any(n => !(n is IdentifierNameSyntax)))
                {
                    return(false);
                }
                var left = memberAccessExpression.DescendantNodes().OfType <IdentifierNameSyntax>().FirstOrDefault();

                if (left == null || left.Identifier.ValueText != localVaribleName)
                {
                    return(false);
                }
                return(true);
            }
            private Boolean IsInvocationExpression(VariableDeclarationSyntax node)
            {
                var invocationExpression = node.DescendantNodes().OfType <InvocationExpressionSyntax>().FirstOrDefault();

                if (invocationExpression == null)
                {
                    return(false);
                }
                var identifier = invocationExpression.DescendantNodes().OfType <IdentifierNameSyntax>().FirstOrDefault();

                if (identifier == null || identifier.Identifier.ValueText != localVaribleName)
                {
                    return(false);
                }
                return(true);
            }
コード例 #11
0
        private static async Task<Document> FixVarType(Document document, SyntaxNode invocationExpressionSyntax, CancellationToken cancellationToken)
        {
            var documentEditor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false);
            VariableDeclarationSyntax variableDeclarator = invocationExpressionSyntax.Ancestors().OfType<VariableDeclarationSyntax>().FirstOrDefault();
            if (variableDeclarator is not null)
            {
                var varIdentifier = variableDeclarator.DescendantNodes().OfType<IdentifierNameSyntax>().Where(id => id.IsVar).FirstOrDefault();
                if (varIdentifier is not null)
                {
                    var node = SyntaxFactory.QualifiedName(SyntaxFactory.ParseName(AppWindowNamespace), (SimpleNameSyntax)SyntaxFactory.ParseName(AppWindowTypeName));
                    documentEditor.ReplaceNode(varIdentifier, node);
                    return document.WithSyntaxRoot(documentEditor.GetChangedRoot());
                }
            }

            return document;
        }
コード例 #12
0
        public void TestGetSyntaxNodesFromIdentifierVar()
        {
            string code1 = "public class TestCLass{ int prop; public void Main(){ prop = 1;}}";

            SyntaxTree tree1 = CSharpSyntaxTree.ParseText(code1);
            VariableDeclarationSyntax node1  = tree1.GetRootAsync().Result.DescendantNodesAndSelf().OfType <VariableDeclarationSyntax>().First();
            MethodDeclarationSyntax   node2  = tree1.GetRootAsync().Result.DescendantNodesAndSelf().OfType <MethodDeclarationSyntax>().Last();
            IdentifierNameSyntax      node3  = node2.DescendantNodes().OfType <IdentifierNameSyntax>().First();
            VariableDeclaratorSyntax  eNode  = node1.DescendantNodes().OfType <VariableDeclaratorSyntax>().First();
            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(node3);

            Assert.IsNotNull(result, "Returns a list");
            Assert.AreEqual(1, result.Count(), "List has one node");
            CollectionAssert.Contains(result, eNode, "Node is expected node");
        }