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 })); } }
public override int GetCognitiveComplexity(SyntaxNode node) { var walker = new CognitiveComplexityWalker(); walker.Visit(node); return(walker.VisitEndedCorrectly ? walker.Complexity : -1); }
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 })); } }