-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSuggestCommandsHaveValidators.cs
More file actions
93 lines (78 loc) · 3.33 KB
/
SuggestCommandsHaveValidators.cs
File metadata and controls
93 lines (78 loc) · 3.33 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
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
namespace LeanCode.CodeAnalysis.Analyzers;
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class SuggestCommandsHaveValidators : DiagnosticAnalyzer
{
private const string HandlerTypeName = "LeanCode.CQRS.Execution.ICommandHandler`1";
private const string ValidatorTypeName = "FluentValidation.IValidator`1";
private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(
DiagnosticsIds.CommandsShouldHaveValidators,
"Commands should be validated",
"`{0}` command should be validated",
"Cqrs",
DiagnosticSeverity.Info,
true
);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(Rule);
public override void Initialize(AnalysisContext context)
{
context.EnableConcurrentExecution();
context.ConfigureGeneratedCodeAnalysis(
GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics
);
context.RegisterSyntaxNodeAction(AnalyzeSymbol, SyntaxKind.ClassDeclaration);
}
private static void AnalyzeSymbol(SyntaxNodeAnalysisContext context)
{
var type = (INamedTypeSymbol)context.ContainingSymbol!;
if (!IsCommandHandler(type, out var commandType))
{
return;
}
var tree = type.DeclaringSyntaxReferences.First().SyntaxTree;
if (!CommandIsValidated(commandType!, tree, context.SemanticModel))
{
var diagnostic = Diagnostic.Create(Rule, type.Locations[0], commandType!.Name);
context.ReportDiagnostic(diagnostic);
}
}
private static bool IsCommandHandler(INamedTypeSymbol type, out INamedTypeSymbol? commandType)
{
var handler = type.AllInterfaces.FirstOrDefault(i => i.GetFullNamespaceName() == HandlerTypeName);
if (handler is null)
{
commandType = null;
return false;
}
var typeArgs = handler.TypeArguments;
if (typeArgs is [INamedTypeSymbol cmdType])
{
commandType = cmdType;
return true;
}
commandType = null;
return false;
}
private static bool CommandIsValidated(INamedTypeSymbol command, SyntaxTree tree, SemanticModel model)
{
// This way of checking is a hack, we are looking for validators only in current file.
// However searching for implementations e.g. via SymbolFinder requires access to full solution
// which we don't have here.
var classes = tree.GetRoot().DescendantNodes().OfType<ClassDeclarationSyntax>();
var types = classes.Select(cl => model.GetDeclaredSymbol(cl) as ITypeSymbol);
return types.Where(cl => cl != null).Where(cl => IsThisCommandValidator(command, cl!)).Any();
}
private static bool IsThisCommandValidator(INamedTypeSymbol command, ITypeSymbol type)
{
return type
.AllInterfaces.Where(i => i.GetFullNamespaceName() == ValidatorTypeName)
.Where(i => SymbolEqualityComparer.Default.Equals(i.TypeArguments.First(), command))
.Any();
}
}