< Summary

Information
Class: NexusLabs.Needlr.AgentFramework.Analyzers.AgentGraphConditionMethodAnalyzer
Assembly: NexusLabs.Needlr.AgentFramework.Analyzers
File(s): /home/runner/work/needlr/needlr/src/NexusLabs.Needlr.AgentFramework.Analyzers/AgentGraphConditionMethodAnalyzer.cs
Line coverage
100%
Covered lines: 38
Uncovered lines: 0
Coverable lines: 38
Total lines: 88
Line coverage: 100%
Branch coverage
88%
Covered branches: 39
Total branches: 44
Branch coverage: 88.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_SupportedDiagnostics()100%11100%
Initialize(...)100%11100%
AnalyzeNamedType(...)88.63%4444100%

File(s)

/home/runner/work/needlr/needlr/src/NexusLabs.Needlr.AgentFramework.Analyzers/AgentGraphConditionMethodAnalyzer.cs

#LineLine coverage
 1using System.Collections.Immutable;
 2
 3using Microsoft.CodeAnalysis;
 4using Microsoft.CodeAnalysis.Diagnostics;
 5
 6namespace NexusLabs.Needlr.AgentFramework.Analyzers;
 7
 8/// <summary>
 9/// Validates that <c>Condition</c> on <c>[AgentGraphEdge]</c> references a
 10/// valid static method on the decorated class with signature
 11/// <c>static bool MethodName(object?)</c>.
 12/// </summary>
 13[DiagnosticAnalyzer(LanguageNames.CSharp)]
 14public sealed class AgentGraphConditionMethodAnalyzer : DiagnosticAnalyzer
 15{
 16    public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics =>
 20017        ImmutableArray.Create(MafDiagnosticDescriptors.GraphConditionMethodInvalid);
 18
 19    public override void Initialize(AnalysisContext context)
 20    {
 2121        context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
 2122        context.EnableConcurrentExecution();
 2123        context.RegisterSymbolAction(AnalyzeNamedType, SymbolKind.NamedType);
 2124    }
 25
 26    private static void AnalyzeNamedType(SymbolAnalysisContext context)
 27    {
 20928        var typeSymbol = (INamedTypeSymbol)context.Symbol;
 29
 83430        foreach (var attr in typeSymbol.GetAttributes())
 31        {
 20832            if (attr.AttributeClass?.Name != "AgentGraphEdgeAttribute" ||
 20833                attr.AttributeClass.ContainingNamespace?.ToDisplayString() != "NexusLabs.Needlr.AgentFramework")
 34            {
 35                continue;
 36            }
 37
 1338            string? conditionName = null;
 5039            foreach (var named in attr.NamedArguments)
 40            {
 1241                if (named.Key == "Condition" && named.Value.Value is string val)
 42                {
 1243                    conditionName = val;
 44                }
 45            }
 46
 1347            if (string.IsNullOrWhiteSpace(conditionName))
 48            {
 49                continue;
 50            }
 51
 1252            var isValid = false;
 1253            var current = typeSymbol;
 3354            while (current != null)
 55            {
 6456                foreach (var member in current.GetMembers(conditionName!))
 57                {
 1058                    if (member is IMethodSymbol method &&
 1059                        method.DeclaredAccessibility == Accessibility.Public &&
 1060                        method.IsStatic &&
 1061                        method.ReturnType.SpecialType == SpecialType.System_Boolean &&
 1062                        method.Parameters.Length == 1 &&
 1063                        method.Parameters[0].Type.SpecialType == SpecialType.System_Object)
 64                    {
 265                        isValid = true;
 266                        break;
 67                    }
 68                }
 69
 2370                if (isValid) break;
 2171                current = current.BaseType;
 72            }
 73
 1274            if (!isValid)
 75            {
 1076                var location = attr.ApplicationSyntaxReference?.GetSyntax(context.CancellationToken).GetLocation()
 1077                    ?? typeSymbol.Locations.FirstOrDefault()
 1078                    ?? Location.None;
 79
 1080                context.ReportDiagnostic(Diagnostic.Create(
 1081                    MafDiagnosticDescriptors.GraphConditionMethodInvalid,
 1082                    location,
 1083                    conditionName,
 1084                    typeSymbol.Name));
 85            }
 86        }
 20987    }
 88}