-
-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathEventUsageAnalyzer.cs
More file actions
341 lines (274 loc) · 15.7 KB
/
EventUsageAnalyzer.cs
File metadata and controls
341 lines (274 loc) · 15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// Copyright (C) Eventuous HQ OÜ. All rights reserved
// Licensed under the Apache License, Version 2.0.
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Operations;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using static Eventuous.Shared.Generators.Constants;
// ReSharper disable CognitiveComplexity
namespace Eventuous.Shared.Generators;
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class EventUsageAnalyzer : DiagnosticAnalyzer {
public const string DiagnosticId = "EVTC001";
static readonly DiagnosticDescriptor MissingEventTypeAttribute = new(
id: DiagnosticId,
title: "Event type is not decorated with [EventType]",
messageFormat: "Event type '{0}' is used as a domain event but isn't annotated with [EventType]",
category: "Eventuous",
defaultSeverity: DiagnosticSeverity.Warning,
isEnabledByDefault: true,
description: "Domain events should be annotated with [EventType] so they can be resolved by the type mapper."
);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => [MissingEventTypeAttribute];
public override void Initialize(AnalysisContext context) {
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.EnableConcurrentExecution();
context.RegisterCompilationStartAction(compilationContext => {
// Resolve well-known type symbols once per compilation
var compilation = compilationContext.Compilation;
var knownTypes = new KnownTypeSymbols(compilation);
compilationContext.RegisterOperationAction(ctx => AnalyzeInvocation(ctx, knownTypes), OperationKind.Invocation);
compilationContext.RegisterOperationAction(ctx => AnalyzeObjectCreation(ctx, knownTypes), OperationKind.ObjectCreation);
});
}
/// <summary>
/// Cache of well-known type symbols resolved from the compilation.
/// This makes the analyzer refactoring-safe by using symbol comparison instead of string matching.
/// </summary>
sealed class KnownTypeSymbols {
public INamedTypeSymbol? EventTypeAttribute { get; }
public INamedTypeSymbol? TypeMapper { get; }
public INamedTypeSymbol? Aggregate { get; }
public INamedTypeSymbol? State { get; }
public INamedTypeSymbol? CommandHandlerBuilder { get; }
public INamedTypeSymbol? IDefineExecution { get; }
public INamedTypeSymbol? ICommandHandlerBuilder { get; }
public INamedTypeSymbol? IDefineStoreOrExecution { get; }
public KnownTypeSymbols(Compilation compilation) {
EventTypeAttribute = compilation.GetTypeByMetadataName(EventTypeAttrFqcn);
TypeMapper = compilation.GetTypeByMetadataName($"{BaseNamespace}.TypeMapper");
Aggregate = compilation.GetTypeByMetadataName($"{BaseNamespace}.Aggregate`1");
State = compilation.GetTypeByMetadataName($"{BaseNamespace}.State`1");
CommandHandlerBuilder = compilation.GetTypeByMetadataName($"{BaseNamespace}.CommandHandlerBuilder");
IDefineExecution = compilation.GetTypeByMetadataName($"{BaseNamespace}.IDefineExecution");
ICommandHandlerBuilder = compilation.GetTypeByMetadataName($"{BaseNamespace}.ICommandHandlerBuilder");
IDefineStoreOrExecution = compilation.GetTypeByMetadataName($"{BaseNamespace}.IDefineStoreOrExecution");
}
}
static ImmutableHashSet<ITypeSymbol> GetExplicitRegistrations(OperationAnalysisContext ctx, KnownTypeSymbols knownTypes) {
var model = ctx.Operation.SemanticModel;
if (model == null) return ImmutableHashSet<ITypeSymbol>.Empty;
var root = ctx.Operation.Syntax.SyntaxTree.GetRoot();
var set = ImmutableHashSet.CreateBuilder<ITypeSymbol>(SymbolEqualityComparer.Default);
foreach (var invSyntax in root.DescendantNodes().OfType<InvocationExpressionSyntax>()) {
if (model.GetOperation(invSyntax) is not IInvocationOperation op) continue;
var m = op.TargetMethod;
// Use symbol comparison when available, fall back to string comparison
if (m.Name != "AddType") continue;
var ct = m.ContainingType;
if (ct == null) continue;
// Prefer symbol comparison (refactoring-safe)
var isTypeMapper = knownTypes.TypeMapper != null
? SymbolEqualityComparer.Default.Equals(ct, knownTypes.TypeMapper)
: ct.Name == "TypeMapper" && ct.ContainingNamespace?.ToDisplayString() == BaseNamespace;
if (!isTypeMapper) continue;
if (m.TypeArguments.Length == 1) {
set.Add(m.TypeArguments[0]);
continue;
}
if (op.Arguments.Length > 0 && op.Arguments[0].Value is ITypeOfOperation typeOfOp) {
set.Add(typeOfOp.TypeOperand);
}
}
return set.ToImmutable();
}
static bool IsExplicitlyRegistered(ITypeSymbol type, OperationAnalysisContext ctx, KnownTypeSymbols knownTypes) {
var set = GetExplicitRegistrations(ctx, knownTypes);
return set.Contains(type);
}
static void AnalyzeInvocation(OperationAnalysisContext ctx, KnownTypeSymbols knownTypes) {
if (ctx.Operation is not IInvocationOperation inv) return;
var method = inv.TargetMethod;
switch (method) {
// Case 1: Aggregate<T>.Apply<TEvent>(TEvent evt)
case { Name: "Apply", TypeArguments.Length: 1, Parameters.Length: 1 }: {
var containing = method.ContainingType;
if (IsAggregate(containing, knownTypes)) {
var eventType = method.TypeArguments[0];
if (IsConcreteEvent(eventType) && !HasEventTypeAttribute(eventType, knownTypes) && !IsExplicitlyRegistered(eventType, ctx, knownTypes)) {
ctx.ReportDiagnostic(Diagnostic.Create(MissingEventTypeAttribute, inv.Syntax.GetLocation(), eventType.ToDisplayString()));
}
}
return;
}
// Case 1b: State<T>.When(...) invocations where an event instance is passed
case { Name: "When", Parameters.Length: 1 } when IsState(method.ContainingType, knownTypes): {
var arg = inv.Arguments.Length > 0 ? inv.Arguments[0].Value : null;
ITypeSymbol? eventType = null;
if (method.TypeArguments.Length == 1) {
eventType = method.TypeArguments[0];
}
eventType ??= arg switch {
IConversionOperation { Operand.Type: not null } conv => conv.Operand.Type,
_ => arg?.Type
};
if (eventType != null && IsConcreteEvent(eventType) && !HasEventTypeAttribute(eventType, knownTypes) && !IsExplicitlyRegistered(eventType, ctx, knownTypes)) {
var location = arg?.Syntax.GetLocation() ?? inv.Syntax.GetLocation();
ctx.ReportDiagnostic(Diagnostic.Create(MissingEventTypeAttribute, location, eventType.ToDisplayString()));
}
return;
}
// Case 1c: State<T>.On<TEvent>(...) handler registrations
case { Name: "On", TypeArguments.Length: 1 } when IsState(method.ContainingType, knownTypes): {
var eventType = method.TypeArguments[0];
if (IsConcreteEvent(eventType) && !HasEventTypeAttribute(eventType, knownTypes) && !IsExplicitlyRegistered(eventType, ctx, knownTypes)) {
ctx.ReportDiagnostic(Diagnostic.Create(MissingEventTypeAttribute, inv.Syntax.GetLocation(), eventType.ToDisplayString()));
}
return;
}
}
// Case 2: Functional service: Act/ActAsync handlers
if (method.Name is "Act" or "ActAsync") {
// Heuristic: only consider the overloads that accept a delegate and are defined in CommandHandlerBuilder interfaces/classes
if (!IsFunctionalServiceAct(method, knownTypes)) return;
foreach (var value in inv.Arguments.Select(arg => arg.Value)) {
switch (value) {
case null:
continue;
// If the argument is a lambda, analyze its body for created event instances
case IAnonymousFunctionOperation lambda:
AnalyzeDelegateBodyForEventCreations(ctx, lambda.Body, knownTypes);
break;
case IConversionOperation { Operand: IAnonymousFunctionOperation lambdaConv }:
AnalyzeDelegateBodyForEventCreations(ctx, lambdaConv.Body, knownTypes);
break;
}
}
}
}
static void AnalyzeDelegateBodyForEventCreations(OperationAnalysisContext ctx, IBlockOperation? body, KnownTypeSymbols knownTypes) {
if (body is null) return;
foreach (var op in body.Descendants()) {
if (op is IObjectCreationOperation create) {
var created = create.Type;
if (created != null && IsConcreteEvent(created) && !HasEventTypeAttribute(created, knownTypes) && !IsExplicitlyRegistered(created, ctx, knownTypes)) {
ctx.ReportDiagnostic(Diagnostic.Create(MissingEventTypeAttribute, create.Syntax.GetLocation(), created.ToDisplayString()));
}
}
}
}
static void AnalyzeObjectCreation(OperationAnalysisContext ctx, KnownTypeSymbols knownTypes) {
// Global safety net for method groups passed into Act where we couldn't traverse the body via the invocation site.
// If the object creation is within a method that appears to be an Act handler (returns NewEvents/ IEnumerable<object>), warn.
if (ctx.Operation is not IObjectCreationOperation create) return;
var created = create.Type;
if (created is null || !IsConcreteEvent(created)) return;
var method = GetEnclosingMethod(ctx.Operation);
if (method == null) return;
if (ReturnsNewEvents(method)) {
if (!HasEventTypeAttribute(created, knownTypes) && !IsExplicitlyRegistered(created, ctx, knownTypes)) {
ctx.ReportDiagnostic(Diagnostic.Create(MissingEventTypeAttribute, create.Syntax.GetLocation(), created.ToDisplayString()));
}
}
}
static IMethodSymbol? GetEnclosingMethod(IOperation op) {
for (var p = op.Parent; p != null; p = p.Parent) {
switch (p) {
case IAnonymousFunctionOperation anon:
return anon.Symbol;
case ILocalFunctionOperation local:
return local.Symbol;
case IMethodBodyOperation body:
return body.SemanticModel?.GetEnclosingSymbol(body.Syntax.SpanStart) as IMethodSymbol;
}
}
return null;
}
static bool ReturnsNewEvents(IMethodSymbol method) {
// NewEvents is a global alias for IEnumerable<object> within Eventuous; we’ll detect either the alias name or the underlying type
var ret = method.ReturnType;
return ret switch {
null => false,
// Check if it's an array
IArrayTypeSymbol { ElementType.SpecialType: SpecialType.System_Object } => true,
// Check name first (alias would appear as IEnumerable<object> in symbols, so rely on namespace/type)
INamedTypeSymbol named when IsIEnumerableOfObject(named) => true,
_ => false
};
}
static bool IsIEnumerableOfObject(INamedTypeSymbol type) {
if (type.Name == "IEnumerable" && type.ContainingNamespace.ToDisplayString() == "System.Collections.Generic" && type.TypeArguments.Length == 1) {
return type.TypeArguments[0] is { SpecialType: SpecialType.System_Object };
}
return false;
}
static bool IsAggregate(INamedTypeSymbol? type, KnownTypeSymbols knownTypes) {
if (type == null) return false;
// Walk base types to check if it derives from Eventuous.Aggregate<>
for (var t = type; t != null; t = t.BaseType) {
// Prefer symbol comparison (refactoring-safe)
if (knownTypes.Aggregate != null) {
if (SymbolEqualityComparer.Default.Equals(t.OriginalDefinition, knownTypes.Aggregate)) {
return true;
}
}
else {
// Fallback to string comparison
if (t is { Name: "Aggregate", Arity: 1 } && t.ContainingNamespace.ToDisplayString() == BaseNamespace) {
return true;
}
}
}
return false;
}
static bool IsState(INamedTypeSymbol? type, KnownTypeSymbols knownTypes) {
if (type == null) return false;
// Walk base types to check if it derives from Eventuous.State<>
for (var t = type; t != null; t = t.BaseType) {
// Prefer symbol comparison (refactoring-safe)
if (knownTypes.State != null) {
if (SymbolEqualityComparer.Default.Equals(t.OriginalDefinition, knownTypes.State)) {
return true;
}
}
else {
// Fallback to string comparison
if (t is { Name: "State", Arity: 1 } && t.ContainingNamespace.ToDisplayString() == BaseNamespace) {
return true;
}
}
}
return false;
}
static bool IsFunctionalServiceAct(IMethodSymbol method, KnownTypeSymbols knownTypes) {
// We only care about the Act methods from CommandHandlerBuilder and the related interfaces in Eventuous namespace
if (method.Name is not ("Act" or "ActAsync")) return false;
var containing = method.ContainingType;
if (containing == null) return false;
// Prefer symbol comparison (refactoring-safe)
if (knownTypes.CommandHandlerBuilder != null || knownTypes.IDefineExecution != null ||
knownTypes.ICommandHandlerBuilder != null || knownTypes.IDefineStoreOrExecution != null) {
return SymbolEqualityComparer.Default.Equals(containing, knownTypes.CommandHandlerBuilder) ||
SymbolEqualityComparer.Default.Equals(containing, knownTypes.IDefineExecution) ||
SymbolEqualityComparer.Default.Equals(containing, knownTypes.ICommandHandlerBuilder) ||
SymbolEqualityComparer.Default.Equals(containing, knownTypes.IDefineStoreOrExecution);
}
// Fallback to string comparison
var ns = containing.ContainingNamespace?.ToDisplayString();
if (ns != BaseNamespace) return false;
return containing.Name is "CommandHandlerBuilder" or "IDefineExecution" or "ICommandHandlerBuilder" or "IDefineStoreOrExecution";
}
static bool IsConcreteEvent(ITypeSymbol type) => type.TypeKind is TypeKind.Class or TypeKind.Struct;
static bool HasEventTypeAttribute(ITypeSymbol type, KnownTypeSymbols knownTypes) {
// Prefer symbol comparison (refactoring-safe)
if (knownTypes.EventTypeAttribute != null) {
return type.GetAttributes().Any(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, knownTypes.EventTypeAttribute));
}
// Fallback to string comparison
return (from attrClass in type.GetAttributes().Select(a => a.AttributeClass).OfType<INamedTypeSymbol>()
let name = attrClass.ToDisplayString()
where name == EventTypeAttrFqcn || attrClass.Name is EventTypeAttribute
select attrClass).Any();
}
}