| | | 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 | | /// Analyzer that hints when an <c>[AgentFunction]</c> <see cref="string"/> parameter is being |
| | | 10 | | /// used to carry JSON (per its name suffix or <c>[Description]</c> text) and could instead be |
| | | 11 | | /// typed as <c>System.Text.Json.JsonElement</c> for direct, typed access. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <remarks> |
| | | 14 | | /// <b>NDLRMAF030</b> (Info): a parameter is named <c>*Json</c>/<c>*_json</c> OR its description |
| | | 15 | | /// mentions <c>"JSON array"</c>/<c>"JSON object"</c>, AND its declared type is <see cref="string"/>. |
| | | 16 | | /// </remarks> |
| | | 17 | | [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| | | 18 | | public sealed class AgentFunctionJsonStringParameterAnalyzer : DiagnosticAnalyzer |
| | | 19 | | { |
| | | 20 | | private const string AgentFunctionAttributeName = "NexusLabs.Needlr.AgentFramework.AgentFunctionAttribute"; |
| | | 21 | | private const string DescriptionAttributeName = "System.ComponentModel.DescriptionAttribute"; |
| | | 22 | | |
| | | 23 | | public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => |
| | 242 | 24 | | ImmutableArray.Create(MafDiagnosticDescriptors.AgentFunctionJsonStringParameter); |
| | | 25 | | |
| | | 26 | | public override void Initialize(AnalysisContext context) |
| | | 27 | | { |
| | 26 | 28 | | context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| | 26 | 29 | | context.EnableConcurrentExecution(); |
| | 26 | 30 | | context.RegisterSymbolAction(AnalyzeMethod, SymbolKind.Method); |
| | 26 | 31 | | } |
| | | 32 | | |
| | | 33 | | private static void AnalyzeMethod(SymbolAnalysisContext context) |
| | | 34 | | { |
| | 416 | 35 | | var method = (IMethodSymbol)context.Symbol; |
| | | 36 | | |
| | 416 | 37 | | var hasAgentFunction = method.GetAttributes() |
| | 431 | 38 | | .Any(a => a.AttributeClass?.ToDisplayString() == AgentFunctionAttributeName); |
| | | 39 | | |
| | 416 | 40 | | if (!hasAgentFunction) |
| | 401 | 41 | | return; |
| | | 42 | | |
| | 68 | 43 | | foreach (var parameter in method.Parameters) |
| | | 44 | | { |
| | 19 | 45 | | if (parameter.Type.SpecialType != SpecialType.System_String) |
| | | 46 | | continue; |
| | | 47 | | |
| | 18 | 48 | | var nameSignal = LooksLikeJsonByName(parameter.Name); |
| | 18 | 49 | | var descriptionSignal = LooksLikeJsonByDescription(parameter, out var descriptionExcerpt); |
| | | 50 | | |
| | 18 | 51 | | if (!nameSignal && !descriptionSignal) |
| | | 52 | | continue; |
| | | 53 | | |
| | 12 | 54 | | var reason = nameSignal && descriptionSignal |
| | 12 | 55 | | ? $"its name '{parameter.Name}' suggests JSON content and its description mentions {descriptionExcerpt}" |
| | 12 | 56 | | : nameSignal |
| | 12 | 57 | | ? $"its name '{parameter.Name}' suggests JSON content" |
| | 12 | 58 | | : $"its description mentions {descriptionExcerpt}"; |
| | | 59 | | |
| | 12 | 60 | | var location = parameter.Locations.FirstOrDefault() ?? method.Locations[0]; |
| | | 61 | | |
| | 12 | 62 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 12 | 63 | | MafDiagnosticDescriptors.AgentFunctionJsonStringParameter, |
| | 12 | 64 | | location, |
| | 12 | 65 | | parameter.Name, |
| | 12 | 66 | | method.Name, |
| | 12 | 67 | | reason)); |
| | | 68 | | } |
| | 15 | 69 | | } |
| | | 70 | | |
| | | 71 | | private static bool LooksLikeJsonByName(string name) => |
| | 18 | 72 | | name.EndsWith("Json", StringComparison.Ordinal) || |
| | 18 | 73 | | name.EndsWith("_json", StringComparison.Ordinal); |
| | | 74 | | |
| | | 75 | | private static bool LooksLikeJsonByDescription(IParameterSymbol parameter, out string excerpt) |
| | | 76 | | { |
| | 18 | 77 | | var descriptionAttr = parameter.GetAttributes() |
| | 36 | 78 | | .FirstOrDefault(a => a.AttributeClass?.ToDisplayString() == DescriptionAttributeName); |
| | | 79 | | |
| | 18 | 80 | | if (descriptionAttr is null |
| | 18 | 81 | | || descriptionAttr.ConstructorArguments.Length < 1 |
| | 18 | 82 | | || descriptionAttr.ConstructorArguments[0].Value is not string text) |
| | | 83 | | { |
| | 0 | 84 | | excerpt = string.Empty; |
| | 0 | 85 | | return false; |
| | | 86 | | } |
| | | 87 | | |
| | 18 | 88 | | if (text.IndexOf("JSON array", StringComparison.OrdinalIgnoreCase) >= 0) |
| | | 89 | | { |
| | 6 | 90 | | excerpt = "\"JSON array\""; |
| | 6 | 91 | | return true; |
| | | 92 | | } |
| | | 93 | | |
| | 12 | 94 | | if (text.IndexOf("JSON object", StringComparison.OrdinalIgnoreCase) >= 0) |
| | | 95 | | { |
| | 2 | 96 | | excerpt = "\"JSON object\""; |
| | 2 | 97 | | return true; |
| | | 98 | | } |
| | | 99 | | |
| | 10 | 100 | | excerpt = string.Empty; |
| | 10 | 101 | | return false; |
| | | 102 | | } |
| | | 103 | | } |