public override SyntaxNode VisitIfStatement(IfStatementSyntax mainIFnode)
            {
                var newIfNode = Cleanup(mainIFnode);

                if (!string.Equals(newIfNode.ToFullString().Trim(), mainIFnode.ToFullString().Trim(), StringComparison.Ordinal))
                {
                    var nextToken = mainIFnode.GetLastToken().GetNextToken();

                    var lineSpan = mainIFnode.GetFileLinePosSpan();

                    AddReport(new ChangesReport(mainIFnode)
                    {
                        LineNumber = lineSpan.StartLinePosition.Line,
                        Column     = lineSpan.StartLinePosition.Character,
                        Message    = "Compact small if-else statements",
                        Generator  = nameof(CompactSmallIfElseStatements)
                    });

                    if
                    (
                        nextToken != null &&
                        mainIFnode.GetTrailingTrivia().Any(t => t.IsKind(SyntaxKind.EndOfLineTrivia)) == false &&
                        nextToken.LeadingTrivia.Any(t => t.IsKind(SyntaxKind.EndOfLineTrivia)) == false
                    )
                    {
                        return(newIfNode.WithTrailingTrivia(newIfNode.GetTrailingTrivia().Add(_endOfLineTrivia)));
                    }

                    return(newIfNode.WithTrailingTrivia(newIfNode.GetTrailingTrivia()));
                }

                return(mainIFnode);
            }
Esempio n. 2
0
            public override SyntaxNode VisitIfStatement(IfStatementSyntax mainIFnode)
            {
                var newIfNode = Cleanup(mainIFnode);

                if (!string.Equals(newIfNode.ToFullString().Trim(), mainIFnode.ToFullString().Trim(), StringComparison.Ordinal))
                {
                    var nextToken = mainIFnode.GetLastToken().GetNextToken();

                    if
                    (
                        nextToken != null &&
                        mainIFnode.GetTrailingTrivia().Any(t => t.IsKind(SyntaxKind.EndOfLineTrivia)) == false &&
                        nextToken.LeadingTrivia.Any(t => t.IsKind(SyntaxKind.EndOfLineTrivia)) == false
                    )
                    {
                        return(newIfNode.WithTrailingTrivia(newIfNode.GetTrailingTrivia().Add(_endOfLineTrivia)));
                    }
                    return(newIfNode.WithTrailingTrivia(newIfNode.GetTrailingTrivia()));
                }
                return(mainIFnode);
            }