From b848a5d946932095432bb8bbd0521a056d44e4a1 Mon Sep 17 00:00:00 2001 From: Dominik Baran Date: Thu, 5 Oct 2023 15:12:59 +0200 Subject: [PATCH 1/5] tests: add tests for clashing renamer and enum types --- Tests/CSharp/MemberTests/MemberTests.cs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/Tests/CSharp/MemberTests/MemberTests.cs b/Tests/CSharp/MemberTests/MemberTests.cs index 4e10feabe..45a8e4629 100644 --- a/Tests/CSharp/MemberTests/MemberTests.cs +++ b/Tests/CSharp/MemberTests/MemberTests.cs @@ -3581,15 +3581,33 @@ public static void Main() } [Fact] - public async Task Issue420_RenameClashingEnumMemberAsync() + public async Task Issue420_DoNotRenameClashingEnumMemberAsync() { await TestConversionVisualBasicToCSharpAsync( @"Enum MyEnum MyEnumFirst + MyEnum End Enum", @" -internal enum MyEnumType +internal enum MyEnum { + MyEnumFirst, + MyEnum +} +"); + } + + [Fact] + public async Task Issue420_DoNotRenameClashingEnumMemberForPublicEnumAsync() + { + await TestConversionVisualBasicToCSharpAsync( + @"Public Enum MyEnum MyEnumFirst + MyEnum +End Enum", @" +public enum MyEnum +{ + MyEnumFirst, + MyEnum } "); } From 6e0749bb43f6af57dcd27a1397a074c629964212 Mon Sep 17 00:00:00 2001 From: Dominik Baran Date: Thu, 5 Oct 2023 15:13:10 +0200 Subject: [PATCH 2/5] fix: clashing renamer for enums --- CodeConverter/CSharp/ClashingMemberRenamer.cs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/CodeConverter/CSharp/ClashingMemberRenamer.cs b/CodeConverter/CSharp/ClashingMemberRenamer.cs index 8fbc5daf6..a61921421 100644 --- a/CodeConverter/CSharp/ClashingMemberRenamer.cs +++ b/CodeConverter/CSharp/ClashingMemberRenamer.cs @@ -22,8 +22,16 @@ public static async Task RenameClashingSymbolsAsync(Project project) var members = containerSymbol.GetMembers() .Where(m => m.Locations.Any(loc => loc.SourceTree != null && compilation.ContainsSyntaxTree(loc.SourceTree))) - .Where(s => containerSymbol.Name == s.Name || containerSymbol is INamedTypeSymbol nt && nt.IsEnumType() && SymbolRenamer.GetName(s).StartsWith(containerSymbol.Name, StringComparison.InvariantCulture)); + .Where(s => ShouldBeRenamed(containerSymbol, s)); var symbolSet = containerSymbol.Yield().Concat(members).ToArray(); return SymbolRenamer.GetSymbolsWithNewNames(symbolSet, new HashSet(symbolSet.Select(SymbolRenamer.GetName)), true); } + + private static bool ShouldBeRenamed(INamespaceOrTypeSymbol containerSymbol, ISymbol symbol) + { + if (containerSymbol is ITypeSymbol namedSymbol && namedSymbol.IsEnumType()) + return false; + + return containerSymbol.Name == symbol.Name; + } } \ No newline at end of file From c17df28f4d163db929fb284263002ecb74921ee4 Mon Sep 17 00:00:00 2001 From: Dominik Baran Date: Thu, 5 Oct 2023 15:14:19 +0200 Subject: [PATCH 3/5] chore: update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a4fc7c6b4..6316a8097 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) * Omit special VB conversions within expression trees [#930](https://github.com/icsharpcode/CodeConverter/issues/930) [#316](https://github.com/icsharpcode/CodeConverter/issues/316) * Support CData [#1032](https://github.com/icsharpcode/CodeConverter/issues/1032) * Use verbatim strings for strings containing newlines +* Fix clashing symbol renamer for Enum types [#1035](https://github.com/icsharpcode/CodeConverter/issues/1035) ### C# -> VB From e1709e7ff85e14fb9700f995815305b95af1d91f Mon Sep 17 00:00:00 2001 From: Dominik Baran Date: Thu, 5 Oct 2023 15:16:03 +0200 Subject: [PATCH 4/5] refactor: use INamedTypeSymbol --- CodeConverter/CSharp/ClashingMemberRenamer.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CodeConverter/CSharp/ClashingMemberRenamer.cs b/CodeConverter/CSharp/ClashingMemberRenamer.cs index a61921421..586701416 100644 --- a/CodeConverter/CSharp/ClashingMemberRenamer.cs +++ b/CodeConverter/CSharp/ClashingMemberRenamer.cs @@ -29,7 +29,7 @@ public static async Task RenameClashingSymbolsAsync(Project project) private static bool ShouldBeRenamed(INamespaceOrTypeSymbol containerSymbol, ISymbol symbol) { - if (containerSymbol is ITypeSymbol namedSymbol && namedSymbol.IsEnumType()) + if (containerSymbol is INamedTypeSymbol namedSymbol && namedSymbol.IsEnumType()) return false; return containerSymbol.Name == symbol.Name; From d6d5ab766549214805f41e29484e82600a0aa0b3 Mon Sep 17 00:00:00 2001 From: Dominik Baran Date: Sat, 7 Oct 2023 17:27:23 +0200 Subject: [PATCH 5/5] tests: regenerate results --- .../ConvertVbLibraryOnly/VbLibrary/AClass.cs | 8 ++++---- .../ConvertVbLibraryOnly/VbLibrary/AnEnum.cs | 2 +- .../ConvertWholeSolution/Prefix.VbLibrary/AClass.cs | 8 ++++---- .../ConvertWholeSolution/Prefix.VbLibrary/AnEnum.cs | 2 +- .../ConvertWholeSolution/VbLibrary/AClass.cs | 8 ++++---- .../ConvertWholeSolution/VbLibrary/AnEnum.cs | 2 +- 6 files changed, 15 insertions(+), 15 deletions(-) diff --git a/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertVbLibraryOnly/VbLibrary/AClass.cs b/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertVbLibraryOnly/VbLibrary/AClass.cs index 02aefaf35..abab83810 100644 --- a/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertVbLibraryOnly/VbLibrary/AClass.cs +++ b/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertVbLibraryOnly/VbLibrary/AClass.cs @@ -29,16 +29,16 @@ private void UseOutParameterInClass() x = argvalue; } - private void UseEnumFromOtherFileInSolution(AnEnumType m) + private void UseEnumFromOtherFileInSolution(AnEnum m) { - string nothing = Enumerable.Empty().ToArray()[(int)AnEnumType.AnEnumMember]; + string nothing = Enumerable.Empty().ToArray()[(int)AnEnum.AnEnumMember]; switch (m) { - case (AnEnumType)(-1): + case (AnEnum)(-1): { return; } - case AnEnumType.AnEnumMember: + case AnEnum.AnEnumMember: { return; } diff --git a/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertVbLibraryOnly/VbLibrary/AnEnum.cs b/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertVbLibraryOnly/VbLibrary/AnEnum.cs index 5d879c11b..7d18bac22 100644 --- a/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertVbLibraryOnly/VbLibrary/AnEnum.cs +++ b/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertVbLibraryOnly/VbLibrary/AnEnum.cs @@ -1,7 +1,7 @@ namespace VbLibrary { - internal enum AnEnumType + internal enum AnEnum { AnEnumMember } diff --git a/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertWholeSolution/Prefix.VbLibrary/AClass.cs b/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertWholeSolution/Prefix.VbLibrary/AClass.cs index d6ee82265..dbc22d4df 100644 --- a/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertWholeSolution/Prefix.VbLibrary/AClass.cs +++ b/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertWholeSolution/Prefix.VbLibrary/AClass.cs @@ -29,16 +29,16 @@ private void UseOutParameterInClass() x = argvalue; } - private void UseEnumFromOtherFileInSolution(AnEnumType m) + private void UseEnumFromOtherFileInSolution(AnEnum m) { - string nothing = Enumerable.Empty().ToArray()[(int)AnEnumType.AnEnumMember]; + string nothing = Enumerable.Empty().ToArray()[(int)AnEnum.AnEnumMember]; switch (m) { - case (AnEnumType)(-1): + case (AnEnum)(-1): { return; } - case AnEnumType.AnEnumMember: + case AnEnum.AnEnumMember: { return; } diff --git a/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertWholeSolution/Prefix.VbLibrary/AnEnum.cs b/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertWholeSolution/Prefix.VbLibrary/AnEnum.cs index 6185a0c5f..58c7f4f52 100644 --- a/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertWholeSolution/Prefix.VbLibrary/AnEnum.cs +++ b/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertWholeSolution/Prefix.VbLibrary/AnEnum.cs @@ -1,7 +1,7 @@ namespace Prefix.VbLibrary { - internal enum AnEnumType + internal enum AnEnum { AnEnumMember } diff --git a/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertWholeSolution/VbLibrary/AClass.cs b/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertWholeSolution/VbLibrary/AClass.cs index 02aefaf35..abab83810 100644 --- a/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertWholeSolution/VbLibrary/AClass.cs +++ b/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertWholeSolution/VbLibrary/AClass.cs @@ -29,16 +29,16 @@ private void UseOutParameterInClass() x = argvalue; } - private void UseEnumFromOtherFileInSolution(AnEnumType m) + private void UseEnumFromOtherFileInSolution(AnEnum m) { - string nothing = Enumerable.Empty().ToArray()[(int)AnEnumType.AnEnumMember]; + string nothing = Enumerable.Empty().ToArray()[(int)AnEnum.AnEnumMember]; switch (m) { - case (AnEnumType)(-1): + case (AnEnum)(-1): { return; } - case AnEnumType.AnEnumMember: + case AnEnum.AnEnumMember: { return; } diff --git a/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertWholeSolution/VbLibrary/AnEnum.cs b/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertWholeSolution/VbLibrary/AnEnum.cs index 5d879c11b..7d18bac22 100644 --- a/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertWholeSolution/VbLibrary/AnEnum.cs +++ b/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertWholeSolution/VbLibrary/AnEnum.cs @@ -1,7 +1,7 @@ namespace VbLibrary { - internal enum AnEnumType + internal enum AnEnum { AnEnumMember }