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 diff --git a/CodeConverter/CSharp/ClashingMemberRenamer.cs b/CodeConverter/CSharp/ClashingMemberRenamer.cs index 8fbc5daf6..586701416 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 INamedTypeSymbol namedSymbol && namedSymbol.IsEnumType()) + return false; + + return containerSymbol.Name == symbol.Name; + } } \ No newline at end of file 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 } "); } 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 }