| | | 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.SignalR.Analyzers; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Analyzer that validates HubPathAttribute usage for AOT compatibility. |
| | | 12 | | /// </summary> |
| | | 13 | | [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| | | 14 | | public sealed class HubPathAttributeAnalyzer : DiagnosticAnalyzer |
| | | 15 | | { |
| | | 16 | | private const string HubPathAttributeName = "HubPathAttribute"; |
| | | 17 | | private const string HubPathAttributeShortName = "HubPath"; |
| | | 18 | | |
| | | 19 | | public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => |
| | 205 | 20 | | ImmutableArray.Create( |
| | 205 | 21 | | DiagnosticDescriptors.HubPathMustBeConstant, |
| | 205 | 22 | | DiagnosticDescriptors.HubTypeMustBeTypeOf); |
| | | 23 | | |
| | | 24 | | public override void Initialize(AnalysisContext context) |
| | | 25 | | { |
| | 18 | 26 | | context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| | 18 | 27 | | context.EnableConcurrentExecution(); |
| | | 28 | | |
| | 18 | 29 | | context.RegisterSyntaxNodeAction(AnalyzeAttribute, SyntaxKind.Attribute); |
| | 18 | 30 | | } |
| | | 31 | | |
| | | 32 | | private static void AnalyzeAttribute(SyntaxNodeAnalysisContext context) |
| | | 33 | | { |
| | 20 | 34 | | var attributeSyntax = (AttributeSyntax)context.Node; |
| | | 35 | | |
| | | 36 | | // Check if this is a HubPathAttribute |
| | 20 | 37 | | var attributeName = GetAttributeName(attributeSyntax); |
| | 20 | 38 | | if (attributeName != HubPathAttributeName && attributeName != HubPathAttributeShortName) |
| | | 39 | | { |
| | 10 | 40 | | return; |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | // Verify it's actually the Needlr HubPathAttribute via semantic model |
| | 10 | 44 | | var symbolInfo = context.SemanticModel.GetSymbolInfo(attributeSyntax, context.CancellationToken); |
| | 10 | 45 | | if (symbolInfo.Symbol is not IMethodSymbol attributeConstructor) |
| | | 46 | | { |
| | 0 | 47 | | return; |
| | | 48 | | } |
| | | 49 | | |
| | 10 | 50 | | var attributeType = attributeConstructor.ContainingType; |
| | 10 | 51 | | if (attributeType?.ContainingNamespace?.ToDisplayString() != "NexusLabs.Needlr.SignalR") |
| | | 52 | | { |
| | 1 | 53 | | return; |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | // Analyze the attribute arguments |
| | 9 | 57 | | var argumentList = attributeSyntax.ArgumentList; |
| | 9 | 58 | | if (argumentList == null) |
| | | 59 | | { |
| | 0 | 60 | | return; |
| | | 61 | | } |
| | | 62 | | |
| | 52 | 63 | | foreach (var argument in argumentList.Arguments) |
| | | 64 | | { |
| | 17 | 65 | | var parameterName = GetParameterName(argument, context.SemanticModel); |
| | | 66 | | |
| | 17 | 67 | | if (parameterName == "hubPath" || (parameterName == null && IsFirstPositionalArgument(argument, argumentList |
| | | 68 | | { |
| | 9 | 69 | | AnalyzeHubPathArgument(context, argument); |
| | | 70 | | } |
| | 8 | 71 | | else if (parameterName == "hubType" || (parameterName == null && IsSecondPositionalArgument(argument, argume |
| | | 72 | | { |
| | 8 | 73 | | AnalyzeHubTypeArgument(context, argument); |
| | | 74 | | } |
| | | 75 | | } |
| | 9 | 76 | | } |
| | | 77 | | |
| | | 78 | | private static string? GetAttributeName(AttributeSyntax attribute) |
| | | 79 | | { |
| | 20 | 80 | | return attribute.Name switch |
| | 20 | 81 | | { |
| | 9 | 82 | | IdentifierNameSyntax identifier => identifier.Identifier.Text, |
| | 11 | 83 | | QualifiedNameSyntax qualified => qualified.Right.Identifier.Text, |
| | 0 | 84 | | _ => null |
| | 20 | 85 | | }; |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | private static string? GetParameterName(AttributeArgumentSyntax argument, SemanticModel semanticModel) |
| | | 89 | | { |
| | 17 | 90 | | return argument.NameEquals?.Name.Identifier.Text ?? argument.NameColon?.Name.Identifier.Text; |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | private static bool IsFirstPositionalArgument(AttributeArgumentSyntax argument, AttributeArgumentListSyntax argument |
| | | 94 | | { |
| | 11 | 95 | | if (argument.NameEquals != null || argument.NameColon != null) |
| | | 96 | | { |
| | 0 | 97 | | return false; |
| | | 98 | | } |
| | | 99 | | |
| | 32 | 100 | | var positionalArgs = argumentList.Arguments.Where(a => a.NameEquals == null && a.NameColon == null).ToList(); |
| | 11 | 101 | | return positionalArgs.Count > 0 && positionalArgs[0] == argument; |
| | | 102 | | } |
| | | 103 | | |
| | | 104 | | private static bool IsSecondPositionalArgument(AttributeArgumentSyntax argument, AttributeArgumentListSyntax argumen |
| | | 105 | | { |
| | 5 | 106 | | if (argument.NameEquals != null || argument.NameColon != null) |
| | | 107 | | { |
| | 0 | 108 | | return false; |
| | | 109 | | } |
| | | 110 | | |
| | 15 | 111 | | var positionalArgs = argumentList.Arguments.Where(a => a.NameEquals == null && a.NameColon == null).ToList(); |
| | 5 | 112 | | return positionalArgs.Count > 1 && positionalArgs[1] == argument; |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | private static void AnalyzeHubPathArgument(SyntaxNodeAnalysisContext context, AttributeArgumentSyntax argument) |
| | | 116 | | { |
| | 9 | 117 | | var expression = argument.Expression; |
| | | 118 | | |
| | | 119 | | // Check if it's a constant expression |
| | 9 | 120 | | var constantValue = context.SemanticModel.GetConstantValue(expression, context.CancellationToken); |
| | | 121 | | |
| | 9 | 122 | | if (!constantValue.HasValue) |
| | | 123 | | { |
| | | 124 | | // Not a constant - report diagnostic |
| | 4 | 125 | | var expressionText = expression.ToString(); |
| | 4 | 126 | | var diagnostic = Diagnostic.Create( |
| | 4 | 127 | | DiagnosticDescriptors.HubPathMustBeConstant, |
| | 4 | 128 | | expression.GetLocation(), |
| | 4 | 129 | | expressionText); |
| | | 130 | | |
| | 4 | 131 | | context.ReportDiagnostic(diagnostic); |
| | | 132 | | } |
| | 9 | 133 | | } |
| | | 134 | | |
| | | 135 | | private static void AnalyzeHubTypeArgument(SyntaxNodeAnalysisContext context, AttributeArgumentSyntax argument) |
| | | 136 | | { |
| | 8 | 137 | | var expression = argument.Expression; |
| | | 138 | | |
| | | 139 | | // HubType must be a typeof expression |
| | 8 | 140 | | if (expression is not TypeOfExpressionSyntax) |
| | | 141 | | { |
| | 0 | 142 | | var expressionText = expression.ToString(); |
| | 0 | 143 | | var diagnostic = Diagnostic.Create( |
| | 0 | 144 | | DiagnosticDescriptors.HubTypeMustBeTypeOf, |
| | 0 | 145 | | expression.GetLocation(), |
| | 0 | 146 | | expressionText); |
| | | 147 | | |
| | 0 | 148 | | context.ReportDiagnostic(diagnostic); |
| | | 149 | | } |
| | 8 | 150 | | } |
| | | 151 | | } |