Skip to content

Commit

Permalink
Fix source generation for standalone enums (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
eiriktsarpalis authored Oct 26, 2024
1 parent ce639f0 commit a1a471c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using Microsoft.CodeAnalysis;
using System.Diagnostics;
using Microsoft.CodeAnalysis;

namespace TypeShape.Roslyn;

public partial class TypeDataModelGenerator
{
private static bool TryMapEnum(ITypeSymbol type, out TypeDataModel? model, out TypeDataModelGenerationStatus status)
private bool TryMapEnum(ITypeSymbol type, ref TypeDataModelGenerationContext ctx, out TypeDataModel? model, out TypeDataModelGenerationStatus status)
{
if (type.TypeKind is not TypeKind.Enum)
{
Expand All @@ -13,13 +14,16 @@ private static bool TryMapEnum(ITypeSymbol type, out TypeDataModel? model, out T
return false;
}

INamedTypeSymbol underlyingType = ((INamedTypeSymbol)type).EnumUnderlyingType!;
status = IncludeNestedType(underlyingType, ref ctx);
Debug.Assert(status is TypeDataModelGenerationStatus.Success);

model = new EnumDataModel
{
Type = type,
UnderlyingType = ((INamedTypeSymbol)type).EnumUnderlyingType!,
UnderlyingType = underlyingType,
};

status = TypeDataModelGenerationStatus.Success;

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ protected TypeDataModelGenerationStatus IncludeNestedType(ITypeSymbol type, ref
/// </remarks>
protected virtual TypeDataModelGenerationStatus MapType(ITypeSymbol type, ref TypeDataModelGenerationContext ctx, out TypeDataModel? model)
{
if (TryMapEnum(type, out model, out TypeDataModelGenerationStatus status))
if (TryMapEnum(type, ref ctx, out model, out TypeDataModelGenerationStatus status))
{
return status;
}
Expand Down
17 changes: 17 additions & 0 deletions tests/TypeShape.SourceGenerator.UnitTests/CompilationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,21 @@ public partial class MyWitness;
TypeShapeSourceGeneratorResult result = CompilationHelpers.RunTypeShapeSourceGenerator(compilation);
Assert.Empty(result.Diagnostics);
}

[Fact]
public static void EnumGeneration_NoErrors()
{
// Regression test for https://github.com/eiriktsarpalis/typeshape-csharp/issues/29
Compilation compilation = CompilationHelpers.CreateCompilation("""
using TypeShape;
enum MyEnum { A, B, C }
[GenerateShape<MyEnum>]
partial class Witness { }
""");

TypeShapeSourceGeneratorResult result = CompilationHelpers.RunTypeShapeSourceGenerator(compilation);
Assert.Empty(result.Diagnostics);
}
}

0 comments on commit a1a471c

Please sign in to comment.