| | | 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 [OpenDecoratorFor] attribute usage: |
| | | 13 | | /// - NDLRGEN006: Type argument must be an open generic interface |
| | | 14 | | /// - NDLRGEN007: Decorator class must be an open generic with matching arity |
| | | 15 | | /// - NDLRGEN008: Decorator class must implement the open generic interface |
| | | 16 | | /// </summary> |
| | | 17 | | [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| | | 18 | | public sealed class OpenDecoratorForAttributeAnalyzer : DiagnosticAnalyzer |
| | | 19 | | { |
| | | 20 | | private const string OpenDecoratorForAttributeName = "OpenDecoratorForAttribute"; |
| | | 21 | | private const string GeneratorsNamespace = "NexusLabs.Needlr.Generators"; |
| | | 22 | | |
| | | 23 | | public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => |
| | 256 | 24 | | ImmutableArray.Create( |
| | 256 | 25 | | DiagnosticDescriptors.OpenDecoratorTypeNotOpenGeneric, |
| | 256 | 26 | | DiagnosticDescriptors.OpenDecoratorClassNotOpenGeneric, |
| | 256 | 27 | | DiagnosticDescriptors.OpenDecoratorNotImplementingInterface); |
| | | 28 | | |
| | | 29 | | public override void Initialize(AnalysisContext context) |
| | | 30 | | { |
| | 23 | 31 | | context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| | 23 | 32 | | context.EnableConcurrentExecution(); |
| | | 33 | | |
| | 23 | 34 | | context.RegisterSyntaxNodeAction(AnalyzeAttribute, SyntaxKind.Attribute); |
| | 23 | 35 | | } |
| | | 36 | | |
| | | 37 | | private static void AnalyzeAttribute(SyntaxNodeAnalysisContext context) |
| | | 38 | | { |
| | 169 | 39 | | var attributeSyntax = (AttributeSyntax)context.Node; |
| | 169 | 40 | | var attributeSymbol = context.SemanticModel.GetSymbolInfo(attributeSyntax).Symbol?.ContainingType; |
| | | 41 | | |
| | 169 | 42 | | if (attributeSymbol == null) |
| | 0 | 43 | | return; |
| | | 44 | | |
| | | 45 | | // Check if this is an [OpenDecoratorFor] attribute |
| | 169 | 46 | | if (!IsOpenDecoratorForAttribute(attributeSymbol)) |
| | 154 | 47 | | return; |
| | | 48 | | |
| | | 49 | | // Get the class this attribute is applied to |
| | 15 | 50 | | var classDeclaration = attributeSyntax.Parent?.Parent as ClassDeclarationSyntax; |
| | 15 | 51 | | if (classDeclaration == null) |
| | 0 | 52 | | return; |
| | | 53 | | |
| | 15 | 54 | | var classSymbol = context.SemanticModel.GetDeclaredSymbol(classDeclaration); |
| | 15 | 55 | | if (classSymbol == null) |
| | 0 | 56 | | return; |
| | | 57 | | |
| | | 58 | | // Get the type argument from the attribute constructor |
| | 15 | 59 | | var typeArg = GetTypeArgumentFromAttribute(attributeSyntax, context.SemanticModel); |
| | 15 | 60 | | if (typeArg == null) |
| | 0 | 61 | | return; |
| | | 62 | | |
| | | 63 | | // NDLRGEN006: Validate type argument is an open generic interface |
| | 15 | 64 | | if (!IsOpenGenericInterface(typeArg, out string typeDescription)) |
| | | 65 | | { |
| | 4 | 66 | | var diagnostic = Diagnostic.Create( |
| | 4 | 67 | | DiagnosticDescriptors.OpenDecoratorTypeNotOpenGeneric, |
| | 4 | 68 | | attributeSyntax.GetLocation(), |
| | 4 | 69 | | typeArg.ToDisplayString(), |
| | 4 | 70 | | typeDescription); |
| | | 71 | | |
| | 4 | 72 | | context.ReportDiagnostic(diagnostic); |
| | 4 | 73 | | return; // Don't check other rules if this fails |
| | | 74 | | } |
| | | 75 | | |
| | 11 | 76 | | var openGenericInterface = typeArg as INamedTypeSymbol; |
| | 11 | 77 | | if (openGenericInterface == null) |
| | 0 | 78 | | return; |
| | | 79 | | |
| | 11 | 80 | | int expectedTypeParamCount = openGenericInterface.TypeParameters.Length; |
| | | 81 | | |
| | | 82 | | // NDLRGEN007: Validate decorator class is an open generic with matching arity |
| | 11 | 83 | | if (!classSymbol.IsGenericType || classSymbol.TypeParameters.Length != expectedTypeParamCount) |
| | | 84 | | { |
| | 4 | 85 | | var diagnostic = Diagnostic.Create( |
| | 4 | 86 | | DiagnosticDescriptors.OpenDecoratorClassNotOpenGeneric, |
| | 4 | 87 | | attributeSyntax.GetLocation(), |
| | 4 | 88 | | classSymbol.Name, |
| | 4 | 89 | | openGenericInterface.ToDisplayString(), |
| | 4 | 90 | | expectedTypeParamCount); |
| | | 91 | | |
| | 4 | 92 | | context.ReportDiagnostic(diagnostic); |
| | 4 | 93 | | return; // Don't check implementation if arity doesn't match |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | // NDLRGEN008: Validate decorator implements the open generic interface |
| | 7 | 97 | | if (!ImplementsOpenGenericInterface(classSymbol, openGenericInterface)) |
| | | 98 | | { |
| | 2 | 99 | | var diagnostic = Diagnostic.Create( |
| | 2 | 100 | | DiagnosticDescriptors.OpenDecoratorNotImplementingInterface, |
| | 2 | 101 | | attributeSyntax.GetLocation(), |
| | 2 | 102 | | classSymbol.Name, |
| | 2 | 103 | | openGenericInterface.ToDisplayString()); |
| | | 104 | | |
| | 2 | 105 | | context.ReportDiagnostic(diagnostic); |
| | | 106 | | } |
| | 7 | 107 | | } |
| | | 108 | | |
| | | 109 | | private static bool IsOpenDecoratorForAttribute(INamedTypeSymbol attributeSymbol) |
| | | 110 | | { |
| | 169 | 111 | | if (attributeSymbol.Name != OpenDecoratorForAttributeName) |
| | 154 | 112 | | return false; |
| | | 113 | | |
| | 15 | 114 | | var ns = attributeSymbol.ContainingNamespace?.ToString(); |
| | 15 | 115 | | return ns == GeneratorsNamespace; |
| | | 116 | | } |
| | | 117 | | |
| | | 118 | | private static ITypeSymbol? GetTypeArgumentFromAttribute( |
| | | 119 | | AttributeSyntax attributeSyntax, |
| | | 120 | | SemanticModel semanticModel) |
| | | 121 | | { |
| | | 122 | | // The attribute has a constructor: OpenDecoratorForAttribute(Type openGenericServiceType) |
| | | 123 | | // We need to get the typeof() argument |
| | 15 | 124 | | var argumentList = attributeSyntax.ArgumentList; |
| | 15 | 125 | | if (argumentList == null || argumentList.Arguments.Count == 0) |
| | 0 | 126 | | return null; |
| | | 127 | | |
| | 15 | 128 | | var firstArg = argumentList.Arguments[0]; |
| | 15 | 129 | | var expression = firstArg.Expression; |
| | | 130 | | |
| | | 131 | | // Handle typeof(IHandler<>) |
| | 15 | 132 | | if (expression is TypeOfExpressionSyntax typeOfExpr) |
| | | 133 | | { |
| | 15 | 134 | | var typeInfo = semanticModel.GetTypeInfo(typeOfExpr.Type); |
| | 15 | 135 | | return typeInfo.Type; |
| | | 136 | | } |
| | | 137 | | |
| | 0 | 138 | | return null; |
| | | 139 | | } |
| | | 140 | | |
| | | 141 | | private static bool IsOpenGenericInterface(ITypeSymbol? typeSymbol, out string typeDescription) |
| | | 142 | | { |
| | 15 | 143 | | if (typeSymbol == null) |
| | | 144 | | { |
| | 0 | 145 | | typeDescription = "null"; |
| | 0 | 146 | | return false; |
| | | 147 | | } |
| | | 148 | | |
| | 15 | 149 | | if (typeSymbol is not INamedTypeSymbol namedType) |
| | | 150 | | { |
| | 0 | 151 | | typeDescription = $"{typeSymbol.TypeKind}"; |
| | 0 | 152 | | return false; |
| | | 153 | | } |
| | | 154 | | |
| | 15 | 155 | | if (namedType.TypeKind != TypeKind.Interface) |
| | | 156 | | { |
| | 2 | 157 | | typeDescription = $"{namedType.TypeKind} (not an interface)"; |
| | 2 | 158 | | return false; |
| | | 159 | | } |
| | | 160 | | |
| | 13 | 161 | | if (!namedType.IsGenericType) |
| | | 162 | | { |
| | 2 | 163 | | typeDescription = "non-generic interface"; |
| | 2 | 164 | | return false; |
| | | 165 | | } |
| | | 166 | | |
| | | 167 | | // Check if it's an unbound/open generic (has unsubstituted type parameters) |
| | 11 | 168 | | if (!namedType.IsUnboundGenericType && |
| | 11 | 169 | | !namedType.TypeArguments.All(t => t.TypeKind == TypeKind.TypeParameter)) |
| | | 170 | | { |
| | 0 | 171 | | typeDescription = "closed generic interface (use typeof(IInterface<>) not typeof(IInterface<T>))"; |
| | 0 | 172 | | return false; |
| | | 173 | | } |
| | | 174 | | |
| | 11 | 175 | | typeDescription = "open generic interface"; |
| | 11 | 176 | | return true; |
| | | 177 | | } |
| | | 178 | | |
| | | 179 | | private static bool ImplementsOpenGenericInterface( |
| | | 180 | | INamedTypeSymbol classSymbol, |
| | | 181 | | INamedTypeSymbol openGenericInterface) |
| | | 182 | | { |
| | | 183 | | // Check if the class implements a constructed version of the open generic interface |
| | | 184 | | // e.g., LoggingDecorator<T> : IHandler<T> should match IHandler<> |
| | 7 | 185 | | var unboundInterface = openGenericInterface.IsUnboundGenericType |
| | 7 | 186 | | ? openGenericInterface |
| | 7 | 187 | | : openGenericInterface.ConstructUnboundGenericType(); |
| | | 188 | | |
| | 23 | 189 | | foreach (var iface in classSymbol.AllInterfaces) |
| | | 190 | | { |
| | 7 | 191 | | if (!iface.IsGenericType) |
| | | 192 | | continue; |
| | | 193 | | |
| | 7 | 194 | | var unboundImplemented = iface.ConstructUnboundGenericType(); |
| | 7 | 195 | | if (SymbolEqualityComparer.Default.Equals(unboundImplemented, unboundInterface)) |
| | 5 | 196 | | return true; |
| | | 197 | | } |
| | | 198 | | |
| | 2 | 199 | | return false; |
| | | 200 | | } |
| | | 201 | | } |