|
| 1 | +using System.Collections.Immutable; |
| 2 | +using Microsoft.CodeAnalysis; |
| 3 | +using Microsoft.CodeAnalysis.CSharp; |
| 4 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 5 | +using Microsoft.CodeAnalysis.Diagnostics; |
| 6 | + |
| 7 | +namespace Dapr.Actors.Analyzers; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Analyzes actor registration in Dapr applications. |
| 11 | +/// </summary> |
| 12 | +[DiagnosticAnalyzer(LanguageNames.CSharp)] |
| 13 | +public class ActorAnalyzer : DiagnosticAnalyzer |
| 14 | +{ |
| 15 | + private static readonly DiagnosticDescriptor DiagnosticDescriptorActorRegistration = new( |
| 16 | + "DAPR0001", |
| 17 | + "Actor class not registered", |
| 18 | + "The actor class '{0}' is not registered", |
| 19 | + "Usage", |
| 20 | + DiagnosticSeverity.Warning, |
| 21 | + isEnabledByDefault: true); |
| 22 | + |
| 23 | + private static readonly DiagnosticDescriptor DiagnosticDescriptorJsonSerialization = new( |
| 24 | + "DAPR0002", |
| 25 | + "Use JsonSerialization", |
| 26 | + "Add options.UseJsonSerialization to support interoperability with non-.NET actors", |
| 27 | + "Usage", |
| 28 | + DiagnosticSeverity.Warning, |
| 29 | + isEnabledByDefault: true); |
| 30 | + |
| 31 | + private static readonly DiagnosticDescriptor DiagnosticDescriptorMapActorsHandlers = new( |
| 32 | + "DAPR0003", |
| 33 | + "Call MapActorsHandlers", |
| 34 | + "Call app.MapActorsHandlers to map endpoints for Dapr actors", |
| 35 | + "Usage", |
| 36 | + DiagnosticSeverity.Warning, |
| 37 | + isEnabledByDefault: true); |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Gets the supported diagnostics for this analyzer. |
| 41 | + /// </summary> |
| 42 | + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create( |
| 43 | + DiagnosticDescriptorActorRegistration, |
| 44 | + DiagnosticDescriptorJsonSerialization, |
| 45 | + DiagnosticDescriptorMapActorsHandlers); |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Initializes the analyzer. |
| 49 | + /// </summary> |
| 50 | + /// <param name="context">The analysis context.</param> |
| 51 | + public override void Initialize(AnalysisContext context) |
| 52 | + { |
| 53 | + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| 54 | + context.EnableConcurrentExecution(); |
| 55 | + context.RegisterSyntaxNodeAction(AnalyzeActorRegistration, SyntaxKind.ClassDeclaration); |
| 56 | + context.RegisterSyntaxNodeAction(AnalyzeSerialization, SyntaxKind.CompilationUnit); |
| 57 | + context.RegisterSyntaxNodeAction(AnalyzeMapActorsHandlers, SyntaxKind.CompilationUnit); |
| 58 | + } |
| 59 | + |
| 60 | + private void AnalyzeActorRegistration(SyntaxNodeAnalysisContext context) |
| 61 | + { |
| 62 | + var classDeclaration = (ClassDeclarationSyntax)context.Node; |
| 63 | + |
| 64 | + if (classDeclaration.BaseList != null) |
| 65 | + { |
| 66 | + var baseTypeSyntax = classDeclaration.BaseList.Types[0].Type; |
| 67 | + |
| 68 | + if (context.SemanticModel.GetSymbolInfo(baseTypeSyntax).Symbol is INamedTypeSymbol baseTypeSymbol) |
| 69 | + { |
| 70 | + var baseTypeName = baseTypeSymbol.ToDisplayString(); |
| 71 | + |
| 72 | + { |
| 73 | + var actorTypeName = classDeclaration.Identifier.Text; |
| 74 | + bool isRegistered = CheckIfActorIsRegistered(actorTypeName, context.SemanticModel); |
| 75 | + if (!isRegistered) |
| 76 | + { |
| 77 | + var diagnostic = Diagnostic.Create(DiagnosticDescriptorActorRegistration, classDeclaration.Identifier.GetLocation(), actorTypeName); |
| 78 | + context.ReportDiagnostic(diagnostic); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private static bool CheckIfActorIsRegistered(string actorTypeName, SemanticModel semanticModel) |
| 86 | + { |
| 87 | + var methodInvocations = new List<InvocationExpressionSyntax>(); |
| 88 | + foreach (var syntaxTree in semanticModel.Compilation.SyntaxTrees) |
| 89 | + { |
| 90 | + var root = syntaxTree.GetRoot(); |
| 91 | + methodInvocations.AddRange(root.DescendantNodes().OfType<InvocationExpressionSyntax>()); |
| 92 | + } |
| 93 | + |
| 94 | + foreach (var invocation in methodInvocations) |
| 95 | + { |
| 96 | + if (invocation.Expression is not MemberAccessExpressionSyntax memberAccess) |
| 97 | + { |
| 98 | + continue; |
| 99 | + } |
| 100 | + |
| 101 | + var methodName = memberAccess.Name.Identifier.Text; |
| 102 | + if (methodName == "RegisterActor") |
| 103 | + { |
| 104 | + if (memberAccess.Name is GenericNameSyntax typeArgumentList && typeArgumentList.TypeArgumentList.Arguments.Count > 0) |
| 105 | + { |
| 106 | + if (typeArgumentList.TypeArgumentList.Arguments[0] is IdentifierNameSyntax typeArgument) |
| 107 | + { |
| 108 | + if (typeArgument.Identifier.Text == actorTypeName) |
| 109 | + { |
| 110 | + return true; |
| 111 | + } |
| 112 | + } |
| 113 | + else if (typeArgumentList.TypeArgumentList.Arguments[0] is QualifiedNameSyntax qualifiedName) |
| 114 | + { |
| 115 | + if (qualifiedName.Right.Identifier.Text == actorTypeName) |
| 116 | + { |
| 117 | + return true; |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + return false; |
| 125 | + } |
| 126 | + |
| 127 | + private void AnalyzeSerialization(SyntaxNodeAnalysisContext context) |
| 128 | + { |
| 129 | + var addActorsInvocation = FindInvocation(context, "AddActors"); |
| 130 | + |
| 131 | + if (addActorsInvocation != null) |
| 132 | + { |
| 133 | + var optionsLambda = addActorsInvocation.ArgumentList.Arguments |
| 134 | + .Select(arg => arg.Expression) |
| 135 | + .OfType<SimpleLambdaExpressionSyntax>() |
| 136 | + .FirstOrDefault(); |
| 137 | + |
| 138 | + if (optionsLambda != null) |
| 139 | + { |
| 140 | + var lambdaBody = optionsLambda.Body; |
| 141 | + var assignments = lambdaBody.DescendantNodes().OfType<AssignmentExpressionSyntax>(); |
| 142 | + |
| 143 | + var useJsonSerialization = assignments.Any(assignment => |
| 144 | + assignment.Left is MemberAccessExpressionSyntax memberAccess && |
| 145 | + memberAccess.Name is IdentifierNameSyntax identifier && |
| 146 | + identifier.Identifier.Text == "UseJsonSerialization" && |
| 147 | + assignment.Right is LiteralExpressionSyntax literal && |
| 148 | + literal.Token.ValueText == "true"); |
| 149 | + |
| 150 | + if (!useJsonSerialization) |
| 151 | + { |
| 152 | + var diagnostic = Diagnostic.Create(DiagnosticDescriptorJsonSerialization, addActorsInvocation.GetLocation()); |
| 153 | + context.ReportDiagnostic(diagnostic); |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + private InvocationExpressionSyntax? FindInvocation(SyntaxNodeAnalysisContext context, string methodName) |
| 160 | + { |
| 161 | + foreach (var syntaxTree in context.SemanticModel.Compilation.SyntaxTrees) |
| 162 | + { |
| 163 | + var root = syntaxTree.GetRoot(); |
| 164 | + var invocation = root.DescendantNodes().OfType<InvocationExpressionSyntax>() |
| 165 | + .FirstOrDefault(invocation => invocation.Expression is MemberAccessExpressionSyntax memberAccess && |
| 166 | + memberAccess.Name.Identifier.Text == methodName); |
| 167 | + |
| 168 | + if (invocation != null) |
| 169 | + { |
| 170 | + return invocation; |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + return null; |
| 175 | + } |
| 176 | + |
| 177 | + private void AnalyzeMapActorsHandlers(SyntaxNodeAnalysisContext context) |
| 178 | + { |
| 179 | + var addActorsInvocation = FindInvocation(context, "AddActors"); |
| 180 | + |
| 181 | + if (addActorsInvocation != null) |
| 182 | + { |
| 183 | + bool invokedByWebApplication = false; |
| 184 | + var mapActorsHandlersInvocation = FindInvocation(context, "MapActorsHandlers"); |
| 185 | + |
| 186 | + if (mapActorsHandlersInvocation?.Expression is MemberAccessExpressionSyntax memberAccess) |
| 187 | + { |
| 188 | + var symbolInfo = context.SemanticModel.GetSymbolInfo(memberAccess.Expression); |
| 189 | + if (symbolInfo.Symbol is ILocalSymbol localSymbol) |
| 190 | + { |
| 191 | + var type = localSymbol.Type; |
| 192 | + if (type.ToDisplayString() == "Microsoft.AspNetCore.Builder.WebApplication") |
| 193 | + { |
| 194 | + invokedByWebApplication = true; |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + if (mapActorsHandlersInvocation == null || !invokedByWebApplication) |
| 200 | + { |
| 201 | + var diagnostic = Diagnostic.Create(DiagnosticDescriptorMapActorsHandlers, addActorsInvocation.GetLocation()); |
| 202 | + context.ReportDiagnostic(diagnostic); |
| 203 | + } |
| 204 | + } |
| 205 | + } |
| 206 | +} |
0 commit comments