|
| 1 | +using System.Collections.Immutable; |
| 2 | +using System.Resources; |
| 3 | +using Microsoft.CodeAnalysis; |
| 4 | +using Microsoft.CodeAnalysis.CSharp; |
| 5 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 6 | +using Microsoft.CodeAnalysis.Diagnostics; |
| 7 | + |
| 8 | +namespace Dapr.Jobs.Analyzers |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// DaprJobsAnalyzer. |
| 12 | + /// </summary> |
| 13 | + [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| 14 | + public sealed class MapDaprScheduledJobHandlerAnalyzer : DiagnosticAnalyzer |
| 15 | + { |
| 16 | + internal static readonly DiagnosticDescriptor DaprJobHandlerRule = new ( |
| 17 | + id: "DAPR3001", |
| 18 | + title: new LocalizableResourceString(nameof(Resources.DAPR3001Title), Resources.ResourceManager, typeof(Resources)), |
| 19 | + messageFormat: new LocalizableResourceString(nameof(Resources.DAPR3001MessageFormat), Resources.ResourceManager, typeof(Resources)), |
| 20 | + category: "Usage", |
| 21 | + DiagnosticSeverity.Warning, |
| 22 | + isEnabledByDefault: true |
| 23 | + ); |
| 24 | + |
| 25 | + private const string DaprJobsNameSpace = "Dapr.Jobs"; |
| 26 | + private const string DaprJobScheduleJobAsyncMethod = "ScheduleJobAsync"; |
| 27 | + private const string MethodNameSpace = "Dapr.Jobs.Extensions"; |
| 28 | + private const string MapDaprScheduledJobHandlerMethod = "MapDaprScheduledJobHandler"; |
| 29 | + |
| 30 | + /// <inheritdoc/> |
| 31 | + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => [DaprJobHandlerRule]; |
| 32 | + |
| 33 | + /// <inheritdoc/> |
| 34 | + public override void Initialize(AnalysisContext context) |
| 35 | + { |
| 36 | + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| 37 | + context.EnableConcurrentExecution(); |
| 38 | + |
| 39 | + context.RegisterSyntaxNodeAction(AnalyzeJobSchedulerHandler, SyntaxKind.InvocationExpression); |
| 40 | + } |
| 41 | + |
| 42 | + private static void AnalyzeJobSchedulerHandler(SyntaxNodeAnalysisContext context) |
| 43 | + { |
| 44 | + var invocationExpression = (InvocationExpressionSyntax)context.Node; |
| 45 | + |
| 46 | + if (!IsNamespaceAndMethodNameEqual(context, invocationExpression, DaprJobsNameSpace, DaprJobScheduleJobAsyncMethod)) |
| 47 | + { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + var arguments = invocationExpression.ArgumentList.Arguments; |
| 52 | + if (arguments.Count > 0 && arguments[0].Expression is LiteralExpressionSyntax literal) |
| 53 | + { |
| 54 | + string jobName = literal.Token.ValueText; |
| 55 | + |
| 56 | + // Now, we will check for a corresponding endpoint route. |
| 57 | + var jobNameLocation = invocationExpression.GetLocation(); |
| 58 | + CheckForEndpointRoute(context, jobName, jobNameLocation); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + private static void CheckForEndpointRoute(SyntaxNodeAnalysisContext context, string jobName, Location jobNameLocation) |
| 63 | + { |
| 64 | + var root = context.SemanticModel.SyntaxTree.GetRoot(); |
| 65 | + |
| 66 | + // Search for MapPost with the corresponding route |
| 67 | + var mapDaprScheduledJobHandlersCount = root |
| 68 | + .DescendantNodes() |
| 69 | + .OfType<InvocationExpressionSyntax>() |
| 70 | + .Count(invocation => IsNamespaceAndMethodNameEqual(context, invocation, MethodNameSpace, MapDaprScheduledJobHandlerMethod)); |
| 71 | + |
| 72 | + if (mapDaprScheduledJobHandlersCount > 0) |
| 73 | + { |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + // If no matching route was found, report a diagnostic |
| 78 | + var diagnostic = Diagnostic.Create(DaprJobHandlerRule, jobNameLocation, jobName); |
| 79 | + context.ReportDiagnostic(diagnostic); |
| 80 | + } |
| 81 | + |
| 82 | + /// <summary> |
| 83 | + /// Determines whether a given method invocation matches a specified method name |
| 84 | + /// within a given namespace. |
| 85 | + /// |
| 86 | + /// For eg: MapDaprScheduledJobHandler (methodname) from the "Dapr.Jobs.Extension" (symbolNamespace) is being called. |
| 87 | + /// </summary> |
| 88 | + /// <param name="context">The syntax analysis context providing semantic information.</param> |
| 89 | + /// <param name="invocation">The invocation expression to analyze.</param> |
| 90 | + /// <param name="symbolNamespace">The expected namespace of the method.</param> |
| 91 | + /// <param name="methodName">The expected method name.</param> |
| 92 | + /// <returns> |
| 93 | + /// <c>true</c> if the method belongs to the specified namespace and has the expected name; |
| 94 | + /// otherwise, <c>false</c>. |
| 95 | + /// </returns> |
| 96 | + private static bool IsNamespaceAndMethodNameEqual(SyntaxNodeAnalysisContext context, InvocationExpressionSyntax invocation, string symbolNamespace, string methodName) |
| 97 | + { |
| 98 | + var symbolInfo = context.SemanticModel.GetSymbolInfo(invocation.Expression); |
| 99 | + if (symbolInfo.Symbol is not IMethodSymbol methodSymbol) |
| 100 | + { |
| 101 | + return false; |
| 102 | + } |
| 103 | + |
| 104 | + // Check if the receiver is of type DaprJobsClient |
| 105 | + return methodSymbol.Name == methodName && |
| 106 | + methodSymbol.ContainingNamespace.ToDisplayString() == symbolNamespace; |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments