| | | 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 [Provider] attribute usage: |
| | | 13 | | /// - NDLRGEN031: [Provider] on class requires `partial` modifier |
| | | 14 | | /// - NDLRGEN032: [Provider] interface must only contain get-only properties |
| | | 15 | | /// - NDLRGEN033: Provider property type is a concrete class |
| | | 16 | | /// - NDLRGEN034: Circular provider dependency detected |
| | | 17 | | /// </summary> |
| | | 18 | | [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| | | 19 | | public sealed class ProviderAttributeAnalyzer : DiagnosticAnalyzer |
| | | 20 | | { |
| | | 21 | | private const string ProviderAttributeName = "ProviderAttribute"; |
| | | 22 | | private const string GeneratorsNamespace = "NexusLabs.Needlr.Generators"; |
| | | 23 | | |
| | | 24 | | public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => |
| | 212 | 25 | | ImmutableArray.Create( |
| | 212 | 26 | | DiagnosticDescriptors.ProviderClassNotPartial, |
| | 212 | 27 | | DiagnosticDescriptors.ProviderInterfaceInvalidMember, |
| | 212 | 28 | | DiagnosticDescriptors.ProviderPropertyConcreteType, |
| | 212 | 29 | | DiagnosticDescriptors.ProviderCircularDependency); |
| | | 30 | | |
| | | 31 | | public override void Initialize(AnalysisContext context) |
| | | 32 | | { |
| | 22 | 33 | | context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| | 22 | 34 | | context.EnableConcurrentExecution(); |
| | | 35 | | |
| | 22 | 36 | | context.RegisterSyntaxNodeAction(AnalyzeAttribute, SyntaxKind.Attribute); |
| | 22 | 37 | | } |
| | | 38 | | |
| | | 39 | | private static void AnalyzeAttribute(SyntaxNodeAnalysisContext context) |
| | | 40 | | { |
| | 156 | 41 | | var attributeSyntax = (AttributeSyntax)context.Node; |
| | 156 | 42 | | var attributeSymbol = context.SemanticModel.GetSymbolInfo(attributeSyntax).Symbol?.ContainingType; |
| | | 43 | | |
| | 156 | 44 | | if (attributeSymbol == null) |
| | 0 | 45 | | return; |
| | | 46 | | |
| | 156 | 47 | | if (!IsProviderAttribute(attributeSymbol)) |
| | 143 | 48 | | return; |
| | | 49 | | |
| | 13 | 50 | | var parent = attributeSyntax.Parent?.Parent; |
| | | 51 | | |
| | 13 | 52 | | if (parent is ClassDeclarationSyntax classDeclaration) |
| | | 53 | | { |
| | 3 | 54 | | AnalyzeProviderClass(context, classDeclaration, attributeSyntax); |
| | | 55 | | } |
| | 10 | 56 | | else if (parent is InterfaceDeclarationSyntax interfaceDeclaration) |
| | | 57 | | { |
| | 10 | 58 | | AnalyzeProviderInterface(context, interfaceDeclaration, attributeSyntax); |
| | | 59 | | } |
| | 10 | 60 | | } |
| | | 61 | | |
| | | 62 | | private static void AnalyzeProviderClass( |
| | | 63 | | SyntaxNodeAnalysisContext context, |
| | | 64 | | ClassDeclarationSyntax classDeclaration, |
| | | 65 | | AttributeSyntax attributeSyntax) |
| | | 66 | | { |
| | | 67 | | // NDLRGEN031: Check for partial modifier |
| | 7 | 68 | | var isPartial = classDeclaration.Modifiers.Any(m => m.IsKind(SyntaxKind.PartialKeyword)); |
| | 3 | 69 | | if (!isPartial) |
| | | 70 | | { |
| | 2 | 71 | | var classSymbol = context.SemanticModel.GetDeclaredSymbol(classDeclaration); |
| | 2 | 72 | | if (classSymbol != null) |
| | | 73 | | { |
| | 2 | 74 | | context.ReportDiagnostic( |
| | 2 | 75 | | Diagnostic.Create( |
| | 2 | 76 | | DiagnosticDescriptors.ProviderClassNotPartial, |
| | 2 | 77 | | attributeSyntax.GetLocation(), |
| | 2 | 78 | | classSymbol.Name)); |
| | | 79 | | } |
| | | 80 | | } |
| | 3 | 81 | | } |
| | | 82 | | |
| | | 83 | | private static void AnalyzeProviderInterface( |
| | | 84 | | SyntaxNodeAnalysisContext context, |
| | | 85 | | InterfaceDeclarationSyntax interfaceDeclaration, |
| | | 86 | | AttributeSyntax attributeSyntax) |
| | | 87 | | { |
| | 10 | 88 | | var interfaceSymbol = context.SemanticModel.GetDeclaredSymbol(interfaceDeclaration); |
| | 10 | 89 | | if (interfaceSymbol == null) |
| | 0 | 90 | | return; |
| | | 91 | | |
| | 72 | 92 | | foreach (var member in interfaceSymbol.GetMembers()) |
| | | 93 | | { |
| | | 94 | | // Skip properties - they're valid |
| | 26 | 95 | | if (member is IPropertySymbol propertySymbol) |
| | | 96 | | { |
| | | 97 | | // NDLRGEN032: Check property is get-only |
| | 11 | 98 | | if (propertySymbol.SetMethod != null) |
| | | 99 | | { |
| | 2 | 100 | | context.ReportDiagnostic( |
| | 2 | 101 | | Diagnostic.Create( |
| | 2 | 102 | | DiagnosticDescriptors.ProviderInterfaceInvalidMember, |
| | 2 | 103 | | GetMemberLocation(interfaceDeclaration, member.Name) ?? attributeSyntax.GetLocation(), |
| | 2 | 104 | | interfaceSymbol.Name, |
| | 2 | 105 | | $"a settable property '{member.Name}'")); |
| | | 106 | | } |
| | | 107 | | else |
| | | 108 | | { |
| | | 109 | | // NDLRGEN033: Check for concrete type (warning only) |
| | 9 | 110 | | AnalyzePropertyType(context, interfaceSymbol, propertySymbol, interfaceDeclaration); |
| | | 111 | | } |
| | 9 | 112 | | continue; |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | // Skip special members (constructors, etc.) |
| | 15 | 116 | | if (member.IsImplicitlyDeclared) |
| | | 117 | | continue; |
| | | 118 | | |
| | | 119 | | // NDLRGEN032: Report invalid member types |
| | 15 | 120 | | var memberDescription = member switch |
| | 15 | 121 | | { |
| | 17 | 122 | | IMethodSymbol m when m.MethodKind == MethodKind.Ordinary => $"a method '{member.Name}'", |
| | 0 | 123 | | IEventSymbol => $"an event '{member.Name}'", |
| | 13 | 124 | | _ => $"an unsupported member '{member.Name}'" |
| | 15 | 125 | | }; |
| | | 126 | | |
| | 15 | 127 | | if (member is IMethodSymbol methodSymbol && methodSymbol.MethodKind != MethodKind.Ordinary) |
| | | 128 | | continue; // Skip property getters/setters |
| | | 129 | | |
| | 2 | 130 | | context.ReportDiagnostic( |
| | 2 | 131 | | Diagnostic.Create( |
| | 2 | 132 | | DiagnosticDescriptors.ProviderInterfaceInvalidMember, |
| | 2 | 133 | | GetMemberLocation(interfaceDeclaration, member.Name) ?? attributeSyntax.GetLocation(), |
| | 2 | 134 | | interfaceSymbol.Name, |
| | 2 | 135 | | memberDescription)); |
| | | 136 | | } |
| | 10 | 137 | | } |
| | | 138 | | |
| | | 139 | | private static void AnalyzePropertyType( |
| | | 140 | | SyntaxNodeAnalysisContext context, |
| | | 141 | | INamedTypeSymbol interfaceSymbol, |
| | | 142 | | IPropertySymbol propertySymbol, |
| | | 143 | | InterfaceDeclarationSyntax interfaceDeclaration) |
| | | 144 | | { |
| | 9 | 145 | | var propertyType = propertySymbol.Type; |
| | | 146 | | |
| | | 147 | | // Unwrap nullable |
| | 9 | 148 | | if (propertyType is INamedTypeSymbol namedType && namedType.IsGenericType) |
| | | 149 | | { |
| | 1 | 150 | | var definition = namedType.OriginalDefinition.ToDisplayString(); |
| | 1 | 151 | | if (definition == "System.Nullable<T>") |
| | | 152 | | { |
| | 0 | 153 | | propertyType = namedType.TypeArguments[0]; |
| | | 154 | | } |
| | | 155 | | // Skip collections - they're always fine |
| | 1 | 156 | | if (definition.StartsWith("System.Collections.Generic.IEnumerable<") || |
| | 1 | 157 | | definition.StartsWith("System.Collections.Generic.IReadOnlyCollection<") || |
| | 1 | 158 | | definition.StartsWith("System.Collections.Generic.IReadOnlyList<")) |
| | | 159 | | { |
| | 1 | 160 | | return; |
| | | 161 | | } |
| | | 162 | | } |
| | | 163 | | |
| | | 164 | | // Skip interfaces - they're the recommended pattern |
| | 8 | 165 | | if (propertyType.TypeKind == TypeKind.Interface) |
| | 6 | 166 | | return; |
| | | 167 | | |
| | | 168 | | // Skip factory types (they end with Factory) |
| | 2 | 169 | | if (propertyType.Name.EndsWith("Factory")) |
| | 0 | 170 | | return; |
| | | 171 | | |
| | | 172 | | // Skip provider types (nested providers are fine) |
| | 2 | 173 | | if (HasProviderAttribute(propertyType)) |
| | 0 | 174 | | return; |
| | | 175 | | |
| | | 176 | | // NDLRGEN033: Concrete class type detected |
| | 2 | 177 | | if (propertyType.TypeKind == TypeKind.Class) |
| | | 178 | | { |
| | 2 | 179 | | context.ReportDiagnostic( |
| | 2 | 180 | | Diagnostic.Create( |
| | 2 | 181 | | DiagnosticDescriptors.ProviderPropertyConcreteType, |
| | 2 | 182 | | GetMemberLocation(interfaceDeclaration, propertySymbol.Name) ?? Location.None, |
| | 2 | 183 | | interfaceSymbol.Name, |
| | 2 | 184 | | propertySymbol.Name, |
| | 2 | 185 | | propertyType.ToDisplayString())); |
| | | 186 | | } |
| | 2 | 187 | | } |
| | | 188 | | |
| | | 189 | | private static bool HasProviderAttribute(ITypeSymbol type) |
| | | 190 | | { |
| | 2 | 191 | | return type.GetAttributes().Any(a => |
| | 2 | 192 | | a.AttributeClass?.Name == ProviderAttributeName && |
| | 2 | 193 | | a.AttributeClass.ContainingNamespace?.ToDisplayString() == GeneratorsNamespace); |
| | | 194 | | } |
| | | 195 | | |
| | | 196 | | private static Location? GetMemberLocation(TypeDeclarationSyntax typeDeclaration, string memberName) |
| | | 197 | | { |
| | 22 | 198 | | foreach (var member in typeDeclaration.Members) |
| | | 199 | | { |
| | 8 | 200 | | if (member is PropertyDeclarationSyntax prop && prop.Identifier.Text == memberName) |
| | 4 | 201 | | return prop.GetLocation(); |
| | 4 | 202 | | if (member is MethodDeclarationSyntax method && method.Identifier.Text == memberName) |
| | 2 | 203 | | return method.GetLocation(); |
| | 2 | 204 | | if (member is EventDeclarationSyntax evt && evt.Identifier.Text == memberName) |
| | 0 | 205 | | return evt.GetLocation(); |
| | | 206 | | } |
| | 0 | 207 | | return null; |
| | | 208 | | } |
| | | 209 | | |
| | | 210 | | private static bool IsProviderAttribute(INamedTypeSymbol attributeSymbol) |
| | | 211 | | { |
| | 156 | 212 | | if (attributeSymbol.Name != ProviderAttributeName) |
| | 143 | 213 | | return false; |
| | | 214 | | |
| | 13 | 215 | | return attributeSymbol.ContainingNamespace?.ToDisplayString() == GeneratorsNamespace; |
| | | 216 | | } |
| | | 217 | | } |