Skip to content

Commit 49b0861

Browse files
committed
Rework property pass to keep original property names.
1 parent 3aec51b commit 49b0861

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

src/Generator/Passes/GetterSetterToPropertyPass.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,20 @@ private static void ProcessProperties(Class @class, IEnumerable<Property> proper
234234
{
235235
foreach (Property property in properties)
236236
{
237+
// Look at the getter/setter names and figure out the proper casing for the
238+
// property name.
239+
if (property.GetMethod != null || property.SetMethod != null)
240+
{
241+
var casing = GetPropertyNameCasing(property);
242+
var proposedName = GetNameWithCasing(property.Name, casing);
243+
244+
// Check if there are any types in the namespace or properties in the class
245+
// that can conflict with the proposed name.
246+
if (@class.FindType<Declaration>(proposedName) == null &&
247+
@class.Properties.Find(f => f.Name == proposedName) == null)
248+
property.Name = proposedName;
249+
}
250+
237251
ProcessOverridden(@class, property);
238252

239253
if (!property.HasGetter)
@@ -262,6 +276,39 @@ private static void ProcessProperties(Class @class, IEnumerable<Property> proper
262276
}
263277
}
264278

279+
private static string GetNameWithCasing(string name, RenameCasePattern pattern)
280+
{
281+
if (string.IsNullOrEmpty(name))
282+
return name;
283+
284+
var firstChar = pattern switch
285+
{
286+
RenameCasePattern.UpperCamelCase => char.ToUpperInvariant(name[0]),
287+
RenameCasePattern.LowerCamelCase => char.ToLowerInvariant(name[0]),
288+
_ => throw new ArgumentOutOfRangeException(nameof(pattern), pattern, null)
289+
};
290+
291+
return string.Concat(firstChar, name.Substring(1));
292+
}
293+
294+
private static RenameCasePattern GetPropertyNameCasing(Property property)
295+
{
296+
RenameCasePattern GetCasePattern(char c) =>
297+
char.IsUpper(c) ? RenameCasePattern.UpperCamelCase : RenameCasePattern.LowerCamelCase;
298+
299+
if (property.GetMethod != null)
300+
{
301+
// Prefer the casing of the getter to handle cases like this:
302+
// void prop();
303+
// bool setProp();
304+
var getterName = GetPropertyName(property.GetMethod.Name);
305+
return GetCasePattern(getterName[0]);
306+
}
307+
308+
var setterName = GetPropertyName(property.SetMethod.Name);
309+
return GetCasePattern(setterName[0]);
310+
}
311+
265312
private static void ProcessOverridden(Class @class, Property property)
266313
{
267314
if (!property.IsOverride)

tests/dotnet/CSharp/CSharp.Tests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ public void TestReturnSmallPOD()
167167
{
168168
using (var f = new Foo())
169169
{
170-
foreach (var pod in new[] { f.SmallPodCdecl, f.SmallPodStdcall, f.SmallPodThiscall })
170+
foreach (var pod in new[] { f.SmallPod_cdecl, f.SmallPod_stdcall, f.SmallPod_thiscall })
171171
{
172172
Assert.That(pod.A, Is.EqualTo(10000));
173173
Assert.That(pod.B, Is.EqualTo(40000));

tests/dotnet/Common/Common.Tests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,7 @@ public void TestNonTrivialDtorInvocation()
898898
using (var nonTrivialDtor = new NonTrivialDtor())
899899
{
900900
}
901-
Assert.IsTrue(NonTrivialDtor.dtorCalled);
901+
Assert.IsTrue(NonTrivialDtor.DtorCalled);
902902
}
903903

904904
[Test]

0 commit comments

Comments
 (0)