Exemplo n.º 1
0
        public override int GetCognitiveComplexity(SyntaxNode node)
        {
            var walker = new CognitiveComplexityWalker();

            walker.Walk(node);
            return(walker.VisitEndedCorrectly ? walker.Complexity : -1);
        }
        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 }));
            }
        }