Skip to content
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
4 changes: 3 additions & 1 deletion CSharpEssentials.Enums/Generators/StringEnumGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
static (node, _) => node is EnumDeclarationSyntax,
static (ctx, _) =>
(INamedTypeSymbol)ctx.TargetSymbol)
.Where(static symbol => symbol.DeclaredAccessibility != Accessibility.Private);
.Where(static symbol =>
symbol.DeclaredAccessibility != Accessibility.Private &&
symbol.ContainingType is null);

context.RegisterSourceOutput(enumSymbols, static (spc, enumSymbol) =>
{
Expand Down
40 changes: 40 additions & 0 deletions CSharpEssentials.Tests/Enums/StringEnumGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,26 @@
HttpStatusExtensions.HTTPResponseSnakeCase.Should().Be("httpresponse");
HttpStatusExtensions.HTTPResponseKebabCase.Should().Be("httpresponse");
}

[Fact]
public void StringEnumGenerator_Should_Not_Generate_Extensions_When_Enum_Is_Nested_In_Class()
{
AssertExtensionsTypeIsMissing("NestedClassStatusExtensions");
}

[Fact]
public void StringEnumGenerator_Should_Not_Generate_Extensions_When_Enum_Is_Nested_In_Struct()
{
AssertExtensionsTypeIsMissing("NestedStructStatusExtensions");
}

private static void AssertExtensionsTypeIsMissing(string generatedTypeName)
{
typeof(StringEnumGeneratorTests).Assembly
.GetType($"CSharpEssentials.Tests.Enums.{generatedTypeName}")
.Should()
.BeNull();
}
}

[StringEnum]
Expand All @@ -201,3 +221,23 @@
NotFound,
HTTPResponse
}

internal static class NestedEnumContainer
{
[StringEnum]
public enum NestedClassStatus

Check warning on line 228 in CSharpEssentials.Tests/Enums/StringEnumGeneratorTests.cs

View workflow job for this annotation

GitHub Actions / build

Because an application's API isn't typically referenced from outside the assembly, types can be made internal (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1515)

Check warning on line 228 in CSharpEssentials.Tests/Enums/StringEnumGeneratorTests.cs

View workflow job for this annotation

GitHub Actions / build

Because an application's API isn't typically referenced from outside the assembly, types can be made internal (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1515)
{
Pending,
Completed
}
}

internal struct NestedEnumStructContainer
{
[StringEnum]
public enum NestedStructStatus

Check warning on line 238 in CSharpEssentials.Tests/Enums/StringEnumGeneratorTests.cs

View workflow job for this annotation

GitHub Actions / build

Because an application's API isn't typically referenced from outside the assembly, types can be made internal (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1515)

Check warning on line 238 in CSharpEssentials.Tests/Enums/StringEnumGeneratorTests.cs

View workflow job for this annotation

GitHub Actions / build

Because an application's API isn't typically referenced from outside the assembly, types can be made internal (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1515)
{
Pending,
Completed
}
}
2 changes: 1 addition & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

[![Build](https://github.com/senrecep/CSharpEssentials/actions/workflows/build.yml/badge.svg)](https://github.com/senrecep/CSharpEssentials/actions/workflows/build.yml)

[![Tests](https://img.shields.io/badge/tests-2785%20passing-brightgreen)](https://github.com/senrecep/CSharpEssentials/actions/workflows/build.yml)
[![Tests](https://img.shields.io/badge/tests-2787%20passing-brightgreen)](https://github.com/senrecep/CSharpEssentials/actions/workflows/build.yml)
[![NuGet](https://img.shields.io/nuget/v/CSharpEssentials.svg)](https://www.nuget.org/packages/CSharpEssentials)
[![Downloads](https://img.shields.io/nuget/dt/CSharpEssentials.svg)](https://www.nuget.org/packages/CSharpEssentials)
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/senrecep/CSharpEssentials/blob/main/LICENCE)
Expand Down
Loading