| | | 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>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)] |
| | | 14 | | public sealed class AgentGraphConditionMethodAnalyzer : DiagnosticAnalyzer |
| | | 15 | | { |
| | | 16 | | public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => |
| | 200 | 17 | | ImmutableArray.Create(MafDiagnosticDescriptors.GraphConditionMethodInvalid); |
| | | 18 | | |
| | | 19 | | public override void Initialize(AnalysisContext context) |
| | | 20 | | { |
| | 21 | 21 | | context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| | 21 | 22 | | context.EnableConcurrentExecution(); |
| | 21 | 23 | | context.RegisterSymbolAction(AnalyzeNamedType, SymbolKind.NamedType); |
| | 21 | 24 | | } |
| | | 25 | | |
| | | 26 | | private static void AnalyzeNamedType(SymbolAnalysisContext context) |
| | | 27 | | { |
| | 209 | 28 | | var typeSymbol = (INamedTypeSymbol)context.Symbol; |
| | | 29 | | |
| | 834 | 30 | | foreach (var attr in typeSymbol.GetAttributes()) |
| | | 31 | | { |
| | 208 | 32 | | if (attr.AttributeClass?.Name != "AgentGraphEdgeAttribute" || |
| | 208 | 33 | | attr.AttributeClass.ContainingNamespace?.ToDisplayString() != "NexusLabs.Needlr.AgentFramework") |
| | | 34 | | { |
| | | 35 | | continue; |
| | | 36 | | } |
| | | 37 | | |
| | 13 | 38 | | string? conditionName = null; |
| | 50 | 39 | | foreach (var named in attr.NamedArguments) |
| | | 40 | | { |
| | 12 | 41 | | if (named.Key == "Condition" && named.Value.Value is string val) |
| | | 42 | | { |
| | 12 | 43 | | conditionName = val; |
| | | 44 | | } |
| | | 45 | | } |
| | | 46 | | |
| | 13 | 47 | | if (string.IsNullOrWhiteSpace(conditionName)) |
| | | 48 | | { |
| | | 49 | | continue; |
| | | 50 | | } |
| | | 51 | | |
| | 12 | 52 | | var isValid = false; |
| | 12 | 53 | | var current = typeSymbol; |
| | 33 | 54 | | while (current != null) |
| | | 55 | | { |
| | 64 | 56 | | foreach (var member in current.GetMembers(conditionName!)) |
| | | 57 | | { |
| | 10 | 58 | | if (member is IMethodSymbol method && |
| | 10 | 59 | | method.DeclaredAccessibility == Accessibility.Public && |
| | 10 | 60 | | method.IsStatic && |
| | 10 | 61 | | method.ReturnType.SpecialType == SpecialType.System_Boolean && |
| | 10 | 62 | | method.Parameters.Length == 1 && |
| | 10 | 63 | | method.Parameters[0].Type.SpecialType == SpecialType.System_Object) |
| | | 64 | | { |
| | 2 | 65 | | isValid = true; |
| | 2 | 66 | | break; |
| | | 67 | | } |
| | | 68 | | } |
| | | 69 | | |
| | 23 | 70 | | if (isValid) break; |
| | 21 | 71 | | current = current.BaseType; |
| | | 72 | | } |
| | | 73 | | |
| | 12 | 74 | | if (!isValid) |
| | | 75 | | { |
| | 10 | 76 | | var location = attr.ApplicationSyntaxReference?.GetSyntax(context.CancellationToken).GetLocation() |
| | 10 | 77 | | ?? typeSymbol.Locations.FirstOrDefault() |
| | 10 | 78 | | ?? Location.None; |
| | | 79 | | |
| | 10 | 80 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 10 | 81 | | MafDiagnosticDescriptors.GraphConditionMethodInvalid, |
| | 10 | 82 | | location, |
| | 10 | 83 | | conditionName, |
| | 10 | 84 | | typeSymbol.Name)); |
| | | 85 | | } |
| | | 86 | | } |
| | 209 | 87 | | } |
| | | 88 | | } |