Beispiel #1
0
        public MethodResult TranslateCode() // this needs a total re-write, any translation method/class needs access to the current method syntax, for example if a writeline is encountered, need to check following statements for a readline
        {                                   // could use inheritance, base class with CurrentMethodSyntax field, would prefer composition though
            if (_compilationUnitSyntax == null)
            {
                return(new MethodResult(false, "Compilation unit was null"));
            }

            SyntaxList <MemberDeclarationSyntax> rootMembers = _compilationUnitSyntax.Members;

            if (rootMembers.Count == 0)
            {
                return(new MethodResult(false, "Code root contained no members"));
            }

            if (rootMembers.All(x => x.Kind() != SyntaxKind.ClassDeclaration) &&
                rootMembers.All(x => x.Kind() != SyntaxKind.MethodDeclaration))
            {
                return(new MethodResult(false, "No methods or classes found in code"));
            }

            TranslatedCode = ProcessRootMembers(rootMembers);

            return(new MethodResult(true));
        }
 public static SyntaxList <UsingDirectiveSyntax> AddUsingStatements(
     this SyntaxList <UsingDirectiveSyntax> currentUsings, params string[] newUsings)
 {
     return(currentUsings.AddRange(newUsings.Where(n => currentUsings.All(c => c.Name.ToFullString() != n))
                                   .Select(u => SyntaxFactory.UsingDirective(SyntaxFactory.ParseName(u)))));
 }
        public static void Analyze(SyntaxNodeAnalysisContext context, AccessorListSyntax accessorList)
        {
            SyntaxList <AccessorDeclarationSyntax> accessors = accessorList.Accessors;

            if (accessors.Any(f => f.BodyOrExpressionBody() != null))
            {
                if (accessorList.IsSingleLine(includeExteriorTrivia: false))
                {
                    ReportDiagnostic(context, accessorList);
                }
                else
                {
                    foreach (AccessorDeclarationSyntax accessor in accessors)
                    {
                        if (ShouldBeFormatted(accessor))
                        {
                            ReportDiagnostic(context, accessor);
                        }
                    }
                }
            }
            else
            {
                SyntaxNode parent = accessorList.Parent;

                switch (parent?.Kind())
                {
                case SyntaxKind.PropertyDeclaration:
                {
                    if (accessors.All(f => !f.AttributeLists.Any()) &&
                        !accessorList.IsSingleLine(includeExteriorTrivia: false))
                    {
                        var         propertyDeclaration = (PropertyDeclarationSyntax)parent;
                        SyntaxToken identifier          = propertyDeclaration.Identifier;

                        if (!identifier.IsMissing)
                        {
                            SyntaxToken closeBrace = accessorList.CloseBraceToken;

                            if (!closeBrace.IsMissing)
                            {
                                TextSpan span = TextSpan.FromBounds(identifier.Span.End, closeBrace.Span.Start);

                                if (propertyDeclaration
                                    .DescendantTrivia(span)
                                    .All(f => f.IsWhitespaceOrEndOfLineTrivia()))
                                {
                                    ReportDiagnostic(context, accessorList);
                                }
                            }
                        }
                    }

                    break;
                }

                case SyntaxKind.IndexerDeclaration:
                {
                    if (accessors.All(f => !f.AttributeLists.Any()) &&
                        !accessorList.IsSingleLine(includeExteriorTrivia: false))
                    {
                        var indexerDeclaration = (IndexerDeclarationSyntax)parent;

                        BracketedParameterListSyntax parameterList = indexerDeclaration.ParameterList;

                        if (parameterList != null)
                        {
                            SyntaxToken closeBracket = parameterList.CloseBracketToken;

                            if (!closeBracket.IsMissing)
                            {
                                SyntaxToken closeBrace = accessorList.CloseBraceToken;

                                if (!closeBrace.IsMissing)
                                {
                                    TextSpan span = TextSpan.FromBounds(closeBracket.Span.End, closeBrace.Span.Start);

                                    if (indexerDeclaration
                                        .DescendantTrivia(span)
                                        .All(f => f.IsWhitespaceOrEndOfLineTrivia()))
                                    {
                                        ReportDiagnostic(context, accessorList);
                                    }
                                }
                            }
                        }
                    }

                    break;
                }
                }
            }
        }
        private static void AnalyzeAccessorList(SyntaxNodeAnalysisContext context)
        {
            var accessorList = (AccessorListSyntax)context.Node;

            SyntaxList <AccessorDeclarationSyntax> accessors = accessorList.Accessors;

            if (accessors.Any(f => f.BodyOrExpressionBody() != null))
            {
                if (DiagnosticDescriptors.AddNewLineBeforeAccessorOfFullProperty.IsEffective(context))
                {
                    SyntaxToken token = accessorList.OpenBraceToken;

                    foreach (AccessorDeclarationSyntax accessor in accessors)
                    {
                        if (accessor.BodyOrExpressionBody() != null &&
                            accessor.SyntaxTree.IsSingleLineSpan(TextSpan.FromBounds(token.Span.End, accessor.SpanStart)))
                        {
                            DiagnosticHelpers.ReportDiagnostic(
                                context,
                                DiagnosticDescriptors.AddNewLineBeforeAccessorOfFullProperty,
                                Location.Create(accessor.SyntaxTree, new TextSpan(accessor.SpanStart, 0)));

                            break;
                        }

                        token = accessor.Body?.CloseBraceToken ?? accessor.SemicolonToken;

                        if (!token.Equals(accessor.GetLastToken()))
                        {
                            break;
                        }
                    }
                }

                if (DiagnosticDescriptors.RemoveNewLinesFromAccessorWithSingleLineExpression.IsEffective(context) &&
                    !accessorList.IsSingleLine(includeExteriorTrivia: false))
                {
                    foreach (AccessorDeclarationSyntax accessor in accessors)
                    {
                        if (CanRemoveNewLinesFromAccessor(accessor))
                        {
                            DiagnosticHelpers.ReportDiagnostic(context, DiagnosticDescriptors.RemoveNewLinesFromAccessorWithSingleLineExpression, accessor);
                        }
                    }
                }
            }
            else if (DiagnosticDescriptors.RemoveNewLinesFromAccessorListOfAutoProperty.IsEffective(context))
            {
                SyntaxNode parent = accessorList.Parent;

                switch (parent?.Kind())
                {
                case SyntaxKind.PropertyDeclaration:
                {
                    if (accessors.All(f => !f.AttributeLists.Any()) &&
                        !accessorList.IsSingleLine(includeExteriorTrivia: false))
                    {
                        var         propertyDeclaration = (PropertyDeclarationSyntax)parent;
                        SyntaxToken identifier          = propertyDeclaration.Identifier;

                        if (!identifier.IsMissing)
                        {
                            SyntaxToken closeBrace = accessorList.CloseBraceToken;

                            if (!closeBrace.IsMissing)
                            {
                                TextSpan span = TextSpan.FromBounds(identifier.Span.End, closeBrace.SpanStart);

                                if (propertyDeclaration
                                    .DescendantTrivia(span)
                                    .All(f => f.IsWhitespaceOrEndOfLineTrivia()))
                                {
                                    DiagnosticHelpers.ReportDiagnostic(context, DiagnosticDescriptors.RemoveNewLinesFromAccessorListOfAutoProperty, accessorList);
                                }
                            }
                        }
                    }

                    break;
                }

                case SyntaxKind.IndexerDeclaration:
                {
                    if (accessors.All(f => !f.AttributeLists.Any()) &&
                        !accessorList.IsSingleLine(includeExteriorTrivia: false))
                    {
                        var indexerDeclaration = (IndexerDeclarationSyntax)parent;

                        BracketedParameterListSyntax parameterList = indexerDeclaration.ParameterList;

                        if (parameterList != null)
                        {
                            SyntaxToken closeBracket = parameterList.CloseBracketToken;

                            if (!closeBracket.IsMissing)
                            {
                                SyntaxToken closeBrace = accessorList.CloseBraceToken;

                                if (!closeBrace.IsMissing)
                                {
                                    TextSpan span = TextSpan.FromBounds(closeBracket.Span.End, closeBrace.SpanStart);

                                    if (indexerDeclaration
                                        .DescendantTrivia(span)
                                        .All(f => f.IsWhitespaceOrEndOfLineTrivia()))
                                    {
                                        DiagnosticHelpers.ReportDiagnostic(context, DiagnosticDescriptors.RemoveNewLinesFromAccessorListOfAutoProperty, accessorList);
                                    }
                                }
                            }
                        }
                    }

                    break;
                }
                }
            }
        }
Beispiel #5
0
        private static void Analyze(
            SyntaxNodeAnalysisContext context,
            SyntaxNode containingNode,
            SyntaxList <StatementSyntax> statements)
        {
            int count = statements.Count;

            if (count > 1)
            {
                int i = count - 1;

                while (i >= 0)
                {
                    if (!statements[i].IsKind(SyntaxKind.LocalFunctionStatement))
                    {
                        break;
                    }

                    i--;
                }

                if (i >= 1 &&
                    statements[i].IsKind(SyntaxKind.ReturnStatement))
                {
                    var returnStatement = (ReturnStatementSyntax)statements[i];

                    ExpressionSyntax expression = returnStatement.Expression;

                    if (expression?.IsMissing == false)
                    {
                        StatementSyntax statement = statements[i - 1];

                        SyntaxKind statementKind = statement.Kind();

                        if (statementKind == SyntaxKind.IfStatement)
                        {
                            var ifStatement = (IfStatementSyntax)statements[i - 1];

                            if (!ifStatement.IsSimpleIf())
                            {
                                SemanticModel     semanticModel     = context.SemanticModel;
                                CancellationToken cancellationToken = context.CancellationToken;

                                ISymbol symbol = semanticModel.GetSymbol(expression, cancellationToken);

                                if (IsLocalDeclaredInScopeOrNonRefOrOutParameterOfEnclosingSymbol(symbol, containingNode, semanticModel, cancellationToken) &&
                                    ifStatement.GetChain().All(ifOrElse => IsValueAssignedInLastStatement(ifOrElse, symbol, semanticModel, cancellationToken)) &&
                                    !containingNode.ContainsDirectives(TextSpan.FromBounds(ifStatement.SpanStart, returnStatement.Span.End)))
                                {
                                    context.ReportDiagnostic(
                                        DiagnosticDescriptors.UseReturnInsteadOfAssignment,
                                        GetLastStatementOrDefault(ifStatement));
                                }
                            }
                        }
                        else if (statementKind == SyntaxKind.SwitchStatement)
                        {
                            SemanticModel     semanticModel     = context.SemanticModel;
                            CancellationToken cancellationToken = context.CancellationToken;

                            ISymbol symbol = semanticModel.GetSymbol(expression, cancellationToken);

                            if (IsLocalDeclaredInScopeOrNonRefOrOutParameterOfEnclosingSymbol(symbol, containingNode, semanticModel, cancellationToken))
                            {
                                var switchStatement = (SwitchStatementSyntax)statements[i - 1];

                                SyntaxList <SwitchSectionSyntax> sections = switchStatement.Sections;

                                if (sections.All(section => IsValueAssignedInLastStatement(section, symbol, semanticModel, cancellationToken)) &&
                                    !containingNode.ContainsDirectives(TextSpan.FromBounds(switchStatement.SpanStart, returnStatement.Span.End)))
                                {
                                    context.ReportDiagnostic(
                                        DiagnosticDescriptors.UseReturnInsteadOfAssignment,
                                        GetLastStatementBeforeBreakStatementOrDefault(sections.First()));
                                }
                            }
                        }
                    }
                }
            }
        }