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