| | | 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 | | namespace NexusLabs.Needlr.Generators; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Analyzer that validates [GenerateFactory] and [GenerateFactory<T>] attribute usage: |
| | | 13 | | /// - NDLRGEN003: All constructor parameters are injectable (factory unnecessary) |
| | | 14 | | /// - NDLRGEN004: No constructor parameters are injectable (low value factory) |
| | | 15 | | /// - NDLRGEN005: Type argument T is not an interface implemented by the class |
| | | 16 | | /// </summary> |
| | | 17 | | [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| | | 18 | | public sealed class GenerateFactoryAttributeAnalyzer : DiagnosticAnalyzer |
| | | 19 | | { |
| | | 20 | | private const string GenerateFactoryAttributeName = "GenerateFactoryAttribute"; |
| | | 21 | | private const string GeneratorsNamespace = "NexusLabs.Needlr.Generators"; |
| | | 22 | | |
| | | 23 | | public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => |
| | 208 | 24 | | ImmutableArray.Create( |
| | 208 | 25 | | DiagnosticDescriptors.FactoryAllParamsInjectable, |
| | 208 | 26 | | DiagnosticDescriptors.FactoryNoInjectableParams, |
| | 208 | 27 | | DiagnosticDescriptors.FactoryTypeArgNotImplemented); |
| | | 28 | | |
| | | 29 | | public override void Initialize(AnalysisContext context) |
| | | 30 | | { |
| | 20 | 31 | | context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| | 20 | 32 | | context.EnableConcurrentExecution(); |
| | | 33 | | |
| | 20 | 34 | | context.RegisterSyntaxNodeAction(AnalyzeAttribute, SyntaxKind.Attribute); |
| | 20 | 35 | | } |
| | | 36 | | |
| | | 37 | | private static void AnalyzeAttribute(SyntaxNodeAnalysisContext context) |
| | | 38 | | { |
| | 168 | 39 | | var attributeSyntax = (AttributeSyntax)context.Node; |
| | 168 | 40 | | var attributeSymbol = context.SemanticModel.GetSymbolInfo(attributeSyntax).Symbol?.ContainingType; |
| | | 41 | | |
| | 168 | 42 | | if (attributeSymbol == null) |
| | 0 | 43 | | return; |
| | | 44 | | |
| | | 45 | | // Check if this is a [GenerateFactory] or [GenerateFactory<T>] attribute |
| | 168 | 46 | | if (!IsGenerateFactoryAttribute(attributeSymbol)) |
| | 156 | 47 | | return; |
| | | 48 | | |
| | | 49 | | // Get the class this attribute is applied to |
| | 12 | 50 | | var classDeclaration = attributeSyntax.Parent?.Parent as ClassDeclarationSyntax; |
| | 12 | 51 | | if (classDeclaration == null) |
| | 0 | 52 | | return; |
| | | 53 | | |
| | 12 | 54 | | var classSymbol = context.SemanticModel.GetDeclaredSymbol(classDeclaration); |
| | 12 | 55 | | if (classSymbol == null) |
| | 0 | 56 | | return; |
| | | 57 | | |
| | | 58 | | // NDLRGEN005: Check if generic type argument is implemented by the class |
| | 12 | 59 | | if (attributeSymbol.IsGenericType && attributeSymbol.TypeArguments.Length == 1) |
| | | 60 | | { |
| | 4 | 61 | | var typeArg = attributeSymbol.TypeArguments[0] as INamedTypeSymbol; |
| | 4 | 62 | | if (typeArg != null) |
| | | 63 | | { |
| | | 64 | | // Check if the class implements this interface |
| | 4 | 65 | | bool implementsInterface = classSymbol.AllInterfaces.Any(i => |
| | 9 | 66 | | SymbolEqualityComparer.Default.Equals(i, typeArg)); |
| | | 67 | | |
| | 4 | 68 | | if (!implementsInterface) |
| | | 69 | | { |
| | 2 | 70 | | var diagnostic = Diagnostic.Create( |
| | 2 | 71 | | DiagnosticDescriptors.FactoryTypeArgNotImplemented, |
| | 2 | 72 | | attributeSyntax.GetLocation(), |
| | 2 | 73 | | classSymbol.Name, |
| | 2 | 74 | | typeArg.Name); |
| | | 75 | | |
| | 2 | 76 | | context.ReportDiagnostic(diagnostic); |
| | | 77 | | } |
| | | 78 | | } |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | // Analyze constructor parameters for NDLRGEN003 and NDLRGEN004 |
| | 12 | 82 | | AnalyzeConstructorParameters(context, attributeSyntax, classSymbol); |
| | 12 | 83 | | } |
| | | 84 | | |
| | | 85 | | private static void AnalyzeConstructorParameters( |
| | | 86 | | SyntaxNodeAnalysisContext context, |
| | | 87 | | AttributeSyntax attributeSyntax, |
| | | 88 | | INamedTypeSymbol classSymbol) |
| | | 89 | | { |
| | | 90 | | // Find the best constructor (public, most parameters) |
| | 12 | 91 | | var publicCtors = classSymbol.InstanceConstructors |
| | 12 | 92 | | .Where(c => c.DeclaredAccessibility == Accessibility.Public && !c.IsStatic) |
| | 0 | 93 | | .OrderByDescending(c => c.Parameters.Length) |
| | 12 | 94 | | .ToList(); |
| | | 95 | | |
| | 12 | 96 | | if (publicCtors.Count == 0) |
| | 0 | 97 | | return; |
| | | 98 | | |
| | | 99 | | // Check all constructors - we care about the "best" one for the warning |
| | 12 | 100 | | var bestCtor = publicCtors[0]; |
| | 12 | 101 | | var parameters = bestCtor.Parameters; |
| | | 102 | | |
| | 12 | 103 | | if (parameters.Length == 0) |
| | | 104 | | { |
| | | 105 | | // No parameters at all - factory is pointless |
| | 2 | 106 | | var diagnostic = Diagnostic.Create( |
| | 2 | 107 | | DiagnosticDescriptors.FactoryNoInjectableParams, |
| | 2 | 108 | | attributeSyntax.GetLocation(), |
| | 2 | 109 | | classSymbol.Name); |
| | | 110 | | |
| | 2 | 111 | | context.ReportDiagnostic(diagnostic); |
| | 2 | 112 | | return; |
| | | 113 | | } |
| | | 114 | | |
| | 10 | 115 | | int injectableCount = 0; |
| | 10 | 116 | | int runtimeCount = 0; |
| | | 117 | | |
| | 62 | 118 | | foreach (var param in parameters) |
| | | 119 | | { |
| | 21 | 120 | | if (IsInjectableParameterType(param.Type)) |
| | | 121 | | { |
| | 10 | 122 | | injectableCount++; |
| | | 123 | | } |
| | | 124 | | else |
| | | 125 | | { |
| | 11 | 126 | | runtimeCount++; |
| | | 127 | | } |
| | | 128 | | } |
| | | 129 | | |
| | | 130 | | // NDLRGEN003: All params are injectable - factory unnecessary |
| | 10 | 131 | | if (runtimeCount == 0) |
| | | 132 | | { |
| | 2 | 133 | | var diagnostic = Diagnostic.Create( |
| | 2 | 134 | | DiagnosticDescriptors.FactoryAllParamsInjectable, |
| | 2 | 135 | | attributeSyntax.GetLocation(), |
| | 2 | 136 | | classSymbol.Name); |
| | | 137 | | |
| | 2 | 138 | | context.ReportDiagnostic(diagnostic); |
| | | 139 | | } |
| | | 140 | | // NDLRGEN004: No params are injectable - low value factory |
| | 8 | 141 | | else if (injectableCount == 0) |
| | | 142 | | { |
| | 2 | 143 | | var diagnostic = Diagnostic.Create( |
| | 2 | 144 | | DiagnosticDescriptors.FactoryNoInjectableParams, |
| | 2 | 145 | | attributeSyntax.GetLocation(), |
| | 2 | 146 | | classSymbol.Name); |
| | | 147 | | |
| | 2 | 148 | | context.ReportDiagnostic(diagnostic); |
| | | 149 | | } |
| | 8 | 150 | | } |
| | | 151 | | |
| | | 152 | | private static bool IsGenerateFactoryAttribute(INamedTypeSymbol attributeSymbol) |
| | | 153 | | { |
| | | 154 | | // Handle both GenerateFactoryAttribute and GenerateFactoryAttribute<T> |
| | 168 | 155 | | var name = attributeSymbol.Name; |
| | 168 | 156 | | if (name != GenerateFactoryAttributeName) |
| | | 157 | | { |
| | | 158 | | // Check original definition for generic case |
| | 156 | 159 | | if (attributeSymbol.IsGenericType) |
| | | 160 | | { |
| | 0 | 161 | | name = attributeSymbol.OriginalDefinition.Name; |
| | 0 | 162 | | if (name != GenerateFactoryAttributeName) |
| | 0 | 163 | | return false; |
| | | 164 | | } |
| | | 165 | | else |
| | | 166 | | { |
| | 156 | 167 | | return false; |
| | | 168 | | } |
| | | 169 | | } |
| | | 170 | | |
| | 12 | 171 | | var ns = attributeSymbol.ContainingNamespace?.ToString(); |
| | 12 | 172 | | return ns == GeneratorsNamespace; |
| | | 173 | | } |
| | | 174 | | |
| | | 175 | | private static bool IsInjectableParameterType(ITypeSymbol typeSymbol) |
| | | 176 | | { |
| | | 177 | | // Value types (int, string, DateTime, Guid, etc.) are NOT injectable |
| | 21 | 178 | | if (typeSymbol.IsValueType) |
| | 3 | 179 | | return false; |
| | | 180 | | |
| | | 181 | | // String is special - it's a reference type but not injectable |
| | 18 | 182 | | if (typeSymbol.SpecialType == SpecialType.System_String) |
| | 8 | 183 | | return false; |
| | | 184 | | |
| | | 185 | | // Delegates are not injectable |
| | 10 | 186 | | if (typeSymbol.TypeKind == TypeKind.Delegate) |
| | 0 | 187 | | return false; |
| | | 188 | | |
| | | 189 | | // Arrays are typically not injectable by DI |
| | 10 | 190 | | if (typeSymbol.TypeKind == TypeKind.Array) |
| | 0 | 191 | | return false; |
| | | 192 | | |
| | | 193 | | // Interfaces and classes are injectable |
| | 10 | 194 | | if (typeSymbol.TypeKind == TypeKind.Interface || typeSymbol.TypeKind == TypeKind.Class) |
| | 10 | 195 | | return true; |
| | | 196 | | |
| | 0 | 197 | | return false; |
| | | 198 | | } |
| | | 199 | | } |