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

feat: add analyzer for direct command handler calls #607

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
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
7 changes: 7 additions & 0 deletions Stl.Fusion.sln
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MiniRpc", "samples\MiniRpc\
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MultiServerRpc", "samples\MultiServerRpc\MultiServerRpc.csproj", "{DDD38FE8-3105-44E6-9128-921B0713FF64}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Stl.Analyzers", "src\Stl.Analyzers\Stl.Analyzers.csproj", "{A36C2D5F-65EA-4170-ABE4-7CD9A15B381E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -250,6 +252,10 @@ Global
{DDD38FE8-3105-44E6-9128-921B0713FF64}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DDD38FE8-3105-44E6-9128-921B0713FF64}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DDD38FE8-3105-44E6-9128-921B0713FF64}.Release|Any CPU.Build.0 = Release|Any CPU
{A36C2D5F-65EA-4170-ABE4-7CD9A15B381E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A36C2D5F-65EA-4170-ABE4-7CD9A15B381E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A36C2D5F-65EA-4170-ABE4-7CD9A15B381E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A36C2D5F-65EA-4170-ABE4-7CD9A15B381E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -289,6 +295,7 @@ Global
{5A8530A3-E69B-4BD3-B7A7-01A916D166EB} = {A50B98CF-9AA1-4622-B2AD-5E17610115A7}
{99131CAD-DDC3-4B1A-ABB8-63E0FF32B03E} = {61E78666-E338-4F6B-8CD8-8CD5E13B7C54}
{DDD38FE8-3105-44E6-9128-921B0713FF64} = {61E78666-E338-4F6B-8CD8-8CD5E13B7C54}
{A36C2D5F-65EA-4170-ABE4-7CD9A15B381E} = {A50B98CF-9AA1-4622-B2AD-5E17610115A7}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B50610D6-2B28-4258-98A2-A0B486FD5907}
Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
</ItemGroup>

<!-- Global usings -->
<ItemGroup Condition="!($(MSBuildProjectFile.Contains('Generators')))">
<ItemGroup Condition="!($(MSBuildProjectFile.Contains('Generators'))) and !($(MSBuildProjectFile.Contains('Analyzers')))">
<Using Include="System.Console" Static="True" />
<Using Include="System.Collections" />
<Using Include="System.Collections.Immutable" />
Expand Down
71 changes: 71 additions & 0 deletions src/Stl.Analyzers/CommandHandlerAnalyzer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;

namespace Stl.Analyzers;

[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class CommandHandlerAnalyzer : DiagnosticAnalyzer
{
private static readonly DiagnosticDescriptor DirectCallDiagnostic =
new(
"STLC0001",
"Invalid command handler call",
"Direct command handler calls on command service proxies are not allowed",
"Test",
DiagnosticSeverity.Warning,
isEnabledByDefault: true
);

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } =
ImmutableArray.Create(DirectCallDiagnostic);

public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
context.EnableConcurrentExecution();

context.RegisterSyntaxNodeAction(CheckCommandHandler, SyntaxKind.InvocationExpression);
}

private void CheckCommandHandler(SyntaxNodeAnalysisContext context)
{
if (context.Node is not InvocationExpressionSyntax invocation)
return;

if (context.SemanticModel.GetSymbolInfo(invocation, cancellationToken: context.CancellationToken).Symbol is not IMethodSymbol methodSymbol)
return;

var syntaxReference = methodSymbol.DeclaringSyntaxReferences.FirstOrDefault();

if (syntaxReference is null)
return;

SyntaxNode declaration = syntaxReference.GetSyntax(context.CancellationToken);

if (declaration is not MethodDeclarationSyntax method)
return;

// check if the called method has the CommandHandler attribute
// this is a very naive implementation, the namespace should be checked as well
bool isCommandHandler = method.AttributeLists
.SelectMany(x => x.Attributes)
.Any(
attribute =>
string.Equals(
attribute.Name.ToString(),
"CommandHandler",
StringComparison.Ordinal
)
);

if (!isCommandHandler)
return;

var diagnostic = Diagnostic.Create(DirectCallDiagnostic, context.Node.GetLocation());

context.ReportDiagnostic(diagnostic);
}
}
16 changes: 16 additions & 0 deletions src/Stl.Analyzers/Stl.Analyzers.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net7.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
<Description>Stl.Analyzers - Roslyn analyzers for common issues.</Description>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" />
</ItemGroup>

</Project>