Helper for accessing class in AST
Inheritance: Helper
Beispiel #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MethodASTWalker"/> class.
        /// </summary>
        protected MethodASTWalker(CSharpSyntaxNode node)
        {
            var methodDeclarationSyntaxNode = node as MethodDeclarationSyntax;
            if (methodDeclarationSyntaxNode == null)
            {
                throw new ArgumentException(
                    string.Format("Specified node is not of type {0}",
                    typeof(MethodDeclarationSyntax).Name));
            }

            this.node = node;
            MethodDeclaration methodHelper = new MethodDeclaration(methodDeclarationSyntaxNode);

            this.methodDeclaration = MethodDeclarationTranslationUnit.Create(
                methodHelper.Visibility,
                IdentifierTranslationUnit.Create(methodHelper.ReturnType),
                IdentifierTranslationUnit.Create(methodHelper.Name));

            foreach (TypedIdentifier parameter in methodHelper.Parameters)
            {
                this.methodDeclaration.AddArgument(ArgumentDefinitionTranslationUnit.Create(
                    IdentifierTranslationUnit.Create(parameter.TypeName),
                    IdentifierTranslationUnit.Create(parameter.IdentifierName)));
            }
        }
        public void DecoratingClass()
        {
            SyntaxTree tree = CSharpSyntaxTree.ParseText(@"
            using System;
            namespace Namespace1
            {
                [Obsolete]
                public class Class1 { }
                public class Class2 {
                    public Class1 Method1() { return null; }
                }
            }"
            );

            // Second class
            var node = new NodeLocator(tree).LocateLast(typeof(MethodDeclarationSyntax));
            Assert.IsNotNull(node, string.Format("Node of type `{0}` should be found!",
                typeof(MethodDeclarationSyntax).Name));

            // Loading MSCoreLib
            var compilation = CSharpCompilation.Create("TestAssembly")
                  .AddReferences(
                     MetadataReference.CreateFromFile(
                       typeof(object).Assembly.Location))
                  .AddSyntaxTrees(tree);
            var semanticModel = compilation.GetSemanticModel(tree);

            var methodDeclarationNode = node as MethodDeclarationSyntax;
            var helper = new MethodDeclaration(methodDeclarationNode, semanticModel).ReturnType;

            var attributes = helper.Attributes;
            Assert.IsNotNull(attributes);
            Assert.AreNotEqual(0, attributes.Count(), "Expecting attributes!");

            var attribute = attributes.First();
            Assert.AreEqual("ObsoleteAttribute", attribute.AttributeClassName, "Wrong name!");
            Assert.AreEqual("System.ObsoleteAttribute", attribute.AttributeClassFullName, "Wrong full name!");
            Assert.IsNotNull(attribute.ConstructorArguments);
            Assert.AreEqual(0, attribute.ConstructorArguments.Count(), "Expected no constructor arguments");
        }
        public void MethodParametersAreOfCorrectType()
        {
            var source = @"
                class Class1 {
                    public void Method1(int param1) { }
                    public void Method2(int param1, string param2) { }
                    public void Method3(int param1, string param2, object param3) { }
                    public void Method4(int param1, string param2, object param3, bool param4) { }
                }
            ";

            var syntaxTree = CSharpSyntaxTree.ParseText(source);
            var semanticModel = CSharpCompilation.Create("Class").AddReferences(
                MetadataReference.CreateFromFile(typeof(object).Assembly.Location)).AddSyntaxTrees(
                syntaxTree).GetSemanticModel(syntaxTree);

            IEnumerable<SyntaxNode> nodes = new NodeLocator(syntaxTree).LocateAll(
                typeof(MethodDeclarationSyntax));

            foreach (SyntaxNode node in nodes)
            {
                Assert.IsNotNull(node, string.Format("Node of type `{0}` should be found!",
                    typeof(MethodDeclarationSyntax).Name));
                MethodDeclarationSyntax methodDeclarationNode = node as MethodDeclarationSyntax;

                MethodDeclaration methodDeclaration = new MethodDeclaration(methodDeclarationNode);

                foreach (var param in methodDeclaration.Parameters)
                {
                    Assert.IsNotNull(param, "Parameter should not be null!");
                    Assert.IsInstanceOfType(param, typeof(Parameter), "Wrong parameter type!");
                }
            }
        }
        // When specifying `null` as expected return type, we intend `void`.
        private static void TestRetrieveMethodReturnType(MethodDeclarationSyntax methodDeclarationNode, string expected = null)
        {
            Assert.IsNotNull(methodDeclarationNode, "Found node should be of type `{0}`!",
                typeof(MethodDeclarationSyntax).Name);

            MethodDeclaration methodDeclaration = new MethodDeclaration(methodDeclarationNode);
            string typeName = methodDeclaration.ReturnType.Name;

            Assert.IsNotNull(typeName, "Method name should not be null!");
            Assert.AreNotEqual(string.Empty, typeName, "Method name should not be empty!");
            Assert.AreEqual(expected ?? "void", typeName, "Method name is not the one in source!");
        }
        public void MethodWithParameters()
        {
            var source = @"
                class Class1 {
                    public void Method1(int param1) { }
                    public void Method2(int param1, string param2) { }
                    public void Method3(int param1, string param2, object param3) { }
                    public void Method4(int param1, string param2, object param3, bool param4) { }
                }
            ";

            var syntaxTree = CSharpSyntaxTree.ParseText(source);
            var semanticModel = CSharpCompilation.Create("Class").AddReferences(
                MetadataReference.CreateFromFile(typeof(object).Assembly.Location)).AddSyntaxTrees(
                syntaxTree).GetSemanticModel(syntaxTree);

            IEnumerable<SyntaxNode> nodes = new NodeLocator(syntaxTree).LocateAll(
                typeof(MethodDeclarationSyntax));

            foreach (SyntaxNode node in nodes)
            {
                Assert.IsNotNull(node, string.Format("Node of type `{0}` should be found!",
                    typeof(MethodDeclarationSyntax).Name));
                MethodDeclarationSyntax methodDeclarationNode = node as MethodDeclarationSyntax;

                MethodDeclaration methodDeclaration = new MethodDeclaration(methodDeclarationNode);
                Assert.IsNotNull(methodDeclaration.Parameters, "Expecting list of parameters not to be null!");
                Assert.IsTrue(methodDeclaration.Parameters.Count() > 0, "Expecting parameters!");
            }

            // 1 parameter
            MethodDeclaration method1Declaration = new MethodDeclaration(nodes.ElementAt(0) as MethodDeclarationSyntax);
            Assert.AreEqual(1, method1Declaration.Parameters.Count(), "Expecting different number of paramters!");

            // 2 parameters
            MethodDeclaration method2Declaration = new MethodDeclaration(nodes.ElementAt(1) as MethodDeclarationSyntax);
            Assert.AreEqual(2, method2Declaration.Parameters.Count(), "Expecting different number of paramters!");

            // 3 parameters
            MethodDeclaration method3Declaration = new MethodDeclaration(nodes.ElementAt(2) as MethodDeclarationSyntax);
            Assert.AreEqual(3, method3Declaration.Parameters.Count(), "Expecting different number of paramters!");

            // 4 parameters
            MethodDeclaration method4Declaration = new MethodDeclaration(nodes.ElementAt(3) as MethodDeclarationSyntax);
            Assert.AreEqual(4, method4Declaration.Parameters.Count(), "Expecting different number of paramters!");
        }
        public void MethodWithNoParameters()
        {
            var source = @"
                class Class1 {
                    public void Method1() { }
                }
            ";

            var syntaxTree = CSharpSyntaxTree.ParseText(source);
            var semanticModel = CSharpCompilation.Create("Class").AddReferences(
                MetadataReference.CreateFromFile(typeof(object).Assembly.Location)).AddSyntaxTrees(
                syntaxTree).GetSemanticModel(syntaxTree);

            SyntaxNode node = new NodeLocator(syntaxTree).LocateFirst(typeof(MethodDeclarationSyntax));
            Assert.IsNotNull(node, string.Format("Node of type `{0}` should be found!",
                typeof(MethodDeclarationSyntax).Name));

            MethodDeclarationSyntax methodDeclarationNode = node as MethodDeclarationSyntax;
            MethodDeclaration methodDeclaration = new MethodDeclaration(methodDeclarationNode);

            Assert.IsNotNull(methodDeclaration.Parameters, "Expecting list of parameters not to be null!");
            Assert.AreEqual(0, methodDeclaration.Parameters.Count(), "Expecting no parameters!");
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="node"></param>
        /// <remarks>
        /// We don't need to go further deeper in the tree by visiting the node.
        /// </remarks>
        public override void VisitMethodDeclaration(MethodDeclarationSyntax node)
        {
            // TODO: Use translation unit factories

            var helper = new MethodDeclaration(node, this.semanticModel);

            // Properties in TypeScript will be translated as methods as
            // TypeScript does not support properties in interfaces
            var translationUnit = MethodSignatureDeclarationTranslationUnit.Create(
                helper.Visibility, TypeIdentifierTranslationUnit.Create(helper.ReturnType.Name), IdentifierTranslationUnit.Create(helper.Name));

            this.interfaceDeclaration.AddSignature(translationUnit);

            this.InvokePropertyDeclarationVisited(this, new WalkerEventArgs());
        }
        public void DetectVoidType()
        {
            var tree = CSharpSyntaxTree.ParseText(@"
            using System;
            public class MyClass {
                public void Method1() { }
                public int Method2() { }
            }
            ");

            // First method
            var node = new NodeLocator(tree).LocateFirst(typeof(MethodDeclarationSyntax));
            Assert.IsNotNull(node, string.Format("Node of type `{0}` should be found!",
                typeof(MethodDeclarationSyntax).Name));

            var methodDeclarationNode = node as MethodDeclarationSyntax;
            var helper = new MethodDeclaration(methodDeclarationNode).ReturnType;

            Assert.IsTrue(helper.IsVoid, "Expected void type!");

            // Second method
            node = new NodeLocator(tree).LocateLast(typeof(MethodDeclarationSyntax));
            Assert.IsNotNull(node, string.Format("Node of type `{0}` should be found!",
                typeof(MethodDeclarationSyntax).Name));

            methodDeclarationNode = node as MethodDeclarationSyntax;
            helper = new MethodDeclaration(methodDeclarationNode).ReturnType;

            Assert.IsFalse(helper.IsVoid, "Void type not expected!");
        }
        public void RetrieveAttributesOnClassFromMethodViaSemantics()
        {
            SyntaxTree tree = CSharpSyntaxTree.ParseText(@"
            using System;
            namespace Namespace1
            {
                [Obsolete]
                public class Class1 { }
                public class Class2 {
                    public Class1 Method1() { return null; }
                }
            }"
            );

            // Second class
            var node = new NodeLocator(tree).LocateLast(typeof(MethodDeclarationSyntax));
            Assert.IsNotNull(node, string.Format("Node of type `{0}` should be found!",
                typeof(MethodDeclarationSyntax).Name));

            // Loading MSCoreLib
            var compilation = CSharpCompilation.Create("TestAssembly")
                  .AddReferences(
                     MetadataReference.CreateFromFile(
                       typeof(object).Assembly.Location))
                  .AddSyntaxTrees(tree);
            var semanticModel = compilation.GetSemanticModel(tree);

            var methodDeclarationNode = node as MethodDeclarationSyntax;
            var helper = new MethodDeclaration(methodDeclarationNode, semanticModel).ReturnType;

            var attributes = helper.Attributes;
            Assert.IsNotNull(attributes);
            Assert.AreEqual(1, attributes.Count(), "Expecting 1 attribute!");
            Assert.IsTrue(attributes.First().AttributeClassName.Contains("ObsoleteAttribute"), "Attribute `Obsolete` expected!");
        }
Beispiel #10
0
        public void DetectVoidTypeWithSemanticModel()
        {
            var tree = CSharpSyntaxTree.ParseText(@"
            using System;
            public class MyClass {
                public void Method1() { }
                public int Method2() { }
            }
            ");

            // First method
            var node = new NodeLocator(tree).LocateFirst(typeof(MethodDeclarationSyntax));
            Assert.IsNotNull(node, string.Format("Node of type `{0}` should be found!",
                typeof(MethodDeclarationSyntax).Name));

            // Loading MSCoreLib
            var compilation = CSharpCompilation.Create("TestAssembly")
                  .AddReferences(
                     MetadataReference.CreateFromFile(
                       typeof(object).Assembly.Location))
                  .AddSyntaxTrees(tree);
            var semanticModel = compilation.GetSemanticModel(tree);

            var methodDeclarationNode = node as MethodDeclarationSyntax;
            var helper = new MethodDeclaration(methodDeclarationNode, semanticModel).ReturnType;

            Assert.IsTrue(helper.IsVoid, "Expected void type!");

            // Second method
            node = new NodeLocator(tree).LocateLast(typeof(MethodDeclarationSyntax));
            Assert.IsNotNull(node, string.Format("Node of type `{0}` should be found!",
                typeof(MethodDeclarationSyntax).Name));

            methodDeclarationNode = node as MethodDeclarationSyntax;
            helper = new MethodDeclaration(methodDeclarationNode, semanticModel).ReturnType;

            Assert.IsFalse(helper.IsVoid, "Void type not expected!");
        }