Skip to content

Commit

Permalink
Refactor property handling in GetterSetterToProperty.
Browse files Browse the repository at this point in the history
  • Loading branch information
tritao committed Sep 3, 2024
1 parent 63ca51a commit 59e9b91
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions src/Generator/Passes/GetterSetterToPropertyPass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,12 @@ private IEnumerable<Property> CleanUp(Class @class, List<Property> properties)
for (int i = properties.Count - 1; i >= 0; i--)
{
var property = properties[i];
if (!KeepProperty(property))
{
property.GetMethod.GenerationKind = GenerationKind.Generate;
@class.Properties.Remove(property);
properties.RemoveAt(i);
}
if (KeepProperty(property))
continue;

property.GetMethod.GenerationKind = GenerationKind.Generate;
@class.Properties.Remove(property);
properties.RemoveAt(i);
}

return properties;
Expand Down Expand Up @@ -160,6 +160,14 @@ public virtual bool KeepProperty(Property property)
private static void CreateOrUpdateProperty(List<Property> properties, Method method,
string name, QualifiedType type, bool isSetter = false)
{
string NormalizeName(string name)
{
return string.IsNullOrEmpty(name) ?
name : string.Concat(char.ToLowerInvariant(name[0]), name.Substring(1));
}

var normalizedName = NormalizeName(name);

Type underlyingType = GetUnderlyingType(type);
Property property = properties.Find(
p => p.Field == null &&
Expand All @@ -169,10 +177,10 @@ private static void CreateOrUpdateProperty(List<Property> properties, Method met
p.GetMethod.OriginalReturnType).Equals(underlyingType)) ||
(p.HasSetter && GetUnderlyingType(
p.SetMethod.Parameters[0].QualifiedType).Equals(underlyingType))) &&
Match(p, name));
Match(p, normalizedName));

if (property == null)
properties.Add(property = new Property { Name = name, QualifiedType = type });
properties.Add(property = new Property { Name = normalizedName, QualifiedType = type });

method.AssociatedDeclaration = property;

Expand Down Expand Up @@ -246,7 +254,9 @@ private static void ProcessProperties(Class @class, IEnumerable<Property> proper
property.SetMethod.OriginalReturnType.Type.Desugar().IsPrimitiveType(PrimitiveType.Void))
property.SetMethod.GenerationKind = GenerationKind.Internal;
property.Namespace = @class;

@class.Properties.Add(property);

RenameConflictingMethods(@class, property);
CombineComments(property);
}
Expand Down Expand Up @@ -339,14 +349,8 @@ private static string GetPropertyName(string name)
(string.Compare(name, firstWord, StringComparison.InvariantCultureIgnoreCase) == 0) ||
char.IsNumber(name[3])) return name;

if (name.Length == 4)
{
return char.ToLowerInvariant(
name[3]).ToString(CultureInfo.InvariantCulture);
}

return string.Concat(char.ToLowerInvariant(
name[3]).ToString(CultureInfo.InvariantCulture), name.AsSpan(4));
var rest = (name.Length == 4) ? string.Empty : name.Substring(4);
return string.Concat(name[3], rest);
}

private static string GetPropertyNameFromSetter(string name)
Expand All @@ -359,7 +363,6 @@ private static string GetPropertyNameFromSetter(string name)
return nameBuilder.ToString();

nameBuilder.TrimUnderscores();
nameBuilder[0] = char.ToLowerInvariant(nameBuilder[0]);
return nameBuilder.ToString();
}

Expand Down

0 comments on commit 59e9b91

Please sign in to comment.