| | | 1 | | // Copyright (c) NexusLabs. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System.Collections.Concurrent; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Collections.Immutable; |
| | | 7 | | |
| | | 8 | | using Microsoft.CodeAnalysis; |
| | | 9 | | using Microsoft.CodeAnalysis.CSharp; |
| | | 10 | | using Microsoft.CodeAnalysis.CSharp.Syntax; |
| | | 11 | | using Microsoft.CodeAnalysis.Diagnostics; |
| | | 12 | | |
| | | 13 | | namespace NexusLabs.Needlr.Generators; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Analyzer for <c>[HttpClientOptions]</c> usage. Enforces the contracts the generator |
| | | 17 | | /// relies on and reports six diagnostics: |
| | | 18 | | /// <list type="bullet"> |
| | | 19 | | /// <item><description>NDLRHTTP001 — target must implement <c>INamedHttpClientOptions</c></description></item> |
| | | 20 | | /// <item><description>NDLRHTTP002 — attribute <c>Name</c> and <c>ClientName</c> property disagree</description></item> |
| | | 21 | | /// <item><description>NDLRHTTP003 — <c>ClientName</c> property body is not a literal expression</description></item> |
| | | 22 | | /// <item><description>NDLRHTTP004 — resolved name is empty</description></item> |
| | | 23 | | /// <item><description>NDLRHTTP005 — duplicate client name across types in the compilation</description></item> |
| | | 24 | | /// <item><description>NDLRHTTP006 — <c>ClientName</c> property has the wrong shape</description></item> |
| | | 25 | | /// </list> |
| | | 26 | | /// </summary> |
| | | 27 | | [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| | | 28 | | public sealed class HttpClientOptionsAnalyzer : DiagnosticAnalyzer |
| | | 29 | | { |
| | | 30 | | public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => |
| | 0 | 31 | | ImmutableArray.Create( |
| | 0 | 32 | | DiagnosticDescriptors.HttpClientMustImplementMarker, |
| | 0 | 33 | | DiagnosticDescriptors.HttpClientNameSourceConflict, |
| | 0 | 34 | | DiagnosticDescriptors.HttpClientNamePropertyNotLiteral, |
| | 0 | 35 | | DiagnosticDescriptors.HttpClientNameEmpty, |
| | 0 | 36 | | DiagnosticDescriptors.HttpClientNameCollision, |
| | 0 | 37 | | DiagnosticDescriptors.HttpClientNamePropertyWrongShape); |
| | | 38 | | |
| | | 39 | | public override void Initialize(AnalysisContext context) |
| | | 40 | | { |
| | 0 | 41 | | context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| | 0 | 42 | | context.EnableConcurrentExecution(); |
| | | 43 | | |
| | | 44 | | // Collision detection needs the full compilation — use a compilation start action so |
| | | 45 | | // we can collect per-type resolved names from concurrent symbol actions and then |
| | | 46 | | // emit collision diagnostics in a compilation-end action. |
| | 0 | 47 | | context.RegisterCompilationStartAction(compilationContext => |
| | 0 | 48 | | { |
| | 0 | 49 | | var nameToTypes = new ConcurrentDictionary<string, List<(INamedTypeSymbol Type, Location Location)>>(); |
| | 0 | 50 | | |
| | 0 | 51 | | compilationContext.RegisterSymbolAction( |
| | 0 | 52 | | symbolContext => AnalyzeNamedType(symbolContext, nameToTypes), |
| | 0 | 53 | | SymbolKind.NamedType); |
| | 0 | 54 | | |
| | 0 | 55 | | compilationContext.RegisterCompilationEndAction(endContext => |
| | 0 | 56 | | { |
| | 0 | 57 | | foreach (var kvp in nameToTypes) |
| | 0 | 58 | | { |
| | 0 | 59 | | if (kvp.Value.Count < 2) |
| | 0 | 60 | | continue; |
| | 0 | 61 | | |
| | 0 | 62 | | // Report the collision on every participant after the first, pointing at |
| | 0 | 63 | | // the prior participant so both ends of the clash surface in the IDE. |
| | 0 | 64 | | var first = kvp.Value[0]; |
| | 0 | 65 | | for (int i = 1; i < kvp.Value.Count; i++) |
| | 0 | 66 | | { |
| | 0 | 67 | | var dup = kvp.Value[i]; |
| | 0 | 68 | | endContext.ReportDiagnostic(Diagnostic.Create( |
| | 0 | 69 | | DiagnosticDescriptors.HttpClientNameCollision, |
| | 0 | 70 | | dup.Location, |
| | 0 | 71 | | dup.Type.Name, |
| | 0 | 72 | | kvp.Key, |
| | 0 | 73 | | first.Type.Name)); |
| | 0 | 74 | | } |
| | 0 | 75 | | } |
| | 0 | 76 | | }); |
| | 0 | 77 | | }); |
| | 0 | 78 | | } |
| | | 79 | | |
| | | 80 | | private static void AnalyzeNamedType( |
| | | 81 | | SymbolAnalysisContext context, |
| | | 82 | | ConcurrentDictionary<string, List<(INamedTypeSymbol Type, Location Location)>> nameToTypes) |
| | | 83 | | { |
| | 0 | 84 | | if (context.Symbol is not INamedTypeSymbol typeSymbol) |
| | 0 | 85 | | return; |
| | | 86 | | |
| | 0 | 87 | | if (!HttpClientOptionsAttributeHelper.HasHttpClientOptionsAttribute(typeSymbol)) |
| | 0 | 88 | | return; |
| | | 89 | | |
| | 0 | 90 | | var attrInfo = HttpClientOptionsAttributeHelper.GetHttpClientOptionsAttribute(typeSymbol); |
| | 0 | 91 | | if (!attrInfo.HasValue) |
| | 0 | 92 | | return; |
| | | 93 | | |
| | 0 | 94 | | var typeLocation = typeSymbol.Locations.Length > 0 ? typeSymbol.Locations[0] : Location.None; |
| | 0 | 95 | | var reportLocation = attrInfo.Value.AttributeLocation ?? typeLocation; |
| | | 96 | | |
| | | 97 | | // NDLRHTTP001: must implement INamedHttpClientOptions |
| | 0 | 98 | | if (!HttpClientOptionsAttributeHelper.ImplementsNamedHttpClientOptions(typeSymbol)) |
| | | 99 | | { |
| | 0 | 100 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 0 | 101 | | DiagnosticDescriptors.HttpClientMustImplementMarker, |
| | 0 | 102 | | reportLocation, |
| | 0 | 103 | | typeSymbol.Name)); |
| | | 104 | | // Keep analyzing — the other diagnostics are still meaningful. |
| | | 105 | | } |
| | | 106 | | |
| | | 107 | | // NDLRHTTP006: ClientName property shape check — runs before the literal extraction so |
| | | 108 | | // a wrong-shape property doesn't silently fall through to type-name inference. |
| | 0 | 109 | | var clientNameSymbol = HttpClientOptionsAttributeHelper.GetClientNamePropertySymbol(typeSymbol); |
| | 0 | 110 | | if (clientNameSymbol is not null) |
| | | 111 | | { |
| | 0 | 112 | | var isValidShape = |
| | 0 | 113 | | clientNameSymbol.Type.SpecialType == SpecialType.System_String && |
| | 0 | 114 | | !clientNameSymbol.IsStatic && |
| | 0 | 115 | | clientNameSymbol.GetMethod is not null; |
| | | 116 | | |
| | 0 | 117 | | if (!isValidShape) |
| | | 118 | | { |
| | 0 | 119 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 0 | 120 | | DiagnosticDescriptors.HttpClientNamePropertyWrongShape, |
| | 0 | 121 | | clientNameSymbol.Locations.Length > 0 ? clientNameSymbol.Locations[0] : reportLocation, |
| | 0 | 122 | | typeSymbol.Name)); |
| | | 123 | | } |
| | | 124 | | } |
| | | 125 | | |
| | | 126 | | // Now resolve the name and check conflict / literal / empty rules. |
| | 0 | 127 | | var propResult = HttpClientOptionsAttributeHelper.TryGetClientNameProperty(typeSymbol, out var literalValue); |
| | 0 | 128 | | var attributeName = attrInfo.Value.Name; |
| | | 129 | | |
| | | 130 | | // NDLRHTTP003: non-literal ClientName without an attribute Name fallback |
| | 0 | 131 | | if (propResult == ClientNamePropertyResult.NonLiteral && string.IsNullOrWhiteSpace(attributeName)) |
| | | 132 | | { |
| | | 133 | | // Only report if the property shape was otherwise valid — NDLRHTTP006 already fired |
| | | 134 | | // for shape issues, and piling NDLRHTTP003 on top would be noise. |
| | 0 | 135 | | if (clientNameSymbol is not null && |
| | 0 | 136 | | clientNameSymbol.Type.SpecialType == SpecialType.System_String && |
| | 0 | 137 | | !clientNameSymbol.IsStatic && |
| | 0 | 138 | | clientNameSymbol.GetMethod is not null) |
| | | 139 | | { |
| | 0 | 140 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 0 | 141 | | DiagnosticDescriptors.HttpClientNamePropertyNotLiteral, |
| | 0 | 142 | | clientNameSymbol.Locations.Length > 0 ? clientNameSymbol.Locations[0] : reportLocation, |
| | 0 | 143 | | typeSymbol.Name)); |
| | | 144 | | } |
| | | 145 | | } |
| | | 146 | | |
| | | 147 | | // NDLRHTTP002: attribute Name and literal ClientName property disagree |
| | 0 | 148 | | if (!string.IsNullOrWhiteSpace(attributeName) && |
| | 0 | 149 | | propResult == ClientNamePropertyResult.Literal && |
| | 0 | 150 | | !string.IsNullOrWhiteSpace(literalValue) && |
| | 0 | 151 | | !string.Equals(attributeName, literalValue, System.StringComparison.Ordinal)) |
| | | 152 | | { |
| | 0 | 153 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 0 | 154 | | DiagnosticDescriptors.HttpClientNameSourceConflict, |
| | 0 | 155 | | reportLocation, |
| | 0 | 156 | | typeSymbol.Name, |
| | 0 | 157 | | attributeName!, |
| | 0 | 158 | | literalValue!)); |
| | | 159 | | } |
| | | 160 | | |
| | | 161 | | // Compute the effective name (attribute wins, then literal property, then inferred). |
| | | 162 | | // This matches HttpClientOptionsAttributeHelper.TryResolveClientName exactly. |
| | 0 | 163 | | var effectiveName = |
| | 0 | 164 | | !string.IsNullOrWhiteSpace(attributeName) ? attributeName : |
| | 0 | 165 | | (propResult == ClientNamePropertyResult.Literal && !string.IsNullOrWhiteSpace(literalValue)) ? literalValue |
| | 0 | 166 | | HttpClientOptionsAttributeHelper.InferClientNameFromTypeName(typeSymbol.Name); |
| | | 167 | | |
| | | 168 | | // NDLRHTTP004: empty resolved name |
| | 0 | 169 | | if (string.IsNullOrWhiteSpace(effectiveName)) |
| | | 170 | | { |
| | 0 | 171 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 0 | 172 | | DiagnosticDescriptors.HttpClientNameEmpty, |
| | 0 | 173 | | reportLocation, |
| | 0 | 174 | | typeSymbol.Name)); |
| | 0 | 175 | | return; |
| | | 176 | | } |
| | | 177 | | |
| | | 178 | | // Record for NDLRHTTP005 collision detection. |
| | 0 | 179 | | nameToTypes.AddOrUpdate( |
| | 0 | 180 | | effectiveName!, |
| | 0 | 181 | | _ => new List<(INamedTypeSymbol, Location)> { (typeSymbol, reportLocation) }, |
| | 0 | 182 | | (_, existing) => |
| | 0 | 183 | | { |
| | 0 | 184 | | lock (existing) |
| | 0 | 185 | | { |
| | 0 | 186 | | existing.Add((typeSymbol, reportLocation)); |
| | 0 | 187 | | return existing; |
| | 0 | 188 | | } |
| | 0 | 189 | | }); |
| | 0 | 190 | | } |
| | | 191 | | } |