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

refactor: move diagnostics to dedicated class #1842

Merged
merged 2 commits into from
Sep 24, 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
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 @@
{
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 @@ -89,7 +67,7 @@
/// <param name="candidateMethods">The candidate methods.</param>
/// <param name="candidateInterfaces">The candidate interfaces.</param>
/// <returns></returns>
public void GenerateInterfaceStubs<TContext>(

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

View workflow job for this annotation

GitHub Actions / build / build

Member 'GenerateInterfaceStubs' does not access instance data and can be marked as static (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1822)
TContext context,
Action<TContext, Diagnostic> reportDiagnostic,
Action<TContext, string, SourceText> addSource,
Expand All @@ -104,7 +82,7 @@

// we're going to create a new compilation that contains the attribute.
// TODO: we should allow source generators to provide source during initialize, so that this step isn't required.
var options = (CSharpParseOptions)compilation.SyntaxTrees[0].Options;

Check warning on line 85 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 'compilation' 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)

var disposableInterfaceSymbol = compilation.GetTypeByMetadataName(
"System.IDisposable"
Expand All @@ -115,7 +93,7 @@

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)
return;
}

Expand Down Expand Up @@ -221,7 +199,7 @@
);

// add the attribute text
addSource(

Check warning on line 202 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 'addSource' 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)
context,
"PreserveAttribute.g.cs",
SourceText.From(attributeText, Encoding.UTF8)
Expand Down Expand Up @@ -640,7 +618,7 @@
foreach (var location in methodSymbol.Locations)
{
var diagnostic = Diagnostic.Create(
InvalidRefitMember,
DiagnosticDescriptors.InvalidRefitMember,
location,
methodSymbol.ContainingType.Name,
methodSymbol.Name
Expand Down Expand Up @@ -868,7 +846,7 @@
context.RegisterForSyntaxNotifications(() => new SyntaxReceiver());
}

class SyntaxReceiver : ISyntaxReceiver

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

View workflow job for this annotation

GitHub Actions / build / build

Type 'SyntaxReceiver' can be sealed because it has no subtypes in its containing assembly and is not externally visible (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1852)
{
public List<MethodDeclarationSyntax> CandidateMethods { get; } = new();

Expand Down
Loading