< Summary

Information
Class: NexusLabs.Needlr.AgentFramework.Analyzers.AgentGraphReducerMethodAnalyzer
Assembly: NexusLabs.Needlr.AgentFramework.Analyzers
File(s): /home/runner/work/needlr/needlr/src/NexusLabs.Needlr.AgentFramework.Analyzers/AgentGraphReducerMethodAnalyzer.cs
Line coverage
97%
Covered lines: 45
Uncovered lines: 1
Coverable lines: 46
Total lines: 109
Line coverage: 97.8%
Branch coverage
85%
Covered branches: 48
Total branches: 56
Branch coverage: 85.7%
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%
IsReadOnlyListOfString(...)75%121287.5%

File(s)

/home/runner/work/needlr/needlr/src/NexusLabs.Needlr.AgentFramework.Analyzers/AgentGraphReducerMethodAnalyzer.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>ReducerMethod</c> on <c>[AgentGraphReducer]</c> references
 10/// a valid static method on the decorated class with signature
 11/// <c>static string MethodName(IReadOnlyList&lt;string&gt;)</c>.
 12/// </summary>
 13[DiagnosticAnalyzer(LanguageNames.CSharp)]
 14public sealed class AgentGraphReducerMethodAnalyzer : DiagnosticAnalyzer
 15{
 16    public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics =>
 23717        ImmutableArray.Create(MafDiagnosticDescriptors.GraphReducerMethodInvalid);
 18
 19    public override void Initialize(AnalysisContext context)
 20    {
 2421        context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
 2422        context.EnableConcurrentExecution();
 2423        context.RegisterSymbolAction(AnalyzeNamedType, SymbolKind.NamedType);
 2424    }
 25
 26    private static void AnalyzeNamedType(SymbolAnalysisContext context)
 27    {
 22828        var typeSymbol = (INamedTypeSymbol)context.Symbol;
 29
 87430        foreach (var attr in typeSymbol.GetAttributes())
 31        {
 20932            if (attr.AttributeClass?.Name != "AgentGraphReducerAttribute" ||
 20933                attr.AttributeClass.ContainingNamespace?.ToDisplayString() != "NexusLabs.Needlr.AgentFramework")
 34            {
 35                continue;
 36            }
 37
 1438            string? reducerMethodName = null;
 5639            foreach (var named in attr.NamedArguments)
 40            {
 1441                if (named.Key == "ReducerMethod" && named.Value.Value is string val)
 42                {
 1443                    reducerMethodName = val;
 44                }
 45            }
 46
 1447            if (string.IsNullOrWhiteSpace(reducerMethodName))
 48            {
 49                continue;
 50            }
 51
 1452            var isValid = false;
 1453            var current = typeSymbol;
 3954            while (current != null)
 55            {
 7656                foreach (var member in current.GetMembers(reducerMethodName!))
 57                {
 1258                    if (member is IMethodSymbol method &&
 1259                        method.DeclaredAccessibility == Accessibility.Public &&
 1260                        method.IsStatic &&
 1261                        method.ReturnType.SpecialType == SpecialType.System_String &&
 1262                        method.Parameters.Length == 1 &&
 1263                        IsReadOnlyListOfString(method.Parameters[0].Type))
 64                    {
 265                        isValid = true;
 266                        break;
 67                    }
 68                }
 69
 2770                if (isValid) break;
 2571                current = current.BaseType;
 72            }
 73
 1474            if (!isValid)
 75            {
 1276                var location = attr.ApplicationSyntaxReference?.GetSyntax(context.CancellationToken).GetLocation()
 1277                    ?? typeSymbol.Locations.FirstOrDefault()
 1278                    ?? Location.None;
 79
 1280                context.ReportDiagnostic(Diagnostic.Create(
 1281                    MafDiagnosticDescriptors.GraphReducerMethodInvalid,
 1282                    location,
 1283                    reducerMethodName,
 1284                    typeSymbol.Name));
 85            }
 86        }
 22887    }
 88
 89    private static bool IsReadOnlyListOfString(ITypeSymbol type)
 90    {
 691        if (type is not INamedTypeSymbol namedType || !namedType.IsGenericType)
 92        {
 293            return false;
 94        }
 95
 496        var constructedFrom = namedType.ConstructedFrom;
 497        if (constructedFrom.Name != "IReadOnlyList" || constructedFrom.Arity != 1)
 98        {
 099            return false;
 100        }
 101
 4102        if (constructedFrom.ContainingNamespace?.ToDisplayString() != "System.Collections.Generic")
 103        {
 2104            return false;
 105        }
 106
 2107        return namedType.TypeArguments[0].SpecialType == SpecialType.System_String;
 108    }
 109}