Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn users if not using CancellationToken #93

Merged
merged 3 commits into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1 @@
* text=auto eol=lf
* text=auto
21 changes: 15 additions & 6 deletions src/Immediate.Handlers.Analyzers/AnalyzerReleases.Shipped.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,21 @@

Rule ID | Category | Severity | Notes
--------|----------|----------|--------------------
IHR0001 | ImmediateHandler | Error | HandlerClassAnalyzer
IHR0002 | ImmediateHandler | Error | HandlerClassAnalyzer
IHR0005 | ImmediateHandler | Error | HandlerClassAnalyzer
IHR0006 | ImmediateHandler | Error | BehaviorsAnalyzer
IHR0007 | ImmediateHandler | Error | BehaviorsAnalyzer
IHR0008 | ImmediateHandler | Error | BehaviorsAnalyzer
IHR0001 | ImmediateHandler | Error | HandlerClassAnalyzer
IHR0002 | ImmediateHandler | Error | HandlerClassAnalyzer
IHR0005 | ImmediateHandler | Error | HandlerClassAnalyzer
IHR0006 | ImmediateHandler | Error | BehaviorsAnalyzer
IHR0007 | ImmediateHandler | Error | BehaviorsAnalyzer
IHR0008 | ImmediateHandler | Error | BehaviorsAnalyzer
IHR0009 | ImmediateHandler | Error | HandlerClassAnalyzer
IHR0010 | ImmediateHandler | Error | HandlerClassAnalyzer
IHR0011 | ImmediateHandler | Error | HandlerClassAnalyzer


## Release 1.5

### New Rules

Rule ID | Category | Severity | Notes
--------|----------|----------|--------------------
IHR0012 | ImmediateHandler | Warning | 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 IHR0009HandlerMethodMustBeStatic = "IHR0009";
public const string IHR0010HandlerMethodMustBeUnique = "IHR0010";
public const string IHR0011HandlerMethodMustBePrivate = "IHR0011";
public const string IHR0012HandlerShouldUseCancellationToken = "IHR0012";
}
23 changes: 23 additions & 0 deletions src/Immediate.Handlers.Analyzers/HandlerClassAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ public sealed class HandlerClassAnalyzer : DiagnosticAnalyzer
description: "Static handler method must be static."
);

public static readonly DiagnosticDescriptor HandlerShouldUseCancellationToken =
new(
id: DiagnosticIds.IHR0012HandlerShouldUseCancellationToken,
title: "Handler method should use CancellationToken",
messageFormat: "Method '{0}' should receive a `CancellationToken`",
category: "ImmediateHandler",
defaultSeverity: DiagnosticSeverity.Warning,
isEnabledByDefault: true,
description: "Handlers should use CancellationToken to properly support cancellation."
);

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

public override void Initialize(AnalysisContext context)
Expand Down Expand Up @@ -192,6 +204,17 @@ private static void AnalyzeSymbol(SymbolAnalysisContext context)
);
}

if (!methodSymbol.Parameters[^1].Type.IsCancellationToken())
{
context.ReportDiagnostic(
Diagnostic.Create(
HandlerShouldUseCancellationToken,
methodSymbol.Locations[0],
methodSymbol.Name
)
);
}

break;
}

Expand Down
15 changes: 15 additions & 0 deletions src/Immediate.Handlers.Analyzers/ITypeSymbolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,19 @@ typeSymbol is
public static bool ImplementsBehavior(this INamedTypeSymbol typeSymbol) =>
typeSymbol.IsBehavior2()
|| (typeSymbol.BaseType is not null && ImplementsBehavior(typeSymbol.BaseType.OriginalDefinition));

public static bool IsCancellationToken(this ITypeSymbol typeSymbol) =>
typeSymbol is INamedTypeSymbol
{
Name: "CancellationToken",
ContainingNamespace:
{
Name: "Threading",
ContainingNamespace:
{
Name: "System",
ContainingNamespace.IsGlobalNamespace: true
}
}
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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 HandleMethodWithoutCancellationToken_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;

private static ValueTask<int> {|IHR0012:HandleAsync|}(
Query _)
{
return ValueTask.FromResult(0);
}
}
""",
DriverReferenceAssemblies.Normal
).RunAsync();
}
Loading