-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #466 from TimeWarpEngineering/Cramer/2024-08-04/Ac…
…tionSetMethodSourceGen Add Analyzer to make sure States implement specific CRTP pattern
- Loading branch information
Showing
3 changed files
with
54 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
Source/TimeWarp.State.Analyzer/StateInheritanceAnalyzer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
namespace TimeWarp.State.Analyzer; | ||
|
||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using System.Collections.Immutable; | ||
|
||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public class StateInheritanceAnalyzer : DiagnosticAnalyzer | ||
{ | ||
public const string DiagnosticId = "StateInheritanceRule"; | ||
|
||
private static readonly LocalizableString Title = "Incorrect State<T> inheritance"; | ||
private static readonly LocalizableString MessageFormat = "The type argument for State<T> must be the derived class itself"; | ||
private static readonly LocalizableString Description = "When inheriting from State<T>, T must be the name of the derived class."; | ||
private const string Category = "Design"; | ||
|
||
private static readonly DiagnosticDescriptor Rule = new(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Error, isEnabledByDefault: true, description: Description); | ||
|
||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get { return ImmutableArray.Create(Rule); } } | ||
|
||
public override void Initialize(AnalysisContext context) | ||
{ | ||
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); | ||
context.EnableConcurrentExecution(); | ||
context.RegisterSyntaxNodeAction(AnalyzeNode, SyntaxKind.ClassDeclaration); | ||
} | ||
|
||
private static void AnalyzeNode(SyntaxNodeAnalysisContext context) | ||
{ | ||
var classDeclaration = (ClassDeclarationSyntax)context.Node; | ||
BaseTypeSyntax? baseType = classDeclaration.BaseList?.Types.FirstOrDefault(); | ||
|
||
if (baseType == null) return; | ||
|
||
var baseTypeSymbol = context.SemanticModel.GetSymbolInfo(baseType.Type).Symbol as INamedTypeSymbol; | ||
if (baseTypeSymbol == null || !baseTypeSymbol.Name.Equals("State")) return; | ||
|
||
ITypeSymbol? typeArg = baseTypeSymbol.TypeArguments.FirstOrDefault(); | ||
if (typeArg == null) return; | ||
|
||
INamedTypeSymbol? derivedTypeSymbol = context.SemanticModel.GetDeclaredSymbol(classDeclaration); | ||
if (derivedTypeSymbol == null) return; | ||
|
||
if (!SymbolEqualityComparer.Default.Equals(typeArg, derivedTypeSymbol)) | ||
{ | ||
var diagnostic = Diagnostic.Create(Rule, classDeclaration.Identifier.GetLocation()); | ||
context.ReportDiagnostic(diagnostic); | ||
} | ||
} | ||
} |