| | | 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 | | using NexusLabs.Needlr.Roslyn.Shared; |
| | | 10 | | |
| | | 11 | | namespace NexusLabs.Needlr.SignalR.Analyzers; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Analyzer that flags an <c>IHubRegistrationPlugin</c> implementation that is eligible |
| | | 15 | | /// for Needlr's generated-constructor generation. The SignalR hub-registration generator |
| | | 16 | | /// requires parameterless activation and deliberately excludes such a type from |
| | | 17 | | /// registration, so a plugin shaped this way is silently never registered unless flagged |
| | | 18 | | /// here. |
| | | 19 | | /// </summary> |
| | | 20 | | [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| | | 21 | | public sealed class HubRegistrationPluginConstructorAnalyzer : DiagnosticAnalyzer |
| | | 22 | | { |
| | | 23 | | private const string HubRegistrationPluginInterfaceName = "NexusLabs.Needlr.SignalR.IHubRegistrationPlugin"; |
| | | 24 | | |
| | | 25 | | public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => |
| | 162 | 26 | | ImmutableArray.Create(DiagnosticDescriptors.HubRegistrationPluginRequiresParameterlessActivation); |
| | | 27 | | |
| | | 28 | | public override void Initialize(AnalysisContext context) |
| | | 29 | | { |
| | 15 | 30 | | context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| | 15 | 31 | | context.EnableConcurrentExecution(); |
| | | 32 | | |
| | 15 | 33 | | context.RegisterSyntaxNodeAction(AnalyzeClass, SyntaxKind.ClassDeclaration); |
| | 15 | 34 | | } |
| | | 35 | | |
| | | 36 | | private static void AnalyzeClass(SyntaxNodeAnalysisContext context) |
| | | 37 | | { |
| | 29 | 38 | | var classDeclaration = (ClassDeclarationSyntax)context.Node; |
| | 29 | 39 | | if (context.SemanticModel.GetDeclaredSymbol(classDeclaration) is not INamedTypeSymbol typeSymbol) |
| | 0 | 40 | | return; |
| | | 41 | | |
| | 29 | 42 | | if (!GeneratedConstructorEligibility.IsCanonicalDeclaration(typeSymbol, classDeclaration)) |
| | 2 | 43 | | return; |
| | | 44 | | |
| | 27 | 45 | | if (!ImplementsHubRegistrationPlugin(typeSymbol)) |
| | 20 | 46 | | return; |
| | | 47 | | |
| | 7 | 48 | | if (!GeneratedConstructorEligibility.IsEligibleForGeneratedConstructor(typeSymbol)) |
| | 1 | 49 | | return; |
| | | 50 | | |
| | 6 | 51 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 6 | 52 | | DiagnosticDescriptors.HubRegistrationPluginRequiresParameterlessActivation, |
| | 6 | 53 | | classDeclaration.Identifier.GetLocation(), |
| | 6 | 54 | | typeSymbol.Name)); |
| | 6 | 55 | | } |
| | | 56 | | |
| | | 57 | | private static bool ImplementsHubRegistrationPlugin(INamedTypeSymbol typeSymbol) |
| | | 58 | | { |
| | 35 | 59 | | return typeSymbol.AllInterfaces.Any(i => i.ToDisplayString() == HubRegistrationPluginInterfaceName); |
| | | 60 | | } |
| | | 61 | | } |