From b68e89cc9b57f394f9702c759f706c7d181f25ed Mon Sep 17 00:00:00 2001 From: Erdem Date: Wed, 27 May 2026 22:37:53 +0300 Subject: [PATCH 1/2] fix(enums): skip nested string enum generation --- .../Generators/StringEnumGenerator.cs | 4 +++- .../Enums/StringEnumGeneratorTests.cs | 19 +++++++++++++++++++ README.MD | 4 +--- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/CSharpEssentials.Enums/Generators/StringEnumGenerator.cs b/CSharpEssentials.Enums/Generators/StringEnumGenerator.cs index bf03382..b3af6a5 100644 --- a/CSharpEssentials.Enums/Generators/StringEnumGenerator.cs +++ b/CSharpEssentials.Enums/Generators/StringEnumGenerator.cs @@ -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) => { diff --git a/CSharpEssentials.Tests/Enums/StringEnumGeneratorTests.cs b/CSharpEssentials.Tests/Enums/StringEnumGeneratorTests.cs index a02ae3d..d79d911 100644 --- a/CSharpEssentials.Tests/Enums/StringEnumGeneratorTests.cs +++ b/CSharpEssentials.Tests/Enums/StringEnumGeneratorTests.cs @@ -176,6 +176,15 @@ public void Constants_Should_Handle_Consecutive_Uppercase() HttpStatusExtensions.HTTPResponseSnakeCase.Should().Be("httpresponse"); HttpStatusExtensions.HTTPResponseKebabCase.Should().Be("httpresponse"); } + + [Fact] + public void Nested_Enum_Should_Not_Generate_Extensions() + { + typeof(StringEnumGeneratorTests).Assembly + .GetType("CSharpEssentials.Tests.Enums.NestedStatusExtensions") + .Should() + .BeNull(); + } } [StringEnum] @@ -201,3 +210,13 @@ internal enum HttpStatus NotFound, HTTPResponse } + +internal static class NestedEnumContainer +{ + [StringEnum] + public enum NestedStatus + { + Pending, + Completed + } +} diff --git a/README.MD b/README.MD index c7110ac..a69b77f 100644 --- a/README.MD +++ b/README.MD @@ -6,9 +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) -======= ->>>>>>> pr-21 +[![Tests](https://img.shields.io/badge/tests-2786%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) From ffd8f2f2e98252a1203220c1886b96a49d1d0c61 Mon Sep 17 00:00:00 2001 From: Erdem Date: Wed, 27 May 2026 23:54:32 +0300 Subject: [PATCH 2/2] test(enums): cover nested struct generation skip --- .../Enums/StringEnumGeneratorTests.cs | 27 ++++++++++++++++--- README.MD | 2 +- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/CSharpEssentials.Tests/Enums/StringEnumGeneratorTests.cs b/CSharpEssentials.Tests/Enums/StringEnumGeneratorTests.cs index d79d911..fb4c952 100644 --- a/CSharpEssentials.Tests/Enums/StringEnumGeneratorTests.cs +++ b/CSharpEssentials.Tests/Enums/StringEnumGeneratorTests.cs @@ -178,10 +178,21 @@ public void Constants_Should_Handle_Consecutive_Uppercase() } [Fact] - public void Nested_Enum_Should_Not_Generate_Extensions() + 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.NestedStatusExtensions") + .GetType($"CSharpEssentials.Tests.Enums.{generatedTypeName}") .Should() .BeNull(); } @@ -214,7 +225,17 @@ internal enum HttpStatus internal static class NestedEnumContainer { [StringEnum] - public enum NestedStatus + public enum NestedClassStatus + { + Pending, + Completed + } +} + +internal struct NestedEnumStructContainer +{ + [StringEnum] + public enum NestedStructStatus { Pending, Completed diff --git a/README.MD b/README.MD index a69b77f..ee86c98 100644 --- a/README.MD +++ b/README.MD @@ -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-2786%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)