| | | 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 detects plugin implementations with constructor dependencies. |
| | | 12 | | /// </summary> |
| | | 13 | | [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| | | 14 | | public sealed class PluginConstructorDependenciesAnalyzer : DiagnosticAnalyzer |
| | | 15 | | { |
| | | 16 | | // Plugin interfaces that are instantiated before DI is available |
| | 1 | 17 | | private static readonly ImmutableHashSet<string> PluginInterfaceNames = ImmutableHashSet.Create( |
| | 1 | 18 | | "IServiceCollectionPlugin", |
| | 1 | 19 | | "IPostBuildServiceCollectionPlugin", |
| | 1 | 20 | | "IWebApplicationBuilderPlugin", |
| | 1 | 21 | | "IHostApplicationBuilderPlugin"); |
| | | 22 | | |
| | | 23 | | public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => |
| | 213 | 24 | | ImmutableArray.Create(DiagnosticDescriptors.PluginHasConstructorDependencies); |
| | | 25 | | |
| | | 26 | | public override void Initialize(AnalysisContext context) |
| | | 27 | | { |
| | 19 | 28 | | context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| | 19 | 29 | | context.EnableConcurrentExecution(); |
| | | 30 | | |
| | 19 | 31 | | context.RegisterSyntaxNodeAction(AnalyzeClassDeclaration, SyntaxKind.ClassDeclaration); |
| | 19 | 32 | | } |
| | | 33 | | |
| | | 34 | | private static void AnalyzeClassDeclaration(SyntaxNodeAnalysisContext context) |
| | | 35 | | { |
| | 11 | 36 | | var classDeclaration = (ClassDeclarationSyntax)context.Node; |
| | | 37 | | |
| | | 38 | | // Skip abstract classes |
| | 11 | 39 | | if (classDeclaration.Modifiers.Any(SyntaxKind.AbstractKeyword)) |
| | | 40 | | { |
| | 1 | 41 | | return; |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | // Check if class implements a plugin interface |
| | 10 | 45 | | if (!ImplementsPluginInterface(classDeclaration, context.SemanticModel)) |
| | | 46 | | { |
| | 1 | 47 | | return; |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | // Check constructors |
| | 9 | 51 | | var constructors = classDeclaration.Members |
| | 9 | 52 | | .OfType<ConstructorDeclarationSyntax>() |
| | 11 | 53 | | .Where(c => !c.Modifiers.Any(SyntaxKind.StaticKeyword)) |
| | 9 | 54 | | .ToList(); |
| | | 55 | | |
| | | 56 | | // If no explicit constructors, there's an implicit parameterless constructor - OK |
| | 9 | 57 | | if (constructors.Count == 0) |
| | | 58 | | { |
| | 1 | 59 | | return; |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | // Check if there's at least one public parameterless constructor |
| | 8 | 63 | | var hasParameterlessConstructor = constructors.Any(c => |
| | 18 | 64 | | c.Modifiers.Any(SyntaxKind.PublicKeyword) && |
| | 18 | 65 | | c.ParameterList.Parameters.Count == 0); |
| | | 66 | | |
| | 8 | 67 | | if (hasParameterlessConstructor) |
| | | 68 | | { |
| | 2 | 69 | | return; |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | // Report diagnostic on any constructor with parameters |
| | 36 | 73 | | foreach (var constructor in constructors.Where(c => c.ParameterList.Parameters.Count > 0)) |
| | | 74 | | { |
| | 8 | 75 | | var diagnostic = Diagnostic.Create( |
| | 8 | 76 | | DiagnosticDescriptors.PluginHasConstructorDependencies, |
| | 8 | 77 | | constructor.Identifier.GetLocation(), |
| | 8 | 78 | | classDeclaration.Identifier.Text); |
| | | 79 | | |
| | 8 | 80 | | context.ReportDiagnostic(diagnostic); |
| | | 81 | | } |
| | 6 | 82 | | } |
| | | 83 | | |
| | | 84 | | private static bool ImplementsPluginInterface( |
| | | 85 | | ClassDeclarationSyntax classDeclaration, |
| | | 86 | | SemanticModel semanticModel) |
| | | 87 | | { |
| | 10 | 88 | | if (classDeclaration.BaseList == null) |
| | | 89 | | { |
| | 1 | 90 | | return false; |
| | | 91 | | } |
| | | 92 | | |
| | 27 | 93 | | foreach (var baseType in classDeclaration.BaseList.Types) |
| | | 94 | | { |
| | 9 | 95 | | var typeInfo = semanticModel.GetTypeInfo(baseType.Type); |
| | 9 | 96 | | var typeSymbol = typeInfo.Type; |
| | | 97 | | |
| | 9 | 98 | | if (typeSymbol == null) |
| | | 99 | | { |
| | | 100 | | // Fallback to syntax-based check |
| | 0 | 101 | | var typeName = GetTypeName(baseType.Type); |
| | 0 | 102 | | if (typeName != null && PluginInterfaceNames.Contains(typeName)) |
| | | 103 | | { |
| | 0 | 104 | | return true; |
| | | 105 | | } |
| | | 106 | | continue; |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | // Check the type and all its interfaces |
| | 9 | 110 | | if (IsPluginInterface(typeSymbol)) |
| | | 111 | | { |
| | 9 | 112 | | return true; |
| | | 113 | | } |
| | | 114 | | |
| | 0 | 115 | | foreach (var iface in typeSymbol.AllInterfaces) |
| | | 116 | | { |
| | 0 | 117 | | if (IsPluginInterface(iface)) |
| | | 118 | | { |
| | 0 | 119 | | return true; |
| | | 120 | | } |
| | | 121 | | } |
| | | 122 | | } |
| | | 123 | | |
| | 0 | 124 | | return false; |
| | | 125 | | } |
| | | 126 | | |
| | | 127 | | private static bool IsPluginInterface(ITypeSymbol typeSymbol) |
| | | 128 | | { |
| | 9 | 129 | | return PluginInterfaceNames.Contains(typeSymbol.Name) && |
| | 9 | 130 | | typeSymbol.ContainingNamespace?.ToString() == "NexusLabs.Needlr"; |
| | | 131 | | } |
| | | 132 | | |
| | | 133 | | private static string? GetTypeName(TypeSyntax typeSyntax) |
| | | 134 | | { |
| | 0 | 135 | | return typeSyntax switch |
| | 0 | 136 | | { |
| | 0 | 137 | | IdentifierNameSyntax identifier => identifier.Identifier.Text, |
| | 0 | 138 | | QualifiedNameSyntax qualified => qualified.Right.Identifier.Text, |
| | 0 | 139 | | _ => null |
| | 0 | 140 | | }; |
| | | 141 | | } |
| | | 142 | | } |