Skip to content

Commit

Permalink
Merge pull request #1036 from icsharpcode/fix-1035
Browse files Browse the repository at this point in the history
fix: ClashingMemberRenamer incorrectly renames Enum types
  • Loading branch information
Yozer authored Oct 7, 2023
2 parents 2ea789b + d6d5ab7 commit fd6e10a
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 9 additions & 1 deletion CodeConverter/CSharp/ClashingMemberRenamer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,16 @@ public static async Task<Project> 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<string>(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;
}
}
22 changes: 20 additions & 2 deletions Tests/CSharp/MemberTests/MemberTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ private void UseOutParameterInClass()
x = argvalue;
}

private void UseEnumFromOtherFileInSolution(AnEnumType m)
private void UseEnumFromOtherFileInSolution(AnEnum m)
{
string nothing = Enumerable.Empty<string>().ToArray()[(int)AnEnumType.AnEnumMember];
string nothing = Enumerable.Empty<string>().ToArray()[(int)AnEnum.AnEnumMember];
switch (m)
{
case (AnEnumType)(-1):
case (AnEnum)(-1):
{
return;
}
case AnEnumType.AnEnumMember:
case AnEnum.AnEnumMember:
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

namespace VbLibrary
{
internal enum AnEnumType
internal enum AnEnum
{
AnEnumMember
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ private void UseOutParameterInClass()
x = argvalue;
}

private void UseEnumFromOtherFileInSolution(AnEnumType m)
private void UseEnumFromOtherFileInSolution(AnEnum m)
{
string nothing = Enumerable.Empty<string>().ToArray()[(int)AnEnumType.AnEnumMember];
string nothing = Enumerable.Empty<string>().ToArray()[(int)AnEnum.AnEnumMember];
switch (m)
{
case (AnEnumType)(-1):
case (AnEnum)(-1):
{
return;
}
case AnEnumType.AnEnumMember:
case AnEnum.AnEnumMember:
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

namespace Prefix.VbLibrary
{
internal enum AnEnumType
internal enum AnEnum
{
AnEnumMember
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ private void UseOutParameterInClass()
x = argvalue;
}

private void UseEnumFromOtherFileInSolution(AnEnumType m)
private void UseEnumFromOtherFileInSolution(AnEnum m)
{
string nothing = Enumerable.Empty<string>().ToArray()[(int)AnEnumType.AnEnumMember];
string nothing = Enumerable.Empty<string>().ToArray()[(int)AnEnum.AnEnumMember];
switch (m)
{
case (AnEnumType)(-1):
case (AnEnum)(-1):
{
return;
}
case AnEnumType.AnEnumMember:
case AnEnum.AnEnumMember:
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

namespace VbLibrary
{
internal enum AnEnumType
internal enum AnEnum
{
AnEnumMember
}
Expand Down

0 comments on commit fd6e10a

Please sign in to comment.