-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathEntityBaseClassWithIdTypeGenerationAnalyzer.cs
More file actions
64 lines (51 loc) · 2.75 KB
/
EntityBaseClassWithIdTypeGenerationAnalyzer.cs
File metadata and controls
64 lines (51 loc) · 2.75 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
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
namespace Architect.DomainModeling.Analyzer.Analyzers;
/// <summary>
/// Encourages migrating from Entity<TId, TPrimitive> to EntityAttribute<TId, TIdUnderlying>.
/// </summary>
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class EntityBaseClassWithIdTypeGenerationAnalyzer : DiagnosticAnalyzer
{
public const string DiagnosticId = "EntityBaseClassWithIdTypeGeneration";
[System.Diagnostics.CodeAnalysis.SuppressMessage("CodeQuality", "IDE0079:Remove unnecessary suppression", Justification = "False positive.")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("MicrosoftCodeAnalysisReleaseTracking", "RS2008:Enable analyzer release tracking", Justification = "Not yet implemented.")]
private static readonly DiagnosticDescriptor DiagnosticDescriptor = new DiagnosticDescriptor(
id: "EntityBaseClassWithIdTypeGeneration",
title: "Used entity base class instead of attribute to initiate ID type source generation",
messageFormat: "Entity<TId, TIdPrimitive> is deprecated in favor of the [Entity<TId, TIdUnderlying>] attribute. Use the extended attribute and remove TIdPrimitive from the base class.",
category: "Design",
defaultSeverity: DiagnosticSeverity.Warning,
isEnabledByDefault: true);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => [DiagnosticDescriptor];
public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.EnableConcurrentExecution();
context.RegisterSyntaxNodeAction(AnalyzeClassDeclaration, SyntaxKind.ClassDeclaration);
}
private static void AnalyzeClassDeclaration(SyntaxNodeAnalysisContext context)
{
var classDeclaration = (ClassDeclarationSyntax)context.Node;
// Get the first base type (the actual base class rather than interfaces)
if (classDeclaration.BaseList is not { Types: { Count: > 0 } baseTypes } || baseTypes[0] is not { } baseType)
return;
var typeInfo = context.SemanticModel.GetTypeInfo(baseType.Type, context.CancellationToken);
if (typeInfo.Type is not INamedTypeSymbol baseTypeSymbol)
return;
while (baseTypeSymbol is not null)
{
if (baseTypeSymbol is { Arity: 2, Name: "Entity", ContainingNamespace: { Name: "DomainModeling", ContainingNamespace: { Name: "Architect", ContainingNamespace.IsGlobalNamespace: true, } } })
break;
baseTypeSymbol = baseTypeSymbol.BaseType!;
}
// If Entity<TId, TIdPrimitive>
if (baseTypeSymbol is null)
return;
var diagnostic = Diagnostic.Create(DiagnosticDescriptor, baseType.GetLocation());
context.ReportDiagnostic(diagnostic);
}
}