-
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 #467 from TimeWarpEngineering/Cramer/2024-08-13/St…
…ateReadOnlyPublicPropertiesAnalyzer Add state read only public properties analyzer
- Loading branch information
Showing
8 changed files
with
201 additions
and
9 deletions.
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
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,5 @@ | ||
global using Microsoft.CodeAnalysis; | ||
global using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
global using Microsoft.CodeAnalysis.Diagnostics; | ||
global using System.Collections.Immutable; | ||
global using System.Linq; |
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
73 changes: 73 additions & 0 deletions
73
Source/TimeWarp.State.Analyzer/StateReadOnlyPublicPropertiesAnalyzer.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,73 @@ | ||
namespace TimeWarp.State.Analyzer; | ||
|
||
using Microsoft.CodeAnalysis.CSharp; | ||
|
||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public class StateReadOnlyPublicPropertiesAnalyzer : DiagnosticAnalyzer | ||
{ | ||
public const string DiagnosticId = "StateReadOnlyPublicPropertiesRule"; | ||
|
||
private static readonly LocalizableString Title = "Public property in State class should be read-only"; | ||
private static readonly LocalizableString MessageFormat = "The public property '{0}' in State-derived class should be read-only"; | ||
private static readonly LocalizableString Description = "Public properties in classes inheriting from State should be read-only to enforce immutability."; | ||
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; | ||
|
||
if (!InheritsFromState(classDeclaration, context.SemanticModel)) | ||
return; | ||
|
||
foreach (MemberDeclarationSyntax member in classDeclaration.Members) | ||
{ | ||
if (member is PropertyDeclarationSyntax propertyDeclaration) | ||
{ | ||
AnalyzeProperty(propertyDeclaration, context); | ||
} | ||
} | ||
} | ||
|
||
private static bool InheritsFromState(ClassDeclarationSyntax classDeclaration, SemanticModel semanticModel) | ||
{ | ||
INamedTypeSymbol? classSymbol = semanticModel.GetDeclaredSymbol(classDeclaration); | ||
if (classSymbol == null) | ||
return false; | ||
|
||
INamedTypeSymbol? baseType = classSymbol.BaseType; | ||
while (baseType != null) | ||
{ | ||
if (baseType.Name == "State" && baseType.TypeArguments.Length == 1) | ||
return true; | ||
baseType = baseType.BaseType; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private static void AnalyzeProperty(PropertyDeclarationSyntax propertyDeclaration, SyntaxNodeAnalysisContext context) | ||
{ | ||
if (!propertyDeclaration.Modifiers.Any(SyntaxKind.PublicKeyword)) return; | ||
|
||
AccessorDeclarationSyntax? setter = | ||
propertyDeclaration.AccessorList?.Accessors | ||
.FirstOrDefault(a => a.IsKind(SyntaxKind.SetAccessorDeclaration)); | ||
|
||
if (setter != null && !setter.Modifiers.Any(SyntaxKind.PrivateKeyword)) | ||
{ | ||
var diagnostic = Diagnostic.Create(Rule, propertyDeclaration.Identifier.GetLocation(), propertyDeclaration.Identifier.Text); | ||
context.ReportDiagnostic(diagnostic); | ||
} | ||
} | ||
} |
4 changes: 0 additions & 4 deletions
4
Source/TimeWarp.State.Analyzer/TimeWarpStateActionAnalyzer.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
121 changes: 121 additions & 0 deletions
121
Tests/TimeWarp.State.Analyzer.Tests/StateReadOnlyPublicPropertiesAnalyzer_Tests.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,121 @@ | ||
// // ReSharper disable InconsistentNaming | ||
// namespace StateReadOnlyPublicPropertiesAnalyzer_; | ||
// | ||
// public class Should_Trigger_StateReadOnlyPublicPropertiesRule | ||
// { | ||
// public static async Task Given_PublicPropertyWithPublicSetter() | ||
// { | ||
// const string TestCode = | ||
// """ | ||
// using System.Threading.Tasks; | ||
// using TimeWarp.State; | ||
// | ||
// public class SampleState : State<SampleState> | ||
// { | ||
// public int PublicProperty { get; set; } | ||
// | ||
// public override void Initialize() { } | ||
// } | ||
// """; | ||
// | ||
// var expectedDiagnostic = new DiagnosticResult("StateReadOnlyPublicPropertiesRule", DiagnosticSeverity.Warning) | ||
// .WithSpan(6, 16, 6, 30) | ||
// .WithArguments("PublicProperty"); | ||
// | ||
// var analyzerTest = new CSharpAnalyzerTest<StateReadOnlyPublicPropertiesAnalyzer, FixieVerifier> | ||
// { | ||
// TestCode = TestCode | ||
// }; | ||
// | ||
// analyzerTest.ExpectedDiagnostics.Add(expectedDiagnostic); | ||
// | ||
// const string TimeWarpStateAssemblyPath = @"TimeWarp.State.dll"; | ||
// analyzerTest.TestState.AdditionalReferences.Add(MetadataReference.CreateFromFile(TimeWarpStateAssemblyPath)); | ||
// | ||
// await analyzerTest.RunAsync(); | ||
// } | ||
// | ||
// public static async Task Given_PublicPropertyWithProtectedSetter() | ||
// { | ||
// const string TestCode = | ||
// """ | ||
// using System.Threading.Tasks; | ||
// using TimeWarp.State; | ||
// | ||
// public class SampleState : State<SampleState> | ||
// { | ||
// public int PublicProperty { get; protected set; } | ||
// | ||
// public override void Initialize() { } | ||
// } | ||
// """; | ||
// | ||
// var expectedDiagnostic = new DiagnosticResult("StateReadOnlyPublicPropertiesRule", DiagnosticSeverity.Warning) | ||
// .WithSpan(6, 16, 6, 30) | ||
// .WithArguments("PublicProperty"); | ||
// | ||
// var analyzerTest = new CSharpAnalyzerTest<StateReadOnlyPublicPropertiesAnalyzer, FixieVerifier> | ||
// { | ||
// TestCode = TestCode | ||
// }; | ||
// | ||
// analyzerTest.ExpectedDiagnostics.Add(expectedDiagnostic); | ||
// | ||
// const string TimeWarpStateAssemblyPath = @"TimeWarp.State.dll"; | ||
// analyzerTest.TestState.AdditionalReferences.Add(MetadataReference.CreateFromFile(TimeWarpStateAssemblyPath)); | ||
// | ||
// await analyzerTest.RunAsync(); | ||
// } | ||
// | ||
// public static async Task Given_PublicPropertyWithPrivateSetter() | ||
// { | ||
// const string TestCode = | ||
// """ | ||
// using System.Threading.Tasks; | ||
// using TimeWarp.State; | ||
// | ||
// public class SampleState : State<SampleState> | ||
// { | ||
// public int PublicProperty { get; private set; } | ||
// | ||
// public override void Initialize() { } | ||
// } | ||
// """; | ||
// | ||
// var analyzerTest = new CSharpAnalyzerTest<StateReadOnlyPublicPropertiesAnalyzer, FixieVerifier> | ||
// { | ||
// TestCode = TestCode | ||
// }; | ||
// | ||
// const string TimeWarpStateAssemblyPath = @"TimeWarp.State.dll"; | ||
// analyzerTest.TestState.AdditionalReferences.Add(MetadataReference.CreateFromFile(TimeWarpStateAssemblyPath)); | ||
// | ||
// await analyzerTest.RunAsync(); | ||
// } | ||
// | ||
// public static async Task Given_PublicReadOnlyProperty() | ||
// { | ||
// const string TestCode = | ||
// """ | ||
// using System.Threading.Tasks; | ||
// using TimeWarp.State; | ||
// | ||
// public class SampleState : State<SampleState> | ||
// { | ||
// public int PublicProperty { get; } | ||
// | ||
// public override void Initialize() { } | ||
// } | ||
// """; | ||
// | ||
// var analyzerTest = new CSharpAnalyzerTest<StateReadOnlyPublicPropertiesAnalyzer, FixieVerifier> | ||
// { | ||
// TestCode = TestCode | ||
// }; | ||
// | ||
// const string TimeWarpStateAssemblyPath = @"TimeWarp.State.dll"; | ||
// analyzerTest.TestState.AdditionalReferences.Add(MetadataReference.CreateFromFile(TimeWarpStateAssemblyPath)); | ||
// | ||
// await analyzerTest.RunAsync(); | ||
// } | ||
// } |
File renamed without changes.