From 59e9b91a82cca2fdef1a63be81b8354dfd755a3f Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Sun, 5 Feb 2023 12:44:19 +0000 Subject: [PATCH] Refactor property handling in GetterSetterToProperty. --- .../Passes/GetterSetterToPropertyPass.cs | 37 ++++++++++--------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/src/Generator/Passes/GetterSetterToPropertyPass.cs b/src/Generator/Passes/GetterSetterToPropertyPass.cs index 5ec05eb08..e6c6dde46 100644 --- a/src/Generator/Passes/GetterSetterToPropertyPass.cs +++ b/src/Generator/Passes/GetterSetterToPropertyPass.cs @@ -125,12 +125,12 @@ private IEnumerable CleanUp(Class @class, List 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; @@ -160,6 +160,14 @@ public virtual bool KeepProperty(Property property) private static void CreateOrUpdateProperty(List 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 && @@ -169,10 +177,10 @@ private static void CreateOrUpdateProperty(List 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; @@ -246,7 +254,9 @@ private static void ProcessProperties(Class @class, IEnumerable 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); } @@ -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) @@ -359,7 +363,6 @@ private static string GetPropertyNameFromSetter(string name) return nameBuilder.ToString(); nameBuilder.TrimUnderscores(); - nameBuilder[0] = char.ToLowerInvariant(nameBuilder[0]); return nameBuilder.ToString(); }