예제 #1
0
            public override SyntaxNode VisitExpressionStatement(ExpressionStatementSyntax node)
            {
                if (node.ToString().Length < 110)
                {
                    return(base.VisitExpressionStatement(node));
                }

                var m = node.Expression;
                var rewritableToken = new List <SyntaxToken>();
                var trivia          = new SyntaxTriviaList(SyntaxFactory.EndOfLine("\n"));

                trivia = trivia.AddRange(node.GetLeadingTrivia().Reverse()
                                         .TakeWhile(x => x.IsDirective ^ !x.IsKind(SyntaxKind.EndOfLineTrivia)));

                trivia = trivia.Add(SyntaxFactory.Whitespace("    "));
                var newExpression = SyntaxFactory.ParseExpression("");

                while (m != null && m.ChildNodes().Any())
                {
                    var m2 = m.ChildNodes();

                    if (m2.FirstOrDefault() is MemberAccessExpressionSyntax &&
                        m2.LastOrDefault() is ArgumentListSyntax)
                    {
                        var methodName = m2.FirstOrDefault().As <MemberAccessExpressionSyntax>();
                        var arguments  = m2.LastOrDefault().As <ArgumentListSyntax>();
                        m = m2.FirstOrDefault().As <MemberAccessExpressionSyntax>()?.Expression;

                        if (newExpression.ToString() == "")
                        {
                            newExpression = SyntaxFactory.InvocationExpression(methodName.Name, arguments)
                                            .WithoutTrailingTrivia();
                        }
                        else
                        {
                            newExpression = SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
                                                                                 SyntaxFactory.InvocationExpression(methodName.Name, arguments).WithoutTrailingTrivia(),
                                                                                 SyntaxFactory.Token(SyntaxKind.DotToken).WithLeadingTrivia(trivia), SyntaxFactory.IdentifierName(newExpression.ToString()));
                        }
                    }
                    else if (m2.FirstOrDefault() is IdentifierNameSyntax &&
                             m2.LastOrDefault() is ArgumentListSyntax)
                    {
                        var identifierName = m2.FirstOrDefault() as IdentifierNameSyntax;
                        var arguments      = m2.LastOrDefault() as ArgumentListSyntax;
                        m = null;

                        if (newExpression.ToString() == "")
                        {
                            newExpression = SyntaxFactory.InvocationExpression(identifierName, arguments).WithoutTrailingTrivia();
                        }
                        else
                        {
                            newExpression = SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
                                                                                 SyntaxFactory.InvocationExpression(identifierName.WithoutTrailingTrivia(), arguments.WithoutTrailingTrivia()),
                                                                                 SyntaxFactory.Token(SyntaxKind.DotToken).WithLeadingTrivia(trivia), SyntaxFactory.IdentifierName(newExpression.ToString()));
                        }
                    }
                    else
                    {
                        if (newExpression.ToString() == "")
                        {
                            newExpression = m.WithoutTrailingTrivia();
                        }
                        else
                        {
                            newExpression = SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
                                                                                 m.WithoutTrailingTrivia(), SyntaxFactory.Token(SyntaxKind.DotToken).WithLeadingTrivia(trivia), SyntaxFactory.IdentifierName(newExpression.ToString()));
                        }

                        m = null;
                    }
                }

                if (m != null)
                {
                    newExpression = SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
                                                                         m, SyntaxFactory.IdentifierName(newExpression.ToString()));
                }

                if (!newExpression.ToFullString().Equals(node.ToFullString()))
                {
                    var lineSpan = node.GetFileLinePosSpan();

                    AddReport(new ChangesReport(node)
                    {
                        LineNumber = lineSpan.StartLinePosition.Line,
                        Column     = lineSpan.StartLinePosition.Character,
                        Message    = "your expression should be multi lined",
                        Generator  = nameof(MultiLineExpressionRewriter)
                    });
                }

                return(SyntaxFactory.ExpressionStatement(newExpression)
                       .WithLeadingTrivia(node.GetLeadingTrivia())
                       .WithTrailingTrivia(node.GetTrailingTrivia()));
            }