예제 #1
0
        private MethodDeclarationSyntax AddReturnLogging(MethodDeclarationSyntax methodDecl)
        {
            var returnType = methodDecl.ReturnType;
            MethodDeclarationSyntax newMethod = methodDecl;

            var isVoid = returnType.ChildTokens().Any(x => x.Kind() == SyntaxKind.VoidKeyword);

            var returnStatements = methodDecl
                                   .Body
                                   .DescendantNodes()
                                   .OfType <ReturnStatementSyntax>()
                                   .Where(node => methodDecl.DirectlyContains(node))
                                   .ToArray();

            newMethod = newMethod.TrackNodes(returnStatements);

            if (isVoid)
            {
                var loggingInvocation = GetLoggingStatementWithDictionaryState(methodDecl, LogMethodReturnName);

                loggingInvocation = loggingInvocation.WithTrailingTrivia(SF.LineFeed);

                foreach (var statement in returnStatements)
                {
                    newMethod = newMethod.WithTracking(x => x.InsertNodesBefore, statement, new[] { loggingInvocation });
                }

                return(newMethod);
            }

            var resultBuffVarBame      = "result";
            var isResultBuffVarCreated = false;

            foreach (var statement in returnStatements)
            {
                switch (statement.Expression)
                {
                case IdentifierNameSyntax id:
                    var loggingInvocation = GetLoggingStatementWithSingleStateWithoutName(
                        methodDecl,
                        LogMethodReturnName,
                        id.ChildTokens().Single());

                    loggingInvocation = loggingInvocation
                                        .WithLeadingTrivia(SF.LineFeed)
                                        .WithTrailingTrivia(SF.LineFeed);

                    newMethod = newMethod.WithTracking(x => x.InsertNodesBefore, statement, new[] { loggingInvocation });
                    break;

                default:
                    if (isResultBuffVarCreated == false)
                    {
                        var declaration = LocalVariableExtensions
                                          .LocalVairableDeclaration(
                            methodDecl.ReturnType,
                            resultBuffVarBame,
                            statement.Expression)
                                          .WithTrailingTrivia(SF.EndOfLine("\r\n"));

                        newMethod = newMethod.WithTracking(x => x.InsertNodesBefore, statement, new[] { declaration });
                        isResultBuffVarCreated = true;
                    }
                    else
                    {
                        var assignment = SF.ExpressionStatement(
                            SF.AssignmentExpression(
                                SyntaxKind.SimpleAssignmentExpression,
                                SF.IdentifierName(resultBuffVarBame),
                                statement.Expression))
                                         .WithTrailingTrivia(SF.EndOfLine("\r\n"));

                        newMethod = newMethod.WithTracking(x => x.InsertNodesBefore, statement, new[] { assignment });
                    }

                    var logExpression = GetLoggingStatementWithSingleStateWithoutName(
                        methodDecl,
                        LogMethodReturnName,
                        SF.Identifier(resultBuffVarBame))
                                        .WithTrailingTrivia(SF.EndOfLine("\r\n"));

                    newMethod = newMethod.WithTracking(x => x.InsertNodesBefore, statement, new[] { logExpression });
                    var newReturn = SF.ReturnStatement(SF.IdentifierName(" result"));

                    newMethod = newMethod.WithTracking(x => x.ReplaceNode, statement, new[] { newReturn });
                    break;
                }
            }

            return(newMethod);
        }