Skip to content

Commit

Permalink
Add diagnostic if Handle method is not private (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
viceroypenguin authored Mar 25, 2024
1 parent 363b547 commit d532a15
Show file tree
Hide file tree
Showing 20 changed files with 80 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ IHR0007 | ImmediateHandler | Error | BehaviorsAnalyzer
IHR0008 | ImmediateHandler | Error | BehaviorsAnalyzer
IHR0009 | ImmediateHandler | Error | HandlerClassAnalyzer
IHR0010 | ImmediateHandler | Error | HandlerClassAnalyzer
IHR0011 | ImmediateHandler | Error | HandlerClassAnalyzer
1 change: 1 addition & 0 deletions src/Immediate.Handlers.Analyzers/DiagnosticIds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ internal static class DiagnosticIds
public const string IHR0008BehaviorsMustUseUnboundGenerics = "IHR0008";
public const string IHR0009HandlerMethodMustBeStatic = "IHR0009";
public const string IHR0010HandlerMethodMustBeUnique = "IHR0010";
public const string IHR0011HandlerMethodMustBePrivate = "IHR0011";
}
26 changes: 25 additions & 1 deletion src/Immediate.Handlers.Analyzers/HandlerClassAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ public sealed class HandlerClassAnalyzer : DiagnosticAnalyzer
description: "Static handler method must be static."
);

private static readonly DiagnosticDescriptor HandlerMethodMustBePrivate =
new(
id: DiagnosticIds.IHR0011HandlerMethodMustBePrivate,
title: "Handler method must be private",
messageFormat: "Method '{0}' must not conflict with another static handler method",
category: "ImmediateHandler",
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "Static handler method must be static."
);

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } =
ImmutableArray.Create(
[
Expand All @@ -70,6 +81,7 @@ public sealed class HandlerClassAnalyzer : DiagnosticAnalyzer
HandlerMustNotBeNestedInAnotherClass,
HandlerMethodMustBeStatic,
HandlerMethodMustBeUnique,
HandlerMethodMustBePrivate,
]);

public override void Initialize(AnalysisContext context)
Expand Down Expand Up @@ -164,7 +176,19 @@ private static void AnalyzeSymbol(SymbolAnalysisContext context)
Diagnostic.Create(
HandlerMethodMustReturnTask,
methodSymbol.Locations[0],
methodSymbol.Name)
methodSymbol.Name
)
);
}

if (methodSymbol.DeclaredAccessibility is not Accessibility.Private)
{
context.ReportDiagnostic(
Diagnostic.Create(
HandlerMethodMustBePrivate,
methodSymbol.Locations[0],
methodSymbol.Name
)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ public static class AnalyzerTestHelpers
{
public static CSharpAnalyzerTest<TAnalyzer, DefaultVerifier> CreateAnalyzerTest<TAnalyzer>(
string inputSource,
DriverReferenceAssemblies assemblies,
IEnumerable<DiagnosticResult> expectedDiagnostics
)
DriverReferenceAssemblies assemblies)
where TAnalyzer : DiagnosticAnalyzer, new()
{
var csTest = new CSharpAnalyzerTest<TAnalyzer, DefaultVerifier>
Expand All @@ -31,9 +29,6 @@ IEnumerable<DiagnosticResult> expectedDiagnostics
csTest.TestState.AdditionalReferences
.AddRange(assemblies.GetAdditionalReferences());

csTest.TestState.ExpectedDiagnostics
.AddRange(expectedDiagnostics);

return csTest;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ private static ValueTask<IEnumerable<User>> HandleAsync(
}
}
""",
DriverReferenceAssemblies.Normal,
[]
DriverReferenceAssemblies.Normal
).RunAsync();
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ private static ValueTask<IEnumerable<User>> HandleAsync(
}
}
""",
DriverReferenceAssemblies.Normal,
[]
DriverReferenceAssemblies.Normal
).RunAsync();
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ private static ValueTask<IEnumerable<User>> HandleAsync(
}
}
""",
DriverReferenceAssemblies.Normal,
[]
DriverReferenceAssemblies.Normal
).RunAsync();
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ private static ValueTask<IEnumerable<User>> HandleAsync(
}
}
""",
DriverReferenceAssemblies.Normal,
[]
DriverReferenceAssemblies.Normal
).RunAsync();
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ private static ValueTask<IEnumerable<User>> HandleAsync(
}
}
""",
DriverReferenceAssemblies.Normal,
[]
DriverReferenceAssemblies.Normal
).RunAsync();
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public static class {|IHR0001:GetUsersQuery|}
public record Query;
}
""",
DriverReferenceAssemblies.Normal,
[]
DriverReferenceAssemblies.Normal
).RunAsync();
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public ValueTask<IEnumerable<User>> GetUsers()
public interface ILogger<T>;
""",
DriverReferenceAssemblies.Normal,
[]
DriverReferenceAssemblies.Normal
).RunAsync();
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ private ValueTask<int> Handle(
}
}
""",
DriverReferenceAssemblies.Normal,
[]
DriverReferenceAssemblies.Normal
).RunAsync();
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ private async ValueTask Handle(
}
}
""",
DriverReferenceAssemblies.Normal,
[]
DriverReferenceAssemblies.Normal
).RunAsync();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Immediate.Handlers.Analyzers;
using Immediate.Handlers.Tests.Helpers;

namespace Immediate.Handlers.Tests.AnalyzerTests.HandlerClassAnalyzerTests;

[System.Diagnostics.CodeAnalysis.SuppressMessage("Naming", "CA1724:Type names should not match namespaces", Justification = "Not being consumed by other code")]
public partial class Tests
{
[Fact]
public async Task HandleMethodIsNotPrivate_AlertDiagnostic() =>
await AnalyzerTestHelpers.CreateAnalyzerTest<HandlerClassAnalyzer>(
"""
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Immediate.Handlers.Shared;
[Handler]
public class GetUsersQuery
{
public record Query;
public static ValueTask<int> {|IHR0011:HandleAsync|}(
Query _,
CancellationToken token)
{
return ValueTask.FromResult(0);
}
}
""",
DriverReferenceAssemblies.Normal
).RunAsync();
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ public ValueTask<IEnumerable<User>> GetUsers()
public interface ILogger<T>;
""",
DriverReferenceAssemblies.Normal,
[]
DriverReferenceAssemblies.Normal
).RunAsync();
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public record Query;
}
}
""",
DriverReferenceAssemblies.Normal,
[]
DriverReferenceAssemblies.Normal
).RunAsync();
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ private static ValueTask<int> HandleAsync(
}
}
""",
DriverReferenceAssemblies.Normal,
[]
DriverReferenceAssemblies.Normal
).RunAsync();
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ private static ValueTask<int> HandleAsync(
}
}
""",
DriverReferenceAssemblies.Normal,
[]
DriverReferenceAssemblies.Normal
).RunAsync();
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ private static ValueTask<int> HandleAsync(
}
}
""",
DriverReferenceAssemblies.Normal,
[]
DriverReferenceAssemblies.Normal
).RunAsync();
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ private static ValueTask<int> HandleAsync(
}
}
""",
DriverReferenceAssemblies.Normal,
[]
DriverReferenceAssemblies.Normal
).RunAsync();
}

0 comments on commit d532a15

Please sign in to comment.