Skip to content

Commit

Permalink
Rework property pass to keep original property names.
Browse files Browse the repository at this point in the history
  • Loading branch information
tritao committed Sep 3, 2024
1 parent 3aec51b commit 49b0861
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
47 changes: 47 additions & 0 deletions src/Generator/Passes/GetterSetterToPropertyPass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,20 @@ private static void ProcessProperties(Class @class, IEnumerable<Property> proper
{
foreach (Property property in properties)
{
// Look at the getter/setter names and figure out the proper casing for the
// property name.
if (property.GetMethod != null || property.SetMethod != null)
{
var casing = GetPropertyNameCasing(property);
var proposedName = GetNameWithCasing(property.Name, casing);

// Check if there are any types in the namespace or properties in the class
// that can conflict with the proposed name.
if (@class.FindType<Declaration>(proposedName) == null &&
@class.Properties.Find(f => f.Name == proposedName) == null)
property.Name = proposedName;
}

ProcessOverridden(@class, property);

if (!property.HasGetter)
Expand Down Expand Up @@ -262,6 +276,39 @@ private static void ProcessProperties(Class @class, IEnumerable<Property> proper
}
}

private static string GetNameWithCasing(string name, RenameCasePattern pattern)
{
if (string.IsNullOrEmpty(name))
return name;

var firstChar = pattern switch
{
RenameCasePattern.UpperCamelCase => char.ToUpperInvariant(name[0]),
RenameCasePattern.LowerCamelCase => char.ToLowerInvariant(name[0]),
_ => throw new ArgumentOutOfRangeException(nameof(pattern), pattern, null)
};

return string.Concat(firstChar, name.Substring(1));
}

private static RenameCasePattern GetPropertyNameCasing(Property property)
{
RenameCasePattern GetCasePattern(char c) =>
char.IsUpper(c) ? RenameCasePattern.UpperCamelCase : RenameCasePattern.LowerCamelCase;

if (property.GetMethod != null)
{
// Prefer the casing of the getter to handle cases like this:
// void prop();
// bool setProp();
var getterName = GetPropertyName(property.GetMethod.Name);
return GetCasePattern(getterName[0]);
}

var setterName = GetPropertyName(property.SetMethod.Name);
return GetCasePattern(setterName[0]);
}

private static void ProcessOverridden(Class @class, Property property)
{
if (!property.IsOverride)
Expand Down
2 changes: 1 addition & 1 deletion tests/dotnet/CSharp/CSharp.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public void TestReturnSmallPOD()
{
using (var f = new Foo())
{
foreach (var pod in new[] { f.SmallPodCdecl, f.SmallPodStdcall, f.SmallPodThiscall })
foreach (var pod in new[] { f.SmallPod_cdecl, f.SmallPod_stdcall, f.SmallPod_thiscall })
{
Assert.That(pod.A, Is.EqualTo(10000));
Assert.That(pod.B, Is.EqualTo(40000));
Expand Down
2 changes: 1 addition & 1 deletion tests/dotnet/Common/Common.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,7 @@ public void TestNonTrivialDtorInvocation()
using (var nonTrivialDtor = new NonTrivialDtor())
{
}
Assert.IsTrue(NonTrivialDtor.dtorCalled);
Assert.IsTrue(NonTrivialDtor.DtorCalled);
}

[Test]
Expand Down

0 comments on commit 49b0861

Please sign in to comment.