| | | 1 | | using System.Collections.Immutable; |
| | | 2 | | |
| | | 3 | | using Microsoft.CodeAnalysis; |
| | | 4 | | using Microsoft.CodeAnalysis.CSharp; |
| | | 5 | | using Microsoft.CodeAnalysis.CSharp.Syntax; |
| | | 6 | | using Microsoft.CodeAnalysis.Diagnostics; |
| | | 7 | | |
| | | 8 | | namespace NexusLabs.Needlr.Analyzers; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Analyzer that validates [Intercept] attribute usage: |
| | | 12 | | /// - NDLRCOR007: Intercept type must implement IMethodInterceptor |
| | | 13 | | /// - NDLRCOR008: [Intercept] applied to class without interfaces |
| | | 14 | | /// </summary> |
| | | 15 | | [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| | | 16 | | public sealed class InterceptAttributeAnalyzer : DiagnosticAnalyzer |
| | | 17 | | { |
| | | 18 | | private const string InterceptAttributeName = "InterceptAttribute"; |
| | | 19 | | private const string IMethodInterceptorName = "IMethodInterceptor"; |
| | | 20 | | private const string NeedlrNamespace = "NexusLabs.Needlr"; |
| | | 21 | | |
| | | 22 | | public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => |
| | 152 | 23 | | ImmutableArray.Create( |
| | 152 | 24 | | DiagnosticDescriptors.InterceptTypeMustImplementInterface, |
| | 152 | 25 | | DiagnosticDescriptors.InterceptOnClassWithoutInterfaces); |
| | | 26 | | |
| | | 27 | | public override void Initialize(AnalysisContext context) |
| | | 28 | | { |
| | 13 | 29 | | context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| | 13 | 30 | | context.EnableConcurrentExecution(); |
| | | 31 | | |
| | 13 | 32 | | context.RegisterSyntaxNodeAction(AnalyzeAttribute, SyntaxKind.Attribute); |
| | 13 | 33 | | } |
| | | 34 | | |
| | | 35 | | private static void AnalyzeAttribute(SyntaxNodeAnalysisContext context) |
| | | 36 | | { |
| | 40 | 37 | | var attributeSyntax = (AttributeSyntax)context.Node; |
| | 40 | 38 | | var attributeSymbol = context.SemanticModel.GetSymbolInfo(attributeSyntax).Symbol?.ContainingType; |
| | | 39 | | |
| | 40 | 40 | | if (attributeSymbol == null) |
| | 0 | 41 | | return; |
| | | 42 | | |
| | | 43 | | // Check if this is an [Intercept] or [Intercept<T>] attribute |
| | 40 | 44 | | if (!IsInterceptAttribute(attributeSymbol)) |
| | 32 | 45 | | return; |
| | | 46 | | |
| | | 47 | | // Get the interceptor type from the attribute |
| | 8 | 48 | | var interceptorType = GetInterceptorType(attributeSyntax, attributeSymbol, context.SemanticModel); |
| | | 49 | | |
| | 8 | 50 | | if (interceptorType != null) |
| | | 51 | | { |
| | | 52 | | // NDLRCOR007: Check if interceptor implements IMethodInterceptor |
| | 8 | 53 | | if (!ImplementsIMethodInterceptor(interceptorType)) |
| | | 54 | | { |
| | 2 | 55 | | var diagnostic = Diagnostic.Create( |
| | 2 | 56 | | DiagnosticDescriptors.InterceptTypeMustImplementInterface, |
| | 2 | 57 | | attributeSyntax.GetLocation(), |
| | 2 | 58 | | interceptorType.Name); |
| | | 59 | | |
| | 2 | 60 | | context.ReportDiagnostic(diagnostic); |
| | | 61 | | } |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | // Find the class/method this attribute is applied to |
| | 8 | 65 | | var targetNode = attributeSyntax.Parent?.Parent; |
| | | 66 | | |
| | | 67 | | // If applied to a class, check NDLRCOR008 |
| | 8 | 68 | | if (targetNode is ClassDeclarationSyntax classDeclaration) |
| | | 69 | | { |
| | 7 | 70 | | var classSymbol = context.SemanticModel.GetDeclaredSymbol(classDeclaration); |
| | 7 | 71 | | if (classSymbol != null) |
| | | 72 | | { |
| | | 73 | | // Check if class implements any non-system interfaces |
| | 7 | 74 | | var hasUserInterface = classSymbol.AllInterfaces.Any(i => |
| | 12 | 75 | | !IsSystemInterface(i) && !IsNeedlrInternalInterface(i)); |
| | | 76 | | |
| | 7 | 77 | | if (!hasUserInterface) |
| | | 78 | | { |
| | 4 | 79 | | var diagnostic = Diagnostic.Create( |
| | 4 | 80 | | DiagnosticDescriptors.InterceptOnClassWithoutInterfaces, |
| | 4 | 81 | | attributeSyntax.GetLocation(), |
| | 4 | 82 | | classDeclaration.Identifier.Text); |
| | | 83 | | |
| | 4 | 84 | | context.ReportDiagnostic(diagnostic); |
| | | 85 | | } |
| | | 86 | | } |
| | | 87 | | } |
| | 8 | 88 | | } |
| | | 89 | | |
| | | 90 | | private static bool IsInterceptAttribute(INamedTypeSymbol attributeSymbol) |
| | | 91 | | { |
| | | 92 | | // Check for InterceptAttribute or InterceptAttribute<T> |
| | 40 | 93 | | var name = attributeSymbol.Name; |
| | 40 | 94 | | if (name != InterceptAttributeName) |
| | 32 | 95 | | return false; |
| | | 96 | | |
| | 8 | 97 | | var ns = attributeSymbol.ContainingNamespace?.ToString(); |
| | 8 | 98 | | return ns == NeedlrNamespace; |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | private static INamedTypeSymbol? GetInterceptorType( |
| | | 102 | | AttributeSyntax attributeSyntax, |
| | | 103 | | INamedTypeSymbol attributeSymbol, |
| | | 104 | | SemanticModel semanticModel) |
| | | 105 | | { |
| | | 106 | | // Generic attribute: [Intercept<LoggingInterceptor>] |
| | 8 | 107 | | if (attributeSymbol.IsGenericType && attributeSymbol.TypeArguments.Length == 1) |
| | | 108 | | { |
| | 6 | 109 | | return attributeSymbol.TypeArguments[0] as INamedTypeSymbol; |
| | | 110 | | } |
| | | 111 | | |
| | | 112 | | // Non-generic attribute: [Intercept(typeof(LoggingInterceptor))] |
| | 2 | 113 | | if (attributeSyntax.ArgumentList?.Arguments.Count > 0) |
| | | 114 | | { |
| | 2 | 115 | | var firstArg = attributeSyntax.ArgumentList.Arguments[0].Expression; |
| | 2 | 116 | | if (firstArg is TypeOfExpressionSyntax typeOfExpr) |
| | | 117 | | { |
| | 2 | 118 | | var typeInfo = semanticModel.GetTypeInfo(typeOfExpr.Type); |
| | 2 | 119 | | return typeInfo.Type as INamedTypeSymbol; |
| | | 120 | | } |
| | | 121 | | } |
| | | 122 | | |
| | 0 | 123 | | return null; |
| | | 124 | | } |
| | | 125 | | |
| | | 126 | | private static bool ImplementsIMethodInterceptor(INamedTypeSymbol typeSymbol) |
| | | 127 | | { |
| | 8 | 128 | | return typeSymbol.AllInterfaces.Any(i => |
| | 14 | 129 | | i.Name == IMethodInterceptorName && |
| | 14 | 130 | | i.ContainingNamespace?.ToString() == NeedlrNamespace); |
| | | 131 | | } |
| | | 132 | | |
| | | 133 | | private static bool IsSystemInterface(INamedTypeSymbol interfaceSymbol) |
| | | 134 | | { |
| | 5 | 135 | | var ns = interfaceSymbol.ContainingNamespace?.ToString() ?? ""; |
| | 5 | 136 | | return ns.StartsWith("System", StringComparison.Ordinal) || |
| | 5 | 137 | | ns.StartsWith("Microsoft", StringComparison.Ordinal); |
| | | 138 | | } |
| | | 139 | | |
| | | 140 | | private static bool IsNeedlrInternalInterface(INamedTypeSymbol interfaceSymbol) |
| | | 141 | | { |
| | | 142 | | // IMethodInterceptor is an internal Needlr interface, not a user service interface |
| | 3 | 143 | | return interfaceSymbol.Name == IMethodInterceptorName && |
| | 3 | 144 | | interfaceSymbol.ContainingNamespace?.ToString() == NeedlrNamespace; |
| | | 145 | | } |
| | | 146 | | } |