Skip to content

Commit

Permalink
refactor: move diagnostics to dedicated class (#1842)
Browse files Browse the repository at this point in the history
Co-authored-by: Chris Pulman <chris.pulman@yahoo.com>
  • Loading branch information
TimothyMakkison and ChrisPulman authored Sep 24, 2024
1 parent fd0dd65 commit 1869ca6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 24 deletions.
29 changes: 29 additions & 0 deletions InterfaceStubGenerator.Shared/DiagnosticDescriptors.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Microsoft.CodeAnalysis;

namespace Refit.Generator;

internal static class DiagnosticDescriptors
{
#pragma warning disable RS2008 // Enable analyzer release tracking
public static readonly DiagnosticDescriptor InvalidRefitMember =
new(
"RF001",
"Refit types must have Refit HTTP method attributes",
"Method {0}.{1} either has no Refit HTTP method attribute or you've used something other than a string literal for the 'path' argument",
"Refit",
DiagnosticSeverity.Warning,
true
);

public static readonly DiagnosticDescriptor RefitNotReferenced =
new(
"RF002",
"Refit must be referenced",
"Refit is not referenced. Add a reference to Refit.",
"Refit",
DiagnosticSeverity.Error,
true
);
#pragma warning restore RS2008 // Enable analyzer release tracking
}

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<Import_RootNamespace>InterfaceStubGenerator.Shared</Import_RootNamespace>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)DiagnosticDescriptors.cs" />
<Compile Include="$(MSBuildThisFileDirectory)InterfaceStubGenerator.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ITypeSymbolExtensions.cs" />
</ItemGroup>
Expand Down
26 changes: 2 additions & 24 deletions InterfaceStubGenerator.Shared/InterfaceStubGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,6 @@ public class InterfaceStubGenerator : ISourceGenerator
{
private const string TypeParameterVariableName = "______typeParameters";

#pragma warning disable RS2008 // Enable analyzer release tracking
static readonly DiagnosticDescriptor InvalidRefitMember =
new(
"RF001",
"Refit types must have Refit HTTP method attributes",
"Method {0}.{1} either has no Refit HTTP method attribute or you've used something other than a string literal for the 'path' argument",
"Refit",
DiagnosticSeverity.Warning,
true
);

static readonly DiagnosticDescriptor RefitNotReferenced =
new(
"RF002",
"Refit must be referenced",
"Refit is not referenced. Add a reference to Refit.",
"Refit",
DiagnosticSeverity.Error,
true
);
#pragma warning restore RS2008 // Enable analyzer release tracking

#if !ROSLYN_4

/// <summary>
Expand Down Expand Up @@ -115,7 +93,7 @@ ImmutableArray<InterfaceDeclarationSyntax> candidateInterfaces

if (httpMethodBaseAttributeSymbol == null)
{
reportDiagnostic(context, Diagnostic.Create(RefitNotReferenced, null));
reportDiagnostic(context, Diagnostic.Create(DiagnosticDescriptors.RefitNotReferenced, null));

Check warning on line 96 in InterfaceStubGenerator.Shared/InterfaceStubGenerator.cs

View workflow job for this annotation

GitHub Actions / build / build

In externally visible method 'void InterfaceStubGenerator.GenerateInterfaceStubs<TContext>(TContext context, Action<TContext, Diagnostic> reportDiagnostic, Action<TContext, string, SourceText> addSource, CSharpCompilation compilation, string? refitInternalNamespace, ImmutableArray<MethodDeclarationSyntax> candidateMethods, ImmutableArray<InterfaceDeclarationSyntax> candidateInterfaces)', validate parameter 'reportDiagnostic' is non-null before using it. If appropriate, throw an 'ArgumentNullException' when the argument is 'null'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1062)

Check warning on line 96 in InterfaceStubGenerator.Shared/InterfaceStubGenerator.cs

View workflow job for this annotation

GitHub Actions / build / build

In externally visible method 'void InterfaceStubGeneratorV2.GenerateInterfaceStubs<TContext>(TContext context, Action<TContext, Diagnostic> reportDiagnostic, Action<TContext, string, SourceText> addSource, CSharpCompilation compilation, string? refitInternalNamespace, ImmutableArray<MethodDeclarationSyntax> candidateMethods, ImmutableArray<InterfaceDeclarationSyntax> candidateInterfaces)', validate parameter 'reportDiagnostic' is non-null before using it. If appropriate, throw an 'ArgumentNullException' when the argument is 'null'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1062)
return;
}

Expand Down Expand Up @@ -640,7 +618,7 @@ IMethodSymbol methodSymbol
foreach (var location in methodSymbol.Locations)
{
var diagnostic = Diagnostic.Create(
InvalidRefitMember,
DiagnosticDescriptors.InvalidRefitMember,
location,
methodSymbol.ContainingType.Name,
methodSymbol.Name
Expand Down

0 comments on commit 1869ca6

Please sign in to comment.