Example #1
0
        MethodDeclarationSyntax RewriteMethod(SourceFile file, MethodInfo inMethodInfo)
        {
            var inMethodSyntax = inMethodInfo.DeclarationSyntax;
            //Log.LogMessage("Method {0}: {1}", inMethodInfo.Symbol.Name, inMethodInfo.Symbol.);

            Log.LogMessage(MessageImportance.Low, "  Rewriting method {0} to {1}", inMethodInfo.Symbol.Name, inMethodInfo.Transformed);

            // Visit all method invocations inside the method, rewrite them to async if needed
            var rewriter = new MethodInvocationRewriter(Log, file.SemanticModel, _excludedTypes);
            var outMethod = (MethodDeclarationSyntax)rewriter.Visit(inMethodSyntax);

            // Method signature
            outMethod = outMethod
                .WithIdentifier(SyntaxFactory.Identifier(inMethodInfo.Transformed))
                .WithAttributeLists(new SyntaxList<AttributeListSyntax>())
                .WithModifiers(inMethodSyntax.Modifiers
                  .Add(SyntaxFactory.Token(SyntaxKind.AsyncKeyword))
                  //.Remove(SyntaxFactory.Token(SyntaxKind.OverrideKeyword))
                  //.Remove(SyntaxFactory.Token(SyntaxKind.NewKeyword))
                );

            // Transform return type adding Task<>
            var returnType = inMethodSyntax.ReturnType.ToString();
            outMethod = outMethod.WithReturnType(SyntaxFactory.ParseTypeName(
                returnType == "void" ? "Task" : String.Format("Task<{0}>", returnType))
            );

            // Remove the override and new attributes. Seems like the clean .Remove above doesn't work...
            for (var i = 0; i < outMethod.Modifiers.Count;)
            {
                var text = outMethod.Modifiers[i].Text;
                if (text == "override" || text == "new") {
                    outMethod = outMethod.WithModifiers(outMethod.Modifiers.RemoveAt(i));
                    continue;
                }
                i++;
            }

            if (inMethodInfo.WithOverride) {
                outMethod = outMethod.AddModifiers(SyntaxFactory.Token(SyntaxKind.OverrideKeyword));
            }

            return outMethod;
        }
Example #2
0
 CompilationUnitSyntax RewriteFile(SourceFile file)
 {
     return SyntaxFactory.CompilationUnit()
       .WithUsings(SyntaxFactory.List(
           file.SyntaxTree.GetRoot().DescendantNodes().OfType<UsingDirectiveSyntax>().Concat(ExtraUsingDirectives)
       ))
       .WithMembers(SyntaxFactory.List<MemberDeclarationSyntax>(file.NamespaceToClasses.Select(ntc =>
           SyntaxFactory.NamespaceDeclaration(ntc.Key.Name)
           .WithMembers(SyntaxFactory.List<MemberDeclarationSyntax>(ntc.Value.Select(mbc =>
               SyntaxFactory.ClassDeclaration(mbc.Key.Identifier)
               .WithModifiers(mbc.Key.Modifiers)
               .WithMembers(SyntaxFactory.List<MemberDeclarationSyntax>(mbc.Value.Select(m => RewriteMethod(file, m))))
           ).ToArray()))
       )))
       .WithEndOfFileToken(SyntaxFactory.Token(SyntaxKind.EndOfFileToken))
       .NormalizeWhitespace();
 }