Skip to content

Commit 8c823de

Browse files
committed
Refctor EffectiveDefaultValueCreatorMethodName property (replacing GetDefaultValueCreatorMethodName method)
1 parent 9d785b6 commit 8c823de

File tree

3 files changed

+37
-20
lines changed

3 files changed

+37
-20
lines changed

src/CommunityToolkit.Maui.SourceGenerators.Internal.UnitTests/BindablePropertyModelTests.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ public void BindablePropertyModel_WithAllParameters_StoresCorrectValues()
9191
Assert.Equal(newKeywordText, model.NewKeywordText);
9292
Assert.Equal(hasInitializer, model.HasInitializer);
9393
Assert.Equal("TestPropertyProperty", model.BindablePropertyName);
94+
Assert.Equal(defaultValueCreatorMethodName, model.EffectiveDefaultValueCreatorMethodName);
9495
}
9596

9697
[Fact]
@@ -161,4 +162,36 @@ static Compilation CreateCompilation(string source)
161162
references,
162163
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
163164
}
165+
166+
[Fact]
167+
public void BindablePropertyName_WithInitializer_ReturnsCorrectEffectiveDefaultValueCreatorMethodName()
168+
{
169+
// Arrange
170+
var compilation = CreateCompilation("public class TestClass { public string TestProperty { get; set; } = \"Initial Value\"; }");
171+
var typeSymbol = compilation.GetTypeByMetadataName("TestClass")!;
172+
var propertySymbol = typeSymbol.GetMembers("TestProperty").OfType<IPropertySymbol>().First();
173+
174+
var model = new BindablePropertyModel(
175+
"TestProperty",
176+
propertySymbol.Type,
177+
typeSymbol,
178+
"null",
179+
"Microsoft.Maui.Controls.BindingMode.OneWay",
180+
"null",
181+
"null",
182+
"null",
183+
"null",
184+
"null",
185+
string.Empty,
186+
true, // IsReadOnlyBindableProperty
187+
string.Empty, // SetterAccessibility
188+
true
189+
);
190+
191+
// Act
192+
var effectiveDefaultValueCreatorMethodName = model.EffectiveDefaultValueCreatorMethodName;
193+
194+
// Assert
195+
Assert.Equal("__createDefaultTestProperty", effectiveDefaultValueCreatorMethodName);
196+
}
164197
}

src/CommunityToolkit.Maui.SourceGenerators.Internal/Generators/BindablePropertyAttributeSourceGenerator.cs

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ static void GenerateReadOnlyBindableProperty(StringBuilder sb, in BindableProper
242242
.Append(", ")
243243
.Append(info.CoerceValueMethodName)
244244
.Append(", ")
245-
.Append(GetDefaultValueCreatorMethodName(in info))
245+
.Append(info.EffectiveDefaultValueCreatorMethodName)
246246
.Append(");\n");
247247

248248
// Generate public BindableProperty from the key
@@ -298,7 +298,7 @@ static void GenerateBindableProperty(StringBuilder sb, in BindablePropertyModel
298298
.Append(", ")
299299
.Append(info.CoerceValueMethodName)
300300
.Append(", ")
301-
.Append(GetDefaultValueCreatorMethodName(in info))
301+
.Append(info.EffectiveDefaultValueCreatorMethodName)
302302
.Append(");\n");
303303

304304
sb.Append('\n');
@@ -546,23 +546,6 @@ static string GetFormattedReturnType(ITypeSymbol typeSymbol)
546546
}
547547
}
548548

549-
/// <summary>
550-
/// Determines the name of the method used to create the default value for the specified bindable property model.
551-
/// </summary>
552-
/// <param name="info">The bindable property model for which to retrieve the default value creator method name.</param>
553-
/// <returns>A string containing the name of the method that creates the default value for the property.
554-
/// If the property has an initializer, the returned name is of the generated default value method;
555-
/// otherwise, the default value creator method name from the model is returned.</returns>
556-
static string GetDefaultValueCreatorMethodName(in BindablePropertyModel info)
557-
{
558-
if (info.HasInitializer)
559-
{
560-
return "__createDefault" + info.PropertyName;
561-
}
562-
563-
return info.DefaultValueCreatorMethodName;
564-
}
565-
566549
/// <summary>
567550
/// Generates the boolean initialization flag used by bindable properties with initializers to indicate that the getter
568551
/// should return the backing field while the generated default value method is executing.
@@ -591,7 +574,7 @@ static void GenerateDefaultValueMethod(StringBuilder sb, in BindablePropertyMode
591574
var sanitizedPropertyName = IsDotnetKeyword(info.PropertyName) ? string.Concat("@", info.PropertyName) : info.PropertyName;
592575

593576
sb.Append("static object ")
594-
.Append(GetDefaultValueCreatorMethodName(in info))
577+
.Append(info.EffectiveDefaultValueCreatorMethodName)
595578
.Append("(global::Microsoft.Maui.Controls.BindableObject bindable)\n")
596579
.Append("{\n")
597580
.Append("((")

src/CommunityToolkit.Maui.SourceGenerators.Internal/Models/Records.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ record BindablePropertyModel(string PropertyName, ITypeSymbol ReturnType, ITypeS
77
{
88
public string BindablePropertyName => $"{PropertyName}Property";
99
public string BindablePropertyKeyName => $"{char.ToLower(PropertyName[0])}{PropertyName[1..]}PropertyKey";
10+
public string EffectiveDefaultValueCreatorMethodName => HasInitializer ? $"__createDefault{PropertyName}" : DefaultValueCreatorMethodName;
1011
}
1112

1213
record SemanticValues(ClassInformation ClassInformation, EquatableArray<BindablePropertyModel> BindableProperties);

0 commit comments

Comments
 (0)