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

Issue a warning for non-public commands #72

Merged
merged 1 commit into from
Nov 18, 2022
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
30 changes: 30 additions & 0 deletions src/Merq.CodeAnalysis.Tests/CommandInterfaceTests.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,43 @@
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Testing;
using Microsoft.CodeAnalysis.Testing;
using Microsoft.CodeAnalysis.Testing.Verifiers;
using Analyzer = Microsoft.CodeAnalysis.CSharp.Testing.CSharpAnalyzerVerifier<Merq.CommandInterfaceAnalyzer, Microsoft.CodeAnalysis.Testing.Verifiers.XUnitVerifier>;
using AnalyzerTest = Microsoft.CodeAnalysis.CSharp.Testing.CSharpAnalyzerTest<Merq.CommandInterfaceAnalyzer, Microsoft.CodeAnalysis.Testing.Verifiers.XUnitVerifier>;

namespace Merq;

public class CommandInterfaceTests
{
[Fact]
public async Task NonPublicCommand()
{
var test = new CSharpAnalyzerTest<PublicCommandAnalyzer, XUnitVerifier>
{
TestCode = """
using Merq;
using System;

record {|#0:Command|} : ICommand;

class Handler : ICommandHandler<Command>
{
public bool CanExecute(Command command) => true;
public void Execute(Command command) { }
}
"""
}.WithMerq();

var expected = Analyzer.Diagnostic(Diagnostics.CommandTypesShouldBePublic)
.WithLocation(0)
.WithArguments("Command");

test.ExpectedDiagnostics.Add(expected);

await test.RunAsync();
}

[Fact]
public async Task CommandMissingInterface()
{
Expand Down
12 changes: 12 additions & 0 deletions src/Merq.CodeAnalysis/Diagnostics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,16 @@ public static class Diagnostics
DiagnosticSeverity.Warning,
isEnabledByDefault: true,
description: "In order to support automatic hierarchical dynamic conversion for records, the Create method must be accessible within the compilation.");

/// <summary>
/// MERQ007: Consider making command types public.
/// </summary>
public static DiagnosticDescriptor CommandTypesShouldBePublic { get; } = new(
"MERQ007",
"Consider making command types public",
"Consider making command '{0}' public.",
"Design",
DiagnosticSeverity.Warning,
isEnabledByDefault: true,
description: "Public commands have better performance characteristics when dynamic conversion (aka 'duck-typing') is used.");
}
40 changes: 40 additions & 0 deletions src/Merq.CodeAnalysis/PublicCommandAnalyzer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;

namespace Merq;

[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class PublicCommandAnalyzer : DiagnosticAnalyzer
{
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } =
ImmutableArray.Create(Diagnostics.CommandTypesShouldBePublic);

public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.EnableConcurrentExecution();
context.RegisterSymbolAction(AnalyzeTypeSymbol, SymbolKind.NamedType);
}

static void AnalyzeTypeSymbol(SymbolAnalysisContext context)
{
var namedType = (INamedTypeSymbol)context.Symbol;
if (context.Compilation.GetTypeByMetadataName("Merq.ICommand") is not INamedTypeSymbol syncCmd ||
context.Compilation.GetTypeByMetadataName("Merq.IAsyncCommand") is not INamedTypeSymbol asyncCmd ||
context.Compilation.GetTypeByMetadataName("Merq.ICommand`1") is not INamedTypeSymbol syncCmdRet ||
context.Compilation.GetTypeByMetadataName("Merq.IAsyncCommand`1") is not INamedTypeSymbol asyncCmdRet)
return;

if (namedType.DeclaredAccessibility == Accessibility.Public)
return;

if (namedType.Is(syncCmd) || namedType.Is(asyncCmd) || namedType.Is(syncCmdRet) || namedType.Is(asyncCmdRet))
{
context.ReportDiagnostic(Diagnostic.Create(
Diagnostics.CommandTypesShouldBePublic,
namedType.Locations[0],
namedType.Name));
}
}
}