-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFullEntityImageAnalyzer.cs
More file actions
47 lines (38 loc) · 1.57 KB
/
Copy pathFullEntityImageAnalyzer.cs
File metadata and controls
47 lines (38 loc) · 1.57 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
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using System.Collections.Immutable;
namespace XrmPluginCore.SourceGenerator.Analyzers;
/// <summary>
/// Analyzer that reports XPC3005 when WithPreImage or WithPostImage is called
/// without specifying any attributes, resulting in a full entity image registration.
/// </summary>
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class FullEntityImageAnalyzer : DiagnosticAnalyzer
{
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics =>
ImmutableArray.Create(DiagnosticDescriptors.FullEntityImage);
public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.EnableConcurrentExecution();
context.RegisterSyntaxNodeAction(AnalyzeInvocation, SyntaxKind.InvocationExpression);
}
private void AnalyzeInvocation(SyntaxNodeAnalysisContext context)
{
var invocation = (InvocationExpressionSyntax)context.Node;
if (invocation.Expression is not MemberAccessExpressionSyntax memberAccess)
return;
var methodName = memberAccess.Name.Identifier.Text;
if (methodName != Constants.WithPreImageMethodName && methodName != Constants.WithPostImageMethodName)
return;
// Only report when called with no arguments (full entity image)
if (invocation.ArgumentList.Arguments.Count > 0)
return;
context.ReportDiagnostic(Diagnostic.Create(
DiagnosticDescriptors.FullEntityImage,
invocation.GetLocation(),
methodName));
}
}