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

Use declared accessibility for generated enum match extension class #26

Merged
merged 1 commit into from
Nov 27, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/// <autogenerated>
/// This code was generated by FxKit. Manual edits will not be saved.
/// </autogenerated>
#nullable enable
using System;

namespace EnumTest.PrettyCool;

internal static partial class MyEnumMatchExtension
{
/// <summary>
/// Perform an exhaustive match on the enum value.
/// </summary>
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public static TMatchResult Match<TMatchResult>(
this EnumTest.PrettyCool.MyEnum source,
Func<TMatchResult> One,
Func<TMatchResult> Two,
Func<TMatchResult> Three) => source switch
{
EnumTest.PrettyCool.MyEnum.One => One(),
EnumTest.PrettyCool.MyEnum.Two => Two(),
EnumTest.PrettyCool.MyEnum.Three => Three(),
_ => throw new ArgumentOutOfRangeException(nameof(source), source, null)
};

/// <summary>
/// Perform an exhaustive match on the enum value.
/// </summary>
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public static TMatchResult Match<TMatchResult>(
this EnumTest.PrettyCool.MyEnum source,
TMatchResult One,
TMatchResult Two,
TMatchResult Three) => source switch
{
EnumTest.PrettyCool.MyEnum.One => One,
EnumTest.PrettyCool.MyEnum.Two => Two,
EnumTest.PrettyCool.MyEnum.Three => Three,
_ => throw new ArgumentOutOfRangeException(nameof(source), source, null)
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,28 @@ public enum MyEnum
await generated.VerifyGeneratedCode();
}

[Test]
public async Task RespectsAccessibility()
{
var generated = Generate(
"""
using System;
using FxKit.CompilerServices;

namespace EnumTest.PrettyCool;

[EnumMatch]
internal enum MyEnum
{
One,
Two,
Three
}
""");

await generated.VerifyGeneratedCode();
}

private static string Generate(string source) =>
CodeGeneratorTestUtil.GetGeneratedOutput(new EnumMatchGenerator(), source);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Runtime.CompilerServices;
using FxKit.CompilerServices.Utilities;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace FxKit.CompilerServices.CodeGenerators.EnumMatch;
Expand Down Expand Up @@ -47,6 +48,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
Name: enumSymbol.Name,
HintName: enumSymbol.GetFullyQualifiedMetadataName(),
ContainingNamespace: enumSymbol.ContainingNamespace.ToDisplayString(),
Accessibility: SyntaxFacts.GetText(enumSymbol.DeclaredAccessibility),
Members: members);
}

Expand Down Expand Up @@ -84,4 +86,5 @@ internal sealed record EnumGeneration(
string Name,
string HintName,
string ContainingNamespace,
string Accessibility,
EquatableArray<string> Members);
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal static string GenerateMatchExtensionClass(EnumGeneration enumGeneration
writer.WriteLine("using System;");
writer.WriteLine();
writer.WriteLine($"namespace {enumGeneration.ContainingNamespace};\n");
writer.WriteLine($"public static partial class {enumGeneration.Name}MatchExtension");
writer.WriteLine($"{enumGeneration.Accessibility} static partial class {enumGeneration.Name}MatchExtension");
using (writer.WriteBlock())
{
WriteMatchFunction(writer, enumGeneration, func: true);
Expand Down