public override void Initialize(WrappingAnalysisContext context)
        {
            context.RegisterSyntaxNodeActionInNonGenerated(
                c => CheckComplexity <MethodDeclarationSyntax>(c, m => m.Identifier.GetLocation()),
                SyntaxKind.MethodDeclaration);

            context.RegisterSyntaxNodeActionInNonGenerated(
                c => CheckComplexity <OperatorDeclarationSyntax>(c, o => o.OperatorKeyword.GetLocation()),
                SyntaxKind.OperatorDeclaration);

            context.RegisterSyntaxNodeActionInNonGenerated(
                c => CheckComplexity <ConstructorDeclarationSyntax>(c, co => co.Identifier.GetLocation()),
                SyntaxKind.ConstructorDeclaration);

            context.RegisterSyntaxNodeActionInNonGenerated(
                c => CheckComplexity <DestructorDeclarationSyntax>(c, d => d.Identifier.GetLocation()),
                SyntaxKind.DestructorDeclaration);

            context.RegisterSyntaxNodeActionInNonGenerated(
                c => CheckComplexity <AccessorDeclarationSyntax>(c, a => a.Keyword.GetLocation()),
                SyntaxKind.GetAccessorDeclaration,
                SyntaxKind.SetAccessorDeclaration,
                SyntaxKind.AddAccessorDeclaration,
                SyntaxKind.RemoveAccessorDeclaration);
        }
Exemplo n.º 2
0
        public override void Initialize(WrappingAnalysisContext context)
        {
            context.RegisterSyntaxTreeActionInNonGenerated(
                c =>
            {
                var root = c.Tree.GetRoot();

                var rootExpressions =
                    root
                    .DescendantNodes(e2 => !(e2 is ExpressionSyntax))
                    .Where(
                        e =>
                        e is ExpressionSyntax &&
                        !IsCompoundExpression(e));

                var compoundExpressionsDescendants =
                    root
                    .DescendantNodes()
                    .Where(IsCompoundExpression)
                    .SelectMany(
                        e =>
                        e
                        .DescendantNodes(
                            e2 =>
                            e == e2 ||
                            !(e2 is ExpressionSyntax))
                        .Where(
                            e2 =>
                            e2 is ExpressionSyntax &&
                            !IsCompoundExpression(e2)));

                var expressionsToCheck = rootExpressions.Concat(compoundExpressionsDescendants);

                var complexExpressions =
                    expressionsToCheck
                    .Select(
                        e =>
                        new
                {
                    Expression = e,
                    Complexity =
                        e
                        .DescendantNodesAndSelf(e2 => !IsCompoundExpression(e2))
                        .Count(
                            e2 =>
                            e2.IsKind(SyntaxKind.ConditionalExpression) ||
                            e2.IsKind(SyntaxKind.LogicalAndExpression) ||
                            e2.IsKind(SyntaxKind.LogicalOrExpression))
                })
                    .Where(e => e.Complexity > Maximum);

                foreach (var complexExpression in complexExpressions)
                {
                    c.ReportDiagnostic(Diagnostic.Create(Rule, complexExpression.Expression.GetLocation(), Maximum, complexExpression.Complexity));
                }
            });
        }
        public override void Initialize(WrappingAnalysisContext context)
        {
            context.RegisterSyntaxTreeActionInNonGenerated(
                c =>
                {
                    var root = c.Tree.GetRoot();

                    var rootExpressions =
                        root
                        .DescendantNodes(e2 => !(e2 is ExpressionSyntax))
                        .Where(
                            e =>
                                e is ExpressionSyntax &&
                                !IsCompoundExpression(e));

                    var compoundExpressionsDescendants =
                        root
                        .DescendantNodes()
                        .Where(IsCompoundExpression)
                        .SelectMany(
                            e =>
                                e
                                .DescendantNodes(
                                    e2 =>
                                        e == e2 ||
                                        !(e2 is ExpressionSyntax))
                                .Where(
                                    e2 =>
                                        e2 is ExpressionSyntax &&
                                        !IsCompoundExpression(e2)));

                    var expressionsToCheck = rootExpressions.Concat(compoundExpressionsDescendants);

                    var complexExpressions =
                        expressionsToCheck
                        .Select(
                            e =>
                            new
                            {
                                Expression = e,
                                Complexity =
                                    e
                                    .DescendantNodesAndSelf(e2 => !IsCompoundExpression(e2))
                                    .Count(
                                        e2 =>
                                            e2.IsKind(SyntaxKind.ConditionalExpression) ||
                                            e2.IsKind(SyntaxKind.LogicalAndExpression) ||
                                            e2.IsKind(SyntaxKind.LogicalOrExpression))
                            })
                        .Where(e => e.Complexity > Maximum);

                    foreach (var complexExpression in complexExpressions)
                    {
                        c.ReportDiagnostic(Diagnostic.Create(Rule, complexExpression.Expression.GetLocation(), Maximum, complexExpression.Complexity));
                    }
                });
        }
Exemplo n.º 4
0
 public override void Initialize(WrappingAnalysisContext context)
 {
     context.RegisterSyntaxTreeActionInNonGenerated(
         c =>
     {
         foreach (var line in c.Tree.GetText().Lines
                  .Where(line => line.Span.Length > Maximum))
         {
             c.ReportDiagnostic(Diagnostic.Create(Rule, c.Tree.GetLocation(line.Span), Maximum, line.Span.Length));
         }
     });
 }
        public sealed override void Initialize(AnalysisContext context)
        {
            var analysisContext = new WrappingAnalysisContext(context);
            Initialize(analysisContext);

            context.RegisterCompilationStartAction(
                cac =>
                {
                    ParameterLoader.SetParameterValues(this, cac.Options);
                    analysisContext.CompilationStartActions.ForEach(startAction => startAction(cac));
                });
        }
Exemplo n.º 6
0
 public override void Initialize(WrappingAnalysisContext context)
 {
     context.RegisterSyntaxTreeActionInNonGenerated(
         c =>
         {
             foreach (var line in c.Tree.GetText().Lines
                 .Where(line => line.Span.Length > Maximum))
             {
                 c.ReportDiagnostic(Diagnostic.Create(Rule, c.Tree.GetLocation(line.Span), Maximum, line.Span.Length));
             }
         });
 }
Exemplo n.º 7
0
        public override void Initialize(WrappingAnalysisContext context)
        {
            context.RegisterSyntaxNodeActionInNonGenerated(
                c =>
                {
                    var identifier = ((ClassDeclarationSyntax)c.Node).Identifier;

                    if (!Regex.IsMatch(identifier.Text, Convention))
                    {
                        c.ReportDiagnostic(Diagnostic.Create(Rule, identifier.GetLocation(), Convention, identifier.Text));
                    }
                },
                SyntaxKind.ClassDeclaration);
        }
Exemplo n.º 8
0
        public override void Initialize(WrappingAnalysisContext context)
        {
            context.RegisterSyntaxNodeActionInNonGenerated(
                c =>
            {
                var identifier = ((ClassDeclarationSyntax)c.Node).Identifier;

                if (!Regex.IsMatch(identifier.Text, Convention))
                {
                    c.ReportDiagnostic(Diagnostic.Create(Rule, identifier.GetLocation(), Convention, identifier.Text));
                }
            },
                SyntaxKind.ClassDeclaration);
        }
        public override void Initialize(WrappingAnalysisContext context)
        {
            context.RegisterSyntaxNodeActionInNonGenerated(
                c =>
                {
                    var switchNode = (SwitchStatementSyntax)c.Node;
                    var labels = NumberOfLabels(switchNode);

                    if (labels > Maximum)
                    {
                        c.ReportDiagnostic(Diagnostic.Create(Rule, switchNode.SwitchKeyword.GetLocation(), Maximum, labels));
                    }
                },
                SyntaxKind.SwitchStatement);
        }
Exemplo n.º 10
0
        public override void Initialize(WrappingAnalysisContext context)
        {
            context.RegisterSyntaxNodeActionInNonGenerated(
                c =>
                {
                    var parameterListNode = (ParameterListSyntax)c.Node;
                    var parameters = parameterListNode.Parameters.Count;

                    if (parameters > Maximum)
                    {
                        c.ReportDiagnostic(Diagnostic.Create(Rule, parameterListNode.GetLocation(), Maximum, parameters, ExtractName(parameterListNode)));
                    }
                },
                SyntaxKind.ParameterList);
        }
Exemplo n.º 11
0
        public override void Initialize(WrappingAnalysisContext context)
        {
            context.RegisterSyntaxTreeActionInNonGenerated(
                c =>
            {
                var root  = c.Tree.GetRoot();
                var lines = root.GetLocation().GetLineSpan().EndLinePosition.Line + 1;

                if (lines > Maximum)
                {
                    var firstLine = c.Tree.GetText().Lines.First();
                    c.ReportDiagnostic(Diagnostic.Create(Rule, c.Tree.GetLocation(firstLine.Span), Maximum, lines));
                }
            });
        }
Exemplo n.º 12
0
        public override void Initialize(WrappingAnalysisContext context)
        {
            context.RegisterSyntaxNodeActionInNonGenerated(
                c =>
            {
                var parameterListNode = (ParameterListSyntax)c.Node;
                var parameters        = parameterListNode.Parameters.Count;

                if (parameters > Maximum)
                {
                    c.ReportDiagnostic(Diagnostic.Create(Rule, parameterListNode.GetLocation(), Maximum, parameters, ExtractName(parameterListNode)));
                }
            },
                SyntaxKind.ParameterList);
        }
Exemplo n.º 13
0
        public override void Initialize(WrappingAnalysisContext context)
        {
            context.RegisterSyntaxTreeActionInNonGenerated(
                c =>
                {
                    var root = c.Tree.GetRoot();
                    var lines = root.GetLocation().GetLineSpan().EndLinePosition.Line + 1;

                    if (lines > Maximum)
                    {
                        var firstLine = c.Tree.GetText().Lines.First();
                        c.ReportDiagnostic(Diagnostic.Create(Rule, c.Tree.GetLocation(firstLine.Span), Maximum, lines));
                    }
                });
        }
        public override void Initialize(WrappingAnalysisContext context)
        {
            context.RegisterSyntaxNodeActionInNonGenerated(
                c =>
            {
                var switchNode = (SwitchStatementSyntax)c.Node;
                var labels     = NumberOfLabels(switchNode);

                if (labels > Maximum)
                {
                    c.ReportDiagnostic(Diagnostic.Create(Rule, switchNode.SwitchKeyword.GetLocation(), Maximum, labels));
                }
            },
                SyntaxKind.SwitchStatement);
        }
 public abstract void Initialize(WrappingAnalysisContext context);