public override void VisitMethodDeclaration(MethodDeclarationSyntax node)
        {
            var returnType  = node.ChildNodes().FirstOrDefault(x => !(x is AttributeListSyntax)).ToString();
            var isSubMethod = node.ChildNodes().FirstOrDefault(x => !(x is AttributeListSyntax)).ChildTokens().Any(x => x.Kind() == SyntaxKind.VoidKeyword);
            var isPartial   = node.ChildTokens().Any(x => x.Kind() == SyntaxKind.PartialKeyword);
            var defineName  = node.ChildTokens().FirstOrDefault(x => x.Kind() == SyntaxKind.IdentifierToken).ToString();

            // ジェネリック型を定義している場合
            if (node.ChildNodes().OfType <TypeParameterListSyntax>().Any())
            {
                var listNode     = node.ChildNodes().OfType <TypeParameterListSyntax>().FirstOrDefault();
                var genericTypes = listNode
                                   .ChildNodes()
                                   .OfType <TypeParameterSyntax>()
                                   .Select(x => x.ChildTokens().FirstOrDefault(y => y.Kind() == SyntaxKind.IdentifierToken).ToString());

                defineName = $"{defineName}<{string.Join(", ", genericTypes)}>";
            }

            var methodArguments = new List <MethodArgument>();

            if (node.ChildNodes().OfType <ParameterListSyntax>().FirstOrDefault().ChildNodes().Any())
            {
                var listNode = node.ChildNodes().OfType <ParameterListSyntax>().FirstOrDefault();
                methodArguments = GetMethodArguments(listNode);
            }

            var startLength     = node.Span.Start;
            var endLength       = node.Span.End;
            var parentNamespace = GetNamespace(DefineKinds.Method, startLength, endLength);

            // EventHandler、Method(Sub, Function)
            var isEventHandler = false;

            if (isSubMethod)
            {
                if (methodArguments.Count == 2)
                {
                    if (methodArguments[0].DefineType == "object" && methodArguments[1].DefineType.EndsWith("EventArgs"))
                    {
                        isEventHandler = true;
                    }
                }
            }

            var isWinAPI = node.AttributeLists.Any(x => x.ToString().Contains("DllImport"));
            var kinds    = DefineKinds.Method;

            if (isEventHandler)
            {
                kinds = DefineKinds.EventHandler;
            }

            // EventHandler っぽいけど、Windows API の場合は、こちらを優先する
            if (isWinAPI)
            {
                kinds = DefineKinds.WindowsAPI;
            }

            UserDefinitions.Add(new UserDefinition
            {
                DefineKinds     = kinds,
                IsPartial       = isPartial,
                Namespace       = parentNamespace,
                DefineName      = defineName,
                DefineFullName  = $"{parentNamespace}.{defineName}",
                MethodArguments = methodArguments,
                ReturnType      = RemoveNamespace(returnType),
                SourceFile      = SourceFile,
                StartLength     = startLength,
                EndLength       = endLength,
            });

            base.VisitMethodDeclaration(node);
        }
Example #2
0
 /// <summary>
 /// Obtient la localisation du nom de la méthode.
 /// </summary>
 /// <param name="methNode">Node de la méthode.</param>
 /// <returns>Localisation.</returns>
 public static Location GetMethodLocation(this MethodDeclarationSyntax methNode)
 {
     return(methNode.ChildTokens()
            .First(x => x.Kind() == SyntaxKind.IdentifierToken)
            .GetLocation());
 }
Example #3
0
        private MethodDeclarationSyntax ProcessTestMethod(MethodDeclarationSyntax node)
        {
            // Replace protected keyword with public
            SyntaxToken replaceToPublic(SyntaxToken token)
            {
                if (token.IsKind(SyntaxKind.ProtectedKeyword))
                {
                    token = SyntaxFactory.Token(token.LeadingTrivia, SyntaxKind.PublicKeyword, token.TrailingTrivia);
                }
                return(token);
            }

            var modifiers = node.Modifiers.Select(replaceToPublic);

            node = node.WithModifiers(new SyntaxTokenList(modifiers));

            // Replace method name with Component_Type_Methodname
            var methodName         = node.ChildTokens().First((x) => x.IsKind(SyntaxKind.IdentifierToken));
            var testTypeName       = testType.ToString("G").Substring(0, 1).ToUpperInvariant() + "_";
            var componentName      = GetComponentName(node);
            var shortComponentName = string.IsNullOrEmpty(componentName) ? componentName : componentName.Substring(0, 2).ToUpperInvariant() + "_";
            var newMethodName      = SyntaxFactory.Identifier(methodName.LeadingTrivia, $"{shortComponentName}{testTypeName}{methodName.ValueText}", methodName.TrailingTrivia);

            node = node.ReplaceToken(methodName, newMethodName);

            // Call original method instead of logic
            var isAsyncMethod = node.Modifiers.Any((x) => x.IsKind(SyntaxKind.AsyncKeyword));
            // hack for proper indentation: margin-to-method indentation + 1/2
            var indentTrivia     = node.QueryNodesOrTokensAtPath(SyntaxKind.Block, SyntaxKind.OpenBraceToken).First().GetLeadingTrivia().First();
            var indentTriviaHalf = SyntaxFactory.SyntaxTrivia(SyntaxKind.WhitespaceTrivia, new string(' ', indentTrivia.ToString().Length / 2));
            ExpressionStatementSyntax body;

            if (isAsyncMethod)
            {
                body = SyntaxFactory.ExpressionStatement(
                    SyntaxFactory.AwaitExpression(
                        SyntaxFactory.Token(new SyntaxTriviaList(indentTrivia, indentTriviaHalf), SyntaxKind.AwaitKeyword, TestHelper.Space.AsList()),
                        SyntaxFactory.InvocationExpression(SyntaxFactory.IdentifierName(methodName.ValueText))
                        ),
                    TestHelper.Semicolon
                    );
            }
            else
            {
                body = SyntaxFactory.ExpressionStatement(
                    SyntaxFactory.InvocationExpression(
                        SyntaxFactory.IdentifierName(
                            SyntaxFactory.Identifier(
                                new SyntaxTriviaList(indentTrivia, indentTriviaHalf),
                                methodName.ValueText,
                                TestHelper.Empty.AsList()
                                )
                            )
                        ),
                    TestHelper.Semicolon
                    );
            }

            var bodyList = new SyntaxList <StatementSyntax>(body);

            Debug.Assert(indentTrivia.IsKind(SyntaxKind.WhitespaceTrivia));
            var openBrace  = SyntaxFactory.Token(indentTrivia.AsList(), SyntaxKind.OpenBraceToken, TestHelper.NewLine.AsList());
            var closeBrace = SyntaxFactory.Token(indentTrivia.AsList(), SyntaxKind.CloseBraceToken, TestHelper.NewLine.AsList());

            node = node.WithBody(SyntaxFactory.Block(openBrace, bodyList, closeBrace));

            // Export TestType arguments and decorate the method with them individually
            var arguments = GetTestTypeArguments(node);

            MethodDeclarationSyntax createNewAttribute(string attributeName)
            {
                return(node.AddAttributeLists(
                           SyntaxFactory.AttributeList(
                               SyntaxFactory.SeparatedList(new[] {
                    SyntaxFactory.Attribute(
                        SyntaxFactory.IdentifierName(attributeName)
                        )
                }
                                                           )
                               )
                           .WithTrailingTrivia(node.AttributeLists.First().GetTrailingTrivia())
                           .WithLeadingTrivia(indentTrivia)
                           ));
            }

            if (arguments.SmokeTest)
            {
                node = createNewAttribute("SmokeTest");
            }
            if (arguments.Devel)
            {
                node = createNewAttribute("DevelTest");
            }
            if (arguments.Production)
            {
                node = createNewAttribute("ProductionTest");
            }

            if (!string.IsNullOrEmpty(componentName))
            {
                node = createNewAttribute(componentName + "Component");
            }

            // Remove unused attributes
            var removableAttrLists = node.AttributeLists.Where((attribList) => attribList.Attributes.Any((attrib) =>
            {
                var testTypes     = new[] { "TestComponent", "InMemoryTest", "LocalTest", "RemoteTest" }.ToList();
                var attributeName = attrib.Name.GetText().ToString();
                return(testTypes.Contains(attributeName));
            }));

            node = node.RemoveNodes(removableAttrLists, SyntaxRemoveOptions.KeepNoTrivia);

            return(node);
        }
Example #4
0
 private static SyntaxToken FindFirstIdentifierInDeclaration(MethodDeclarationSyntax methodDeclaration)
 {
     return(methodDeclaration.ChildTokens().First(t => t.CSharpKind() == SyntaxKind.IdentifierToken));
 }
Example #5
0
        public static DocumentedElement LoadFromSyntaxNode(MethodDeclarationSyntax node)
        {
            SyntaxToken syntaxToken = node.ChildTokens().First(token => token.IsKind(SyntaxKind.IdentifierToken));

            return(DocumentedElement.LoadFromSyntaxTokenAndTrivia(syntaxToken, node.GetLeadingTrivia()));
        }