Skip to content

Commit

Permalink
Make ARGP0044 more generic
Browse files Browse the repository at this point in the history
  • Loading branch information
DoctorKrolic committed May 9, 2024
1 parent bd9cc23 commit 510ee8d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
namespace ArgumentParsing.Generators.Diagnostics.Analyzers;

[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class HelpTextGeneratorOnNonOptionsTypeAnalyzer : DiagnosticAnalyzer
public sealed class MarkerAttributeOnNonOptionsTypeAnalyzer : DiagnosticAnalyzer
{
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } =
ImmutableArray.Create(DiagnosticDescriptors.HelpTextGeneratorOnNonOptionsType);
ImmutableArray.Create(DiagnosticDescriptors.MarkerAttributeOnNonOptionsType);

public override void Initialize(AnalysisContext context)
{
Expand Down Expand Up @@ -43,8 +43,9 @@ private static void AnalyzeType(SymbolAnalysisContext context, KnownTypes knownT
{
context.ReportDiagnostic(
Diagnostic.Create(
DiagnosticDescriptors.HelpTextGeneratorOnNonOptionsType,
helpTextGeneratorAttribute.ApplicationSyntaxReference?.GetSyntax(context.CancellationToken).GetLocation() ?? type.Locations.First()));
DiagnosticDescriptors.MarkerAttributeOnNonOptionsType,
helpTextGeneratorAttribute.ApplicationSyntaxReference?.GetSyntax(context.CancellationToken).GetLocation() ?? type.Locations.First(),
"HelpTextGenerator"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,11 +363,11 @@ public static class DiagnosticDescriptors
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true);

public static readonly DiagnosticDescriptor HelpTextGeneratorOnNonOptionsType = new(
public static readonly DiagnosticDescriptor MarkerAttributeOnNonOptionsType = new(
id: "ARGP0044",
title: "Usage of [HelpTextGenerator] on non-options type",
messageFormat: "Type, annotated with [HelpTextGenerator] attribute is non-options type",
description: "[HelpTextGenerator] is only valid on options types. Consider annotating target type with [OptionsType] attribute.",
title: "Usage of marker attribute on non-options type",
messageFormat: "[{0}] attribute is applied to non-options type",
description: "This marker attribute only has effect when applied to an options type. Consider annotating target type with [OptionsType] attribute.",
category: ArgumentParsingCategoryName,
defaultSeverity: DiagnosticSeverity.Warning,
isEnabledByDefault: true);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using ArgumentParsing.Generators.Diagnostics.Analyzers;
using ArgumentParsing.Tests.Unit.Utilities;
using Microsoft.CodeAnalysis.Testing;

namespace ArgumentParsing.Tests.Unit;

public sealed class HelpTextGeneratorOnNonOptionsTypeAnalyzerTests : AnalyzerTestBase<HelpTextGeneratorOnNonOptionsTypeAnalyzer>
public sealed class MarkerAttributeOnNonOptionsTypeAnalyzerTests : AnalyzerTestBase<MarkerAttributeOnNonOptionsTypeAnalyzer>
{
[Theory]
[InlineData("class")]
[InlineData("struct")]
public async Task NoDiagnosticsWhenOnOptionsType1(string typeKeyword)
public async Task HelpTextGenerator_NoDiagnosticsWhenOnOptionsType1(string typeKeyword)
{
var source = $$"""
[OptionsType, HelpTextGenerator(default, default)]
Expand All @@ -23,7 +24,7 @@ public async Task NoDiagnosticsWhenOnOptionsType1(string typeKeyword)
[Theory]
[InlineData("class")]
[InlineData("struct")]
public async Task NoDiagnosticsWhenOnOptionsType2(string typeKeyword)
public async Task HelpTextGenerator_NoDiagnosticsWhenOnOptionsType2(string typeKeyword)
{
var source = $$"""
[OptionsType]
Expand All @@ -39,7 +40,7 @@ public async Task NoDiagnosticsWhenOnOptionsType2(string typeKeyword)
[Theory]
[InlineData("class")]
[InlineData("struct")]
public async Task NoDiagnosticsWhenOnOptionsType3(string typeKeyword)
public async Task HelpTextGenerator_NoDiagnosticsWhenOnOptionsType3(string typeKeyword)
{
var source = $$"""
[HelpTextGenerator(default, default)]
Expand All @@ -59,15 +60,21 @@ public async Task NoDiagnosticsWhenOnOptionsType3(string typeKeyword)
[Theory]
[InlineData("class")]
[InlineData("struct")]
public async Task WarningOnNonOptionsType(string typeKeyword)
public async Task HelpTextGenerator_WarningOnNonOptionsType(string typeKeyword)
{
var source = $$"""
[{|ARGP0044:HelpTextGenerator(default, default)|}]
[{|#0:HelpTextGenerator(default, default)|}]
{{typeKeyword}} MyOptions
{
}
""";

await VerifyAnalyzerAsync(source);
await VerifyAnalyzerAsync(source,
[
DiagnosticResult
.CompilerWarning("ARGP0044")
.WithLocation(0)
.WithArguments("HelpTextGenerator")
]);
}
}

0 comments on commit 510ee8d

Please sign in to comment.