From f64b5e9a501142064fca6e4c123b8e5216ec2355 Mon Sep 17 00:00:00 2001 From: Timothy Makkison Date: Mon, 23 Sep 2024 22:03:52 +0100 Subject: [PATCH] refactor: move diagnostics to dedicated class --- .../DiagnosticDescriptors.cs | 29 +++++++++++++++++++ .../InterfaceStubGenerator.Shared.projitems | 1 + .../InterfaceStubGenerator.cs | 26 ++--------------- 3 files changed, 32 insertions(+), 24 deletions(-) create mode 100644 InterfaceStubGenerator.Shared/DiagnosticDescriptors.cs diff --git a/InterfaceStubGenerator.Shared/DiagnosticDescriptors.cs b/InterfaceStubGenerator.Shared/DiagnosticDescriptors.cs new file mode 100644 index 000000000..3052ff8cb --- /dev/null +++ b/InterfaceStubGenerator.Shared/DiagnosticDescriptors.cs @@ -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 +} + diff --git a/InterfaceStubGenerator.Shared/InterfaceStubGenerator.Shared.projitems b/InterfaceStubGenerator.Shared/InterfaceStubGenerator.Shared.projitems index 5589f69c2..7b3b9302c 100644 --- a/InterfaceStubGenerator.Shared/InterfaceStubGenerator.Shared.projitems +++ b/InterfaceStubGenerator.Shared/InterfaceStubGenerator.Shared.projitems @@ -9,6 +9,7 @@ InterfaceStubGenerator.Shared + diff --git a/InterfaceStubGenerator.Shared/InterfaceStubGenerator.cs b/InterfaceStubGenerator.Shared/InterfaceStubGenerator.cs index 6b0a236c9..2995c7c31 100644 --- a/InterfaceStubGenerator.Shared/InterfaceStubGenerator.cs +++ b/InterfaceStubGenerator.Shared/InterfaceStubGenerator.cs @@ -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 /// @@ -115,7 +93,7 @@ ImmutableArray candidateInterfaces if (httpMethodBaseAttributeSymbol == null) { - reportDiagnostic(context, Diagnostic.Create(RefitNotReferenced, null)); + reportDiagnostic(context, Diagnostic.Create(DiagnosticDescriptors.RefitNotReferenced, null)); return; } @@ -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