コード例 #1
0
        protected void CheckComplexity <TSyntax>(SyntaxNodeAnalysisContext context, Func <TSyntax, SyntaxNode> nodeSelector,
                                                 Func <TSyntax, Location> getLocationToReport, string declarationType, int threshold)
            where TSyntax : SyntaxNode
        {
            var syntax        = (TSyntax)context.Node;
            var nodeToAnalyze = nodeSelector(syntax);

            if (nodeToAnalyze == null)
            {
                return;
            }

            var cognitiveWalker = new CognitiveComplexityWalker();

            cognitiveWalker.Walk(nodeToAnalyze);
            cognitiveWalker.EnsureVisitEndedCorrectly();

            if (cognitiveWalker.Complexity > Threshold)
            {
                context.ReportDiagnosticWhenActive(Diagnostic.Create(rule, getLocationToReport(syntax),
                                                                     cognitiveWalker.IncrementLocations.ToAdditionalLocations(),
                                                                     cognitiveWalker.IncrementLocations.ToProperties(),
                                                                     new object[] { declarationType, cognitiveWalker.Complexity, threshold }));
            }
        }
コード例 #2
0
ファイル: Metrics.cs プロジェクト: GuinaCosta/sonar-csharp
        public override int GetCognitiveComplexity(SyntaxNode node)
        {
            var walker = new CognitiveComplexityWalker();

            walker.Visit(node);
            return(walker.VisitEndedCorrectly ? walker.Complexity : -1);
        }
コード例 #3
0
        protected void CheckComplexity <TSyntax>(SyntaxNodeAnalysisContext context,
                                                 Func <TSyntax, Location> getLocationToReport, string declarationType, int threshold)
            where TSyntax : SyntaxNode
        {
            var syntax = (TSyntax)context.Node;

            var cognitiveWalker = new CognitiveComplexityWalker();

            cognitiveWalker.Visit(syntax);
            if (cognitiveWalker.NestingLevel != 0)
            {
                throw new Exception($"There is a problem with the cognitive complexity walker. Expecting ending nesting to be '0' got '{cognitiveWalker.NestingLevel}'");
            }

            if (cognitiveWalker.Complexity > Threshold)
            {
                context.ReportDiagnostic(Diagnostic.Create(SupportedDiagnostics.First(), getLocationToReport(syntax),
                                                           cognitiveWalker.Flow.Select(x => x.Location),
                                                           CreatePropertiesFromCognitiveTrace(cognitiveWalker.Flow),
                                                           new object[] { declarationType, cognitiveWalker.Complexity, threshold }));
            }
        }