protected void CheckComplexity <TSyntax>(SyntaxNodeAnalysisContext context, Func <TSyntax, Location> getLocation, Func <TSyntax, SyntaxNode> getNodeToCheck, string declarationType) where TSyntax : SyntaxNode { var node = (TSyntax)context.Node; var walker = new CyclomaticComplexityWalker(); var nodeToCheck = getNodeToCheck(node); if (nodeToCheck == null) { return; } walker.Visit(nodeToCheck); if (walker.CyclomaticComplexity > Maximum) { context.ReportDiagnostic( Diagnostic.Create( Rule, getLocation(node), walker.IncrementLocations.ToAdditionalLocations(), walker.IncrementLocations.ToProperties(), new object[] { Maximum, walker.CyclomaticComplexity, declarationType })); } }
public override int GetComplexity(SyntaxNode node) { var walker = new CyclomaticComplexityWalker(); walker.Visit(node); return(walker.CyclomaticComplexity); }
/// <summary> /// Gets info about cyclomatic complexity, counting the number of predicates within the scope of rootNode and within each nested function /// (methods and anonymous functions). /// </summary> /// <param name="rootNode">The SyntaxNode to measure cyclomatic complexity within</param> /// <returns>A sequence of RegionInfo objects, each containing the region-defining node (either rootNode or a function-defining node) /// and cyclomatic complexity values for the region. There will always be at least one RegionInfo returned for the rootNode, /// plus one for each nested function.</returns> public static IEnumerable <CyclomaticComplexityWalker.RegionInfo> GetCyclomaticComplexityInfo(this SyntaxNode rootNode, SemanticModel model) { var walker = new CyclomaticComplexityWalker(rootNode, model); walker.Visit(rootNode); return(walker.Results); }