コード例 #1
0
        protected override void Initialize(ParameterLoadingAnalysisContext context)
        {
            context.RegisterSyntaxTreeActionInNonGenerated(stac =>
            {
                if (HeaderFormat == null)
                {
                    return;
                }

                if (string.IsNullOrWhiteSpace(HeaderFormat))
                {
                    throw new ArgumentException("Expects a non-empty license header", HeaderFormatRuleParameterKey);
                }

                if (IsRegularExpression && !IsRegexPatternValid(HeaderFormat))
                {
                    throw new ArgumentException($"Invalid regular expression: {HeaderFormat}", HeaderFormatRuleParameterKey);
                }

                var firstNode = stac.Tree.GetRoot().ChildTokens().FirstOrDefault().Parent;
                if (!HasValidLicenseHeader(firstNode))
                {
                    var properties = CreateDiagnosticProperties();
                    stac.ReportDiagnostic(Diagnostic.Create(Rule, Location.Create(stac.Tree, TextSpan.FromBounds(0, 0)), properties));
                }
            });
        }
コード例 #2
0
        protected override void Initialize(ParameterLoadingAnalysisContext 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));
                }
            });
        }
コード例 #3
0
 protected override void Initialize(ParameterLoadingAnalysisContext 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));
             }
         });
 }
コード例 #4
0
 protected override void Initialize(ParameterLoadingAnalysisContext 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));
         }
     });
 }
コード例 #5
0
 protected sealed override void Initialize(ParameterLoadingAnalysisContext context)
 {
     context.RegisterSyntaxTreeActionInNonGenerated(
         GeneratedCodeRecognizer,
         c =>
     {
         foreach (var line in c.Tree.GetText().Lines
                  .Where(line => line.Span.Length > Maximum))
         {
             c.ReportDiagnosticWhenActive(Diagnostic.Create(SupportedDiagnostics[0], c.Tree.GetLocation(line.Span),
                                                            Maximum, line.Span.Length));
         }
     });
 }
コード例 #6
0
        protected sealed override void Initialize(ParameterLoadingAnalysisContext context)
        {
            context.RegisterSyntaxTreeActionInNonGenerated(
                GeneratedCodeRecognizer,
                c =>
            {
                var root  = c.Tree.GetRoot();
                var lines = root.GetLocation().GetLineSpan().EndLinePosition.Line + 1;

                if (lines > Maximum)
                {
                    c.ReportDiagnostic(Diagnostic.Create(Rule, Location.Create(c.Tree, TextSpan.FromBounds(0, 0)), Maximum, lines));
                }
            });
        }
コード例 #7
0
        protected override void Initialize(ParameterLoadingAnalysisContext 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));
                }
            });
        }
コード例 #8
0
        protected override void Initialize(ParameterLoadingAnalysisContext 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));
                    }
                });
        }
コード例 #9
0
        protected override void Initialize(ParameterLoadingAnalysisContext context)
        {
            context.RegisterSyntaxTreeActionInNonGenerated(
                GeneratedCodeRecognizer,
                stac =>
            {
                var linesCount = stac.Tree
                                 .GetRoot()
                                 .DescendantTokens()
                                 .Where(token => !IsEndOfFileToken(token))
                                 .SelectMany(token => token.GetLineNumbers(isZeroBasedCount: false))
                                 .Distinct()
                                 .LongCount();

                if (linesCount > Maximum)
                {
                    stac.ReportDiagnosticWhenActive(Diagnostic.Create(SupportedDiagnostics[0], Location.Create(stac.Tree,
                                                                                                               TextSpan.FromBounds(0, 0)), Maximum, linesCount));
                }
            });
        }
コード例 #10
0
        protected override void Initialize(ParameterLoadingAnalysisContext 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));
                    }
                });
        }