| | | 1 | | using System.Collections.Immutable; |
| | | 2 | | using System.Linq; |
| | | 3 | | |
| | | 4 | | using Microsoft.CodeAnalysis; |
| | | 5 | | using Microsoft.CodeAnalysis.CSharp; |
| | | 6 | | using Microsoft.CodeAnalysis.CSharp.Syntax; |
| | | 7 | | using Microsoft.CodeAnalysis.Diagnostics; |
| | | 8 | | |
| | | 9 | | using NexusLabs.Needlr.Roslyn.Shared; |
| | | 10 | | |
| | | 11 | | namespace NexusLabs.Needlr.Generators; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Analyzer that validates [GenerateFactory] and [GenerateFactory<T>] attribute usage: |
| | | 15 | | /// - NDLRGEN003: All constructor parameters are injectable (factory unnecessary) |
| | | 16 | | /// - NDLRGEN004: No constructor parameters are injectable (low value factory) |
| | | 17 | | /// - NDLRGEN005: Type argument T is not an interface implemented by the class |
| | | 18 | | /// </summary> |
| | | 19 | | [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| | | 20 | | public sealed class GenerateFactoryAttributeAnalyzer : DiagnosticAnalyzer |
| | | 21 | | { |
| | | 22 | | private const string GenerateFactoryAttributeName = "GenerateFactoryAttribute"; |
| | | 23 | | private const string GeneratorsNamespace = "NexusLabs.Needlr.Generators"; |
| | | 24 | | |
| | | 25 | | public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => |
| | 308 | 26 | | ImmutableArray.Create( |
| | 308 | 27 | | DiagnosticDescriptors.FactoryAllParamsInjectable, |
| | 308 | 28 | | DiagnosticDescriptors.FactoryNoInjectableParams, |
| | 308 | 29 | | DiagnosticDescriptors.FactoryTypeArgNotImplemented); |
| | | 30 | | |
| | | 31 | | public override void Initialize(AnalysisContext context) |
| | | 32 | | { |
| | 28 | 33 | | context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| | 28 | 34 | | context.EnableConcurrentExecution(); |
| | | 35 | | |
| | 28 | 36 | | context.RegisterSyntaxNodeAction(AnalyzeAttribute, SyntaxKind.Attribute); |
| | 28 | 37 | | } |
| | | 38 | | |
| | | 39 | | private static void AnalyzeAttribute(SyntaxNodeAnalysisContext context) |
| | | 40 | | { |
| | 268 | 41 | | var attributeSyntax = (AttributeSyntax)context.Node; |
| | 268 | 42 | | var attributeSymbol = context.SemanticModel.GetSymbolInfo(attributeSyntax).Symbol?.ContainingType; |
| | | 43 | | |
| | 268 | 44 | | if (attributeSymbol == null) |
| | 0 | 45 | | return; |
| | | 46 | | |
| | | 47 | | // Check if this is a [GenerateFactory] or [GenerateFactory<T>] attribute |
| | 268 | 48 | | if (!IsGenerateFactoryAttribute(attributeSymbol)) |
| | 251 | 49 | | return; |
| | | 50 | | |
| | | 51 | | // Get the class this attribute is applied to |
| | 17 | 52 | | var classDeclaration = attributeSyntax.Parent?.Parent as ClassDeclarationSyntax; |
| | 17 | 53 | | if (classDeclaration == null) |
| | 0 | 54 | | return; |
| | | 55 | | |
| | 17 | 56 | | var classSymbol = context.SemanticModel.GetDeclaredSymbol(classDeclaration); |
| | 17 | 57 | | if (classSymbol == null) |
| | 0 | 58 | | return; |
| | | 59 | | |
| | | 60 | | // NDLRGEN005: Check if generic type argument is implemented by the class |
| | 17 | 61 | | if (attributeSymbol.IsGenericType && attributeSymbol.TypeArguments.Length == 1) |
| | | 62 | | { |
| | 4 | 63 | | var typeArg = attributeSymbol.TypeArguments[0] as INamedTypeSymbol; |
| | 4 | 64 | | if (typeArg != null) |
| | | 65 | | { |
| | | 66 | | // Check if the class implements this interface |
| | 4 | 67 | | bool implementsInterface = classSymbol.AllInterfaces.Any(i => |
| | 9 | 68 | | SymbolEqualityComparer.Default.Equals(i, typeArg)); |
| | | 69 | | |
| | 4 | 70 | | if (!implementsInterface) |
| | | 71 | | { |
| | 2 | 72 | | var diagnostic = Diagnostic.Create( |
| | 2 | 73 | | DiagnosticDescriptors.FactoryTypeArgNotImplemented, |
| | 2 | 74 | | attributeSyntax.GetLocation(), |
| | 2 | 75 | | classSymbol.Name, |
| | 2 | 76 | | typeArg.Name); |
| | | 77 | | |
| | 2 | 78 | | context.ReportDiagnostic(diagnostic); |
| | | 79 | | } |
| | | 80 | | } |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | // Analyze constructor parameters for NDLRGEN003 and NDLRGEN004 |
| | 17 | 84 | | AnalyzeConstructorParameters(context, attributeSyntax, classSymbol); |
| | 17 | 85 | | } |
| | | 86 | | |
| | | 87 | | private static void AnalyzeConstructorParameters( |
| | | 88 | | SyntaxNodeAnalysisContext context, |
| | | 89 | | AttributeSyntax attributeSyntax, |
| | | 90 | | INamedTypeSymbol classSymbol) |
| | | 91 | | { |
| | 17 | 92 | | var parameterTypes = TryGetGeneratedConstructorParameterTypes(classSymbol) ?? GetBestExplicitConstructorParamete |
| | 17 | 93 | | if (parameterTypes is null) |
| | 0 | 94 | | return; |
| | | 95 | | |
| | 17 | 96 | | if (parameterTypes.Count == 0) |
| | | 97 | | { |
| | | 98 | | // No parameters at all - factory is pointless |
| | 2 | 99 | | var diagnostic = Diagnostic.Create( |
| | 2 | 100 | | DiagnosticDescriptors.FactoryNoInjectableParams, |
| | 2 | 101 | | attributeSyntax.GetLocation(), |
| | 2 | 102 | | classSymbol.Name); |
| | | 103 | | |
| | 2 | 104 | | context.ReportDiagnostic(diagnostic); |
| | 2 | 105 | | return; |
| | | 106 | | } |
| | | 107 | | |
| | 15 | 108 | | int injectableCount = 0; |
| | 15 | 109 | | int runtimeCount = 0; |
| | | 110 | | |
| | 92 | 111 | | foreach (var parameterType in parameterTypes) |
| | | 112 | | { |
| | 31 | 113 | | if (IsInjectableParameterType(parameterType)) |
| | | 114 | | { |
| | 15 | 115 | | injectableCount++; |
| | | 116 | | } |
| | | 117 | | else |
| | | 118 | | { |
| | 16 | 119 | | runtimeCount++; |
| | | 120 | | } |
| | | 121 | | } |
| | | 122 | | |
| | | 123 | | // NDLRGEN003: All params are injectable - factory unnecessary |
| | 15 | 124 | | if (runtimeCount == 0) |
| | | 125 | | { |
| | 4 | 126 | | var diagnostic = Diagnostic.Create( |
| | 4 | 127 | | DiagnosticDescriptors.FactoryAllParamsInjectable, |
| | 4 | 128 | | attributeSyntax.GetLocation(), |
| | 4 | 129 | | classSymbol.Name); |
| | | 130 | | |
| | 4 | 131 | | context.ReportDiagnostic(diagnostic); |
| | | 132 | | } |
| | | 133 | | // NDLRGEN004: No params are injectable - low value factory |
| | 11 | 134 | | else if (injectableCount == 0) |
| | | 135 | | { |
| | 4 | 136 | | var diagnostic = Diagnostic.Create( |
| | 4 | 137 | | DiagnosticDescriptors.FactoryNoInjectableParams, |
| | 4 | 138 | | attributeSyntax.GetLocation(), |
| | 4 | 139 | | classSymbol.Name); |
| | | 140 | | |
| | 4 | 141 | | context.ReportDiagnostic(diagnostic); |
| | | 142 | | } |
| | 11 | 143 | | } |
| | | 144 | | |
| | | 145 | | /// <summary> |
| | | 146 | | /// Returns the effective constructor parameter types for a type eligible for |
| | | 147 | | /// generated-constructor generation (<c>[GenerateConstructor]</c> or a positive |
| | | 148 | | /// field-level constructor guard trigger), derived from the same shared eligible-field |
| | | 149 | | /// model the source generator and type-registry discovery use, or |
| | | 150 | | /// <see langword="null"/> when the type is not eligible. Such a type's effective |
| | | 151 | | /// constructor is emitted by a sibling generator pass rather than authored in its own |
| | | 152 | | /// syntax tree, so runtime-vs-injectable classification must use this field-derived |
| | | 153 | | /// parameter list instead of the type's (implicit, parameterless) symbol-visible |
| | | 154 | | /// constructor. |
| | | 155 | | /// </summary> |
| | | 156 | | private static IReadOnlyList<ITypeSymbol>? TryGetGeneratedConstructorParameterTypes(INamedTypeSymbol classSymbol) |
| | | 157 | | { |
| | 17 | 158 | | if (!GeneratedConstructorEligibility.IsEligibleForGeneratedConstructor(classSymbol)) |
| | 12 | 159 | | return null; |
| | | 160 | | |
| | 5 | 161 | | return GeneratedConstructorEligibility.GetEligibleConstructorFields(classSymbol) |
| | 10 | 162 | | .Select(f => f.Type) |
| | 5 | 163 | | .ToList(); |
| | | 164 | | } |
| | | 165 | | |
| | | 166 | | /// <summary> |
| | | 167 | | /// Returns the parameter types of the best (public, most-parameters) explicitly |
| | | 168 | | /// authored instance constructor, or <see langword="null"/> when the type declares no |
| | | 169 | | /// public instance constructor at all. |
| | | 170 | | /// </summary> |
| | | 171 | | private static IReadOnlyList<ITypeSymbol>? GetBestExplicitConstructorParameterTypes(INamedTypeSymbol classSymbol) |
| | | 172 | | { |
| | 12 | 173 | | var publicCtors = classSymbol.InstanceConstructors |
| | 12 | 174 | | .Where(c => c.DeclaredAccessibility == Accessibility.Public && !c.IsStatic) |
| | 0 | 175 | | .OrderByDescending(c => c.Parameters.Length) |
| | 12 | 176 | | .ToList(); |
| | | 177 | | |
| | 12 | 178 | | if (publicCtors.Count == 0) |
| | 0 | 179 | | return null; |
| | | 180 | | |
| | 33 | 181 | | return publicCtors[0].Parameters.Select(p => p.Type).ToList(); |
| | | 182 | | } |
| | | 183 | | |
| | | 184 | | private static bool IsGenerateFactoryAttribute(INamedTypeSymbol attributeSymbol) |
| | | 185 | | { |
| | | 186 | | // Handle both GenerateFactoryAttribute and GenerateFactoryAttribute<T> |
| | 268 | 187 | | var name = attributeSymbol.Name; |
| | 268 | 188 | | if (name != GenerateFactoryAttributeName) |
| | | 189 | | { |
| | | 190 | | // Check original definition for generic case |
| | 251 | 191 | | if (attributeSymbol.IsGenericType) |
| | | 192 | | { |
| | 0 | 193 | | name = attributeSymbol.OriginalDefinition.Name; |
| | 0 | 194 | | if (name != GenerateFactoryAttributeName) |
| | 0 | 195 | | return false; |
| | | 196 | | } |
| | | 197 | | else |
| | | 198 | | { |
| | 251 | 199 | | return false; |
| | | 200 | | } |
| | | 201 | | } |
| | | 202 | | |
| | 17 | 203 | | var ns = attributeSymbol.ContainingNamespace?.ToString(); |
| | 17 | 204 | | return ns == GeneratorsNamespace; |
| | | 205 | | } |
| | | 206 | | |
| | | 207 | | private static bool IsInjectableParameterType(ITypeSymbol typeSymbol) |
| | | 208 | | { |
| | | 209 | | // Value types (int, string, DateTime, Guid, etc.) are NOT injectable |
| | 31 | 210 | | if (typeSymbol.IsValueType) |
| | 5 | 211 | | return false; |
| | | 212 | | |
| | | 213 | | // String is special - it's a reference type but not injectable |
| | 26 | 214 | | if (typeSymbol.SpecialType == SpecialType.System_String) |
| | 11 | 215 | | return false; |
| | | 216 | | |
| | | 217 | | // Delegates are not injectable |
| | 15 | 218 | | if (typeSymbol.TypeKind == TypeKind.Delegate) |
| | 0 | 219 | | return false; |
| | | 220 | | |
| | | 221 | | // Arrays are typically not injectable by DI |
| | 15 | 222 | | if (typeSymbol.TypeKind == TypeKind.Array) |
| | 0 | 223 | | return false; |
| | | 224 | | |
| | | 225 | | // Interfaces and classes are injectable |
| | 15 | 226 | | if (typeSymbol.TypeKind == TypeKind.Interface || typeSymbol.TypeKind == TypeKind.Class) |
| | 15 | 227 | | return true; |
| | | 228 | | |
| | 0 | 229 | | return false; |
| | | 230 | | } |
| | | 231 | | } |