| | | 1 | | using System.Collections.Immutable; |
| | | 2 | | |
| | | 3 | | using Microsoft.CodeAnalysis; |
| | | 4 | | using Microsoft.CodeAnalysis.Diagnostics; |
| | | 5 | | |
| | | 6 | | namespace 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<string>)</c>. |
| | | 12 | | /// </summary> |
| | | 13 | | [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| | | 14 | | public sealed class AgentGraphReducerMethodAnalyzer : DiagnosticAnalyzer |
| | | 15 | | { |
| | | 16 | | public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => |
| | 237 | 17 | | ImmutableArray.Create(MafDiagnosticDescriptors.GraphReducerMethodInvalid); |
| | | 18 | | |
| | | 19 | | public override void Initialize(AnalysisContext context) |
| | | 20 | | { |
| | 24 | 21 | | context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| | 24 | 22 | | context.EnableConcurrentExecution(); |
| | 24 | 23 | | context.RegisterSymbolAction(AnalyzeNamedType, SymbolKind.NamedType); |
| | 24 | 24 | | } |
| | | 25 | | |
| | | 26 | | private static void AnalyzeNamedType(SymbolAnalysisContext context) |
| | | 27 | | { |
| | 228 | 28 | | var typeSymbol = (INamedTypeSymbol)context.Symbol; |
| | | 29 | | |
| | 874 | 30 | | foreach (var attr in typeSymbol.GetAttributes()) |
| | | 31 | | { |
| | 209 | 32 | | if (attr.AttributeClass?.Name != "AgentGraphReducerAttribute" || |
| | 209 | 33 | | attr.AttributeClass.ContainingNamespace?.ToDisplayString() != "NexusLabs.Needlr.AgentFramework") |
| | | 34 | | { |
| | | 35 | | continue; |
| | | 36 | | } |
| | | 37 | | |
| | 14 | 38 | | string? reducerMethodName = null; |
| | 56 | 39 | | foreach (var named in attr.NamedArguments) |
| | | 40 | | { |
| | 14 | 41 | | if (named.Key == "ReducerMethod" && named.Value.Value is string val) |
| | | 42 | | { |
| | 14 | 43 | | reducerMethodName = val; |
| | | 44 | | } |
| | | 45 | | } |
| | | 46 | | |
| | 14 | 47 | | if (string.IsNullOrWhiteSpace(reducerMethodName)) |
| | | 48 | | { |
| | | 49 | | continue; |
| | | 50 | | } |
| | | 51 | | |
| | 14 | 52 | | var isValid = false; |
| | 14 | 53 | | var current = typeSymbol; |
| | 39 | 54 | | while (current != null) |
| | | 55 | | { |
| | 76 | 56 | | foreach (var member in current.GetMembers(reducerMethodName!)) |
| | | 57 | | { |
| | 12 | 58 | | if (member is IMethodSymbol method && |
| | 12 | 59 | | method.DeclaredAccessibility == Accessibility.Public && |
| | 12 | 60 | | method.IsStatic && |
| | 12 | 61 | | method.ReturnType.SpecialType == SpecialType.System_String && |
| | 12 | 62 | | method.Parameters.Length == 1 && |
| | 12 | 63 | | IsReadOnlyListOfString(method.Parameters[0].Type)) |
| | | 64 | | { |
| | 2 | 65 | | isValid = true; |
| | 2 | 66 | | break; |
| | | 67 | | } |
| | | 68 | | } |
| | | 69 | | |
| | 27 | 70 | | if (isValid) break; |
| | 25 | 71 | | current = current.BaseType; |
| | | 72 | | } |
| | | 73 | | |
| | 14 | 74 | | if (!isValid) |
| | | 75 | | { |
| | 12 | 76 | | var location = attr.ApplicationSyntaxReference?.GetSyntax(context.CancellationToken).GetLocation() |
| | 12 | 77 | | ?? typeSymbol.Locations.FirstOrDefault() |
| | 12 | 78 | | ?? Location.None; |
| | | 79 | | |
| | 12 | 80 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 12 | 81 | | MafDiagnosticDescriptors.GraphReducerMethodInvalid, |
| | 12 | 82 | | location, |
| | 12 | 83 | | reducerMethodName, |
| | 12 | 84 | | typeSymbol.Name)); |
| | | 85 | | } |
| | | 86 | | } |
| | 228 | 87 | | } |
| | | 88 | | |
| | | 89 | | private static bool IsReadOnlyListOfString(ITypeSymbol type) |
| | | 90 | | { |
| | 6 | 91 | | if (type is not INamedTypeSymbol namedType || !namedType.IsGenericType) |
| | | 92 | | { |
| | 2 | 93 | | return false; |
| | | 94 | | } |
| | | 95 | | |
| | 4 | 96 | | var constructedFrom = namedType.ConstructedFrom; |
| | 4 | 97 | | if (constructedFrom.Name != "IReadOnlyList" || constructedFrom.Arity != 1) |
| | | 98 | | { |
| | 0 | 99 | | return false; |
| | | 100 | | } |
| | | 101 | | |
| | 4 | 102 | | if (constructedFrom.ContainingNamespace?.ToDisplayString() != "System.Collections.Generic") |
| | | 103 | | { |
| | 2 | 104 | | return false; |
| | | 105 | | } |
| | | 106 | | |
| | 2 | 107 | | return namedType.TypeArguments[0].SpecialType == SpecialType.System_String; |
| | | 108 | | } |
| | | 109 | | } |