public static MethodDeclarationSyntax ConvertToExpressionBodiedHelper(MethodDeclarationSyntax methodDeclaration)
        {
            var expression = AnalyzeMethods(methodDeclaration);

            if (expression == null)
            {
                return(methodDeclaration);
            }

            var closeParen = methodDeclaration.DescendantTokens()
                             .FirstOrDefault(x => x.IsKind(SyntaxKind.CloseParenToken));

            if (closeParen != null)
            {
                methodDeclaration =
                    methodDeclaration.ReplaceToken(closeParen, closeParen.WithTrailingTrivia(_spaceTrivia));
            }

            var newMethod =
                methodDeclaration
                .WithLeadingTrivia(methodDeclaration.GetLeadingTrivia())
                .WithExpressionBody(
                    SyntaxFactory.ArrowExpressionClause(expression.WithLeadingTrivia(_spaceTrivia)))
                .WithBody(null)
                .WithSemicolonToken(GetSemicolon(methodDeclaration.Body))
                .WithAdditionalAnnotations(Formatter.Annotation);


            return(newMethod);
        }
        public override SyntaxNode VisitMethodDeclaration(MethodDeclarationSyntax node)
        {
            var visibilityTokens = node.DescendantTokens(_ => true)
                                   .Where(_ => _.IsKind(SyntaxKind.PublicKeyword) ||
                                          _.IsKind(SyntaxKind.PrivateKeyword) ||
                                          _.IsKind(SyntaxKind.ProtectedKeyword) ||
                                          _.IsKind(SyntaxKind.InternalKeyword)).ToImmutableList();

            if (!visibilityTokens.Any(_ => _.IsKind(SyntaxKind.PublicKeyword)))
            {
                var tokenPosition = 0;

                var newMethod = node.ReplaceTokens(visibilityTokens,
                                                   (_, __) =>
                {
                    tokenPosition++;

                    return(tokenPosition == 1 ?
                           SyntaxFactory.Token(
                               _.LeadingTrivia,
                               SyntaxKind.PublicKeyword,
                               _.TrailingTrivia) :
                           new SyntaxToken());
                });
                return(newMethod);
            }
            else
            {
                return(node);
            }
        }
Пример #3
0
        private void WriteMethodData(MethodDeclarationSyntax method, string summary, string summary_cleaned, JsonWriter jWriter, string repoPath)
        {
            jWriter.WriteStartObject();

            jWriter.WritePropertyName("code");
            jWriter.WriteValue(method.ToString());

            jWriter.WritePropertyName("code_tokens");
            jWriter.WriteStartArray();
            foreach (var token in method.DescendantTokens())
            {
                jWriter.WriteValue(token.ValueText);
            }
            jWriter.WriteEndArray();

            jWriter.WritePropertyName("docstring");
            jWriter.WriteValue(summary);

            jWriter.WritePropertyName("docstring_tokens");
            jWriter.WriteStartArray();
            foreach (var token in summary_cleaned
                     .Split(new[] { ' ', '\n', '.', '(', ')', ';', '\'', '"', '@' },
                            StringSplitOptions.RemoveEmptyEntries)
                     .Select(t => t.Trim()).Where(t => t.Length > 0))
            {
                jWriter.WriteValue(token);
            }
            jWriter.WriteEndArray();

            jWriter.WritePropertyName("comment_tokens");
            jWriter.WriteStartArray();
            foreach (var token in method.DescendantTrivia()
                     .Where(t => t.IsKind(SyntaxKind.SingleLineCommentTrivia) || t.IsKind(SyntaxKind.MultiLineCommentTrivia))
                     .SelectMany(t => t.ToString()
                                 .Split(new[] { ' ', '\n', '.', '(', ')', ';', '\'', '"', '@', '/' },
                                        StringSplitOptions.RemoveEmptyEntries))
                     .Select(t => t.Trim()).Where(t => t.Length > 0))
            {
                jWriter.WriteValue(token);
            }
            jWriter.WriteEndArray();

            jWriter.WritePropertyName("func_name");
            jWriter.WriteValue(method.Identifier.ToString());

            jWriter.WritePropertyName("language");
            jWriter.WriteValue("csharp");

            jWriter.WritePropertyName("lineno");
            // Roslyn does 0-based line counting.
            jWriter.WriteValue(method.SyntaxTree.GetLineSpan(method.Span).StartLinePosition.Line + 1);

            jWriter.WritePropertyName("path");
            jWriter.WriteValue(method.SyntaxTree.FilePath);

            jWriter.WritePropertyName("repo");
            jWriter.WriteValue(repoPath);

            jWriter.WriteEndObject();
        }
        private void FindEmptySyntaxToken(MethodDeclarationSyntax node)
        {
            int i = 0;

            while (node.DescendantTokens().Any(x => x.IsKind(SyntaxKind.IdentifierToken) && x.ValueText == _methodContext.Current.UnusedLambdaToken.ValueText))
            {
                _methodContext.Current.UnusedLambdaToken = SyntaxFactory.Identifier($"a{++i}");
            }
        }
        private static MethodDeclarationSyntax RenameRule(string ruleName, MethodDeclarationSyntax underwritingRule)
        {
            var identifierToken = underwritingRule.DescendantTokens()
                                  .First(t => t.IsKind(SyntaxKind.IdentifierToken) &&
                                         t.Parent.Kind() == SyntaxKind.MethodDeclaration);
            var newIdentifier = SyntaxFactory.Identifier(ruleName.Replace(" ", ""));

            underwritingRule = underwritingRule.ReplaceToken(identifierToken, newIdentifier);
            return(underwritingRule);
        }