Skip to content

Commit fba8b25

Browse files
committed
Only implement new initializer code for when initializers are present
1 parent 57877ec commit fba8b25

File tree

4 files changed

+83
-104
lines changed

4 files changed

+83
-104
lines changed

src/CommunityToolkit.Maui.SourceGenerators.Internal.UnitTests/BindablePropertyAttributeSourceGeneratorTests/CommonUsageTests.cs

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ public partial class {{defaultTestClassName}}
3838
/// Backing BindableProperty for the <see cref = "Text"/> property.
3939
/// </summary>
4040
public static readonly global::Microsoft.Maui.Controls.BindableProperty TextProperty = global::Microsoft.Maui.Controls.BindableProperty.Create("Text", typeof(string), typeof({{defaultTestNamespace}}.{{defaultTestClassName}}), null, Microsoft.Maui.Controls.BindingMode.OneWay, null, null, null, null, null);
41-
bool __initializingText = false;
42-
public partial string Text { get => __initializingText ? field : (string)GetValue(TextProperty); set => SetValue(TextProperty, field = value); }
41+
public partial string Text { get => (string)GetValue(TextProperty); set => SetValue(TextProperty, value); }
4342
}
4443
""";
4544

@@ -80,8 +79,7 @@ public partial class {{defaultTestClassName}}
8079
/// Backing BindableProperty for the <see cref = "Text"/> property.
8180
/// </summary>
8281
public static readonly global::Microsoft.Maui.Controls.BindableProperty TextProperty = global::Microsoft.Maui.Controls.BindableProperty.Create("Text", typeof(string), typeof({{defaultTestNamespace}}.{{defaultTestClassName}}), (string)"Hello", Microsoft.Maui.Controls.BindingMode.OneWay, null, null, null, null, null);
83-
bool __initializingText = false;
84-
public partial string Text { get => __initializingText ? field : (string)GetValue(TextProperty); set => SetValue(TextProperty, field = value); }
82+
public partial string Text { get => (string)GetValue(TextProperty); set => SetValue(TextProperty, value); }
8583
}
8684
""";
8785

@@ -122,8 +120,7 @@ public partial class {{defaultTestClassName}}
122120
/// Backing BindableProperty for the <see cref = "Text"/> property.
123121
/// </summary>
124122
public new static readonly global::Microsoft.Maui.Controls.BindableProperty TextProperty = global::Microsoft.Maui.Controls.BindableProperty.Create("Text", typeof(string), typeof({{defaultTestNamespace}}.{{defaultTestClassName}}), null, Microsoft.Maui.Controls.BindingMode.OneWay, null, null, null, null, null);
125-
bool __initializingText = false;
126-
public new partial string Text { get => __initializingText ? field : (string)GetValue(TextProperty); set => SetValue(TextProperty, field = value); }
123+
public new partial string Text { get => (string)GetValue(TextProperty); set => SetValue(TextProperty, value); }
127124
}
128125
""";
129126

@@ -164,8 +161,7 @@ public partial class {{defaultTestClassName}}
164161
/// Backing BindableProperty for the <see cref = "Text"/> property.
165162
/// </summary>
166163
public static readonly global::Microsoft.Maui.Controls.BindableProperty TextProperty = global::Microsoft.Maui.Controls.BindableProperty.Create("Text", typeof(string), typeof({{defaultTestNamespace}}.{{defaultTestClassName}}), null, Microsoft.Maui.Controls.BindingMode.OneWay, null, null, null, null, null);
167-
bool __initializingText = false;
168-
public partial string? Text { get => __initializingText ? field : (string? )GetValue(TextProperty); set => SetValue(TextProperty, field = value); }
164+
public partial string? Text { get => (string? )GetValue(TextProperty); set => SetValue(TextProperty, value); }
169165
}
170166
""";
171167

@@ -209,15 +205,13 @@ public partial class {{defaultTestClassName}}
209205
/// Backing BindableProperty for the <see cref = "Text"/> property.
210206
/// </summary>
211207
public static readonly global::Microsoft.Maui.Controls.BindableProperty TextProperty = global::Microsoft.Maui.Controls.BindableProperty.Create("Text", typeof(string), typeof({{defaultTestNamespace}}.{{defaultTestClassName}}), null, Microsoft.Maui.Controls.BindingMode.OneWay, null, null, null, null, null);
212-
bool __initializingText = false;
213-
public partial string Text { get => __initializingText ? field : (string)GetValue(TextProperty); set => SetValue(TextProperty, field = value); }
208+
public partial string Text { get => (string)GetValue(TextProperty); set => SetValue(TextProperty, value); }
214209
215210
/// <summary>
216211
/// Backing BindableProperty for the <see cref = "Number"/> property.
217212
/// </summary>
218213
public static readonly global::Microsoft.Maui.Controls.BindableProperty NumberProperty = global::Microsoft.Maui.Controls.BindableProperty.Create("Number", typeof(int), typeof({{defaultTestNamespace}}.{{defaultTestClassName}}), null, Microsoft.Maui.Controls.BindingMode.OneWay, null, null, null, null, null);
219-
bool __initializingNumber = false;
220-
public partial int Number { get => __initializingNumber ? field : (int)GetValue(NumberProperty); set => SetValue(NumberProperty, field = value); }
214+
public partial int Number { get => (int)GetValue(NumberProperty); set => SetValue(NumberProperty, value); }
221215
}
222216
""";
223217

@@ -271,8 +265,7 @@ public partial class {{defaultTestClassName}}
271265
/// Backing BindableProperty for the <see cref = "Value"/> property.
272266
/// </summary>
273267
public static readonly global::Microsoft.Maui.Controls.BindableProperty ValueProperty = global::Microsoft.Maui.Controls.BindableProperty.Create("Value", typeof(int), typeof({{defaultTestNamespace}}.{{defaultTestClassName}}), (int)42, (Microsoft.Maui.Controls.BindingMode)1, ValidateValue, OnPropertyChanged, OnPropertyChanging, CoerceValue, CreateDefaultValue);
274-
bool __initializingValue = false;
275-
public partial int Value { get => __initializingValue ? field : (int)GetValue(ValueProperty); set => SetValue(ValueProperty, field = value); }
268+
public partial int Value { get => (int)GetValue(ValueProperty); set => SetValue(ValueProperty, value); }
276269
}
277270
""";
278271

@@ -313,8 +306,7 @@ internal partial class TestView
313306
/// Backing BindableProperty for the <see cref = "Text"/> property.
314307
/// </summary>
315308
public static readonly global::Microsoft.Maui.Controls.BindableProperty TextProperty = global::Microsoft.Maui.Controls.BindableProperty.Create("Text", typeof(string), typeof({{defaultTestNamespace}}.{{defaultTestClassName}}), null, Microsoft.Maui.Controls.BindingMode.OneWay, null, null, null, null, null);
316-
bool __initializingText = false;
317-
public partial string Text { get => __initializingText ? field : (string)GetValue(TextProperty); set => SetValue(TextProperty, field = value); }
309+
public partial string Text { get => (string)GetValue(TextProperty); set => SetValue(TextProperty, value); }
318310
}
319311
""";
320312

@@ -394,8 +386,7 @@ public partial class {{defaultTestClassName}}
394386
/// Backing BindableProperty for the <see cref = "Text"/> property.
395387
/// </summary>
396388
public static readonly global::Microsoft.Maui.Controls.BindableProperty TextProperty = global::Microsoft.Maui.Controls.BindableProperty.Create("Text", typeof(string), typeof(TestNamespace.TestView), null, Microsoft.Maui.Controls.BindingMode.OneWay, null, null, null, null, null);
397-
bool __initializingText = false;
398-
public partial string Text { get => __initializingText ? field : (string)GetValue(TextProperty); internal set => SetValue(TextProperty, field = value); }
389+
public partial string Text { get => (string)GetValue(TextProperty); internal set => SetValue(TextProperty, value); }
399390
}
400391
""";
401392

@@ -437,8 +428,7 @@ public partial class {{defaultTestClassName}}
437428
/// </summary>
438429
static readonly global::Microsoft.Maui.Controls.BindablePropertyKey textPropertyKey = global::Microsoft.Maui.Controls.BindableProperty.CreateReadOnly("Text", typeof(string), typeof(TestNamespace.TestView), null, Microsoft.Maui.Controls.BindingMode.OneWay, null, null, null, null, null);
439430
public static readonly global::Microsoft.Maui.Controls.BindableProperty TextProperty = textPropertyKey.BindableProperty;
440-
bool __initializingText = false;
441-
public partial string Text { get => __initializingText ? field : (string)GetValue(TextProperty); private protected set => SetValue(textPropertyKey, field = value); }
431+
public partial string Text { get => (string)GetValue(TextProperty); private protected set => SetValue(textPropertyKey, value); }
442432
}
443433
""";
444434

@@ -479,8 +469,7 @@ public partial class {{defaultTestClassName}}
479469
/// Backing BindableProperty for the <see cref = "Text"/> property.
480470
/// </summary>
481471
public static readonly global::Microsoft.Maui.Controls.BindableProperty TextProperty = global::Microsoft.Maui.Controls.BindableProperty.Create("Text", typeof(string), typeof(TestNamespace.TestView), null, Microsoft.Maui.Controls.BindingMode.OneWay, null, null, null, null, null);
482-
bool __initializingText = false;
483-
public partial string Text { get => __initializingText ? field : (string)GetValue(TextProperty); protected internal set => SetValue(TextProperty, field = value); }
472+
public partial string Text { get => (string)GetValue(TextProperty); protected internal set => SetValue(TextProperty, value); }
484473
}
485474
""";
486475

@@ -522,8 +511,7 @@ public partial class {{defaultTestClassName}}
522511
/// </summary>
523512
static readonly global::Microsoft.Maui.Controls.BindablePropertyKey textPropertyKey = global::Microsoft.Maui.Controls.BindableProperty.CreateReadOnly("Text", typeof(string), typeof(TestNamespace.TestView), null, Microsoft.Maui.Controls.BindingMode.OneWay, null, null, null, null, null);
524513
public static readonly global::Microsoft.Maui.Controls.BindableProperty TextProperty = textPropertyKey.BindableProperty;
525-
bool __initializingText = false;
526-
public partial string Text { get => __initializingText ? field : (string)GetValue(TextProperty); protected set => SetValue(textPropertyKey, field = value); }
514+
public partial string Text { get => (string)GetValue(TextProperty); protected set => SetValue(textPropertyKey, value); }
527515
}
528516
""";
529517

@@ -565,8 +553,7 @@ public partial class {{defaultTestClassName}}
565553
/// </summary>
566554
static readonly global::Microsoft.Maui.Controls.BindablePropertyKey textPropertyKey = global::Microsoft.Maui.Controls.BindableProperty.CreateReadOnly("Text", typeof(string), typeof(TestNamespace.TestView), null, Microsoft.Maui.Controls.BindingMode.OneWay, null, null, null, null, null);
567555
public static readonly global::Microsoft.Maui.Controls.BindableProperty TextProperty = textPropertyKey.BindableProperty;
568-
bool __initializingText = false;
569-
public partial string Text { get => __initializingText ? field : (string)GetValue(TextProperty); private set => SetValue(textPropertyKey, field = value); }
556+
public partial string Text { get => (string)GetValue(TextProperty); private set => SetValue(textPropertyKey, value); }
570557
}
571558
""";
572559

@@ -608,8 +595,7 @@ public partial class {{defaultTestClassName}}
608595
/// </summary>
609596
static readonly global::Microsoft.Maui.Controls.BindablePropertyKey textPropertyKey = global::Microsoft.Maui.Controls.BindableProperty.CreateReadOnly("Text", typeof(string), typeof({{defaultTestNamespace}}.{{defaultTestClassName}}), null, Microsoft.Maui.Controls.BindingMode.OneWay, null, null, null, null, null);
610597
public static readonly global::Microsoft.Maui.Controls.BindableProperty TextProperty = textPropertyKey.BindableProperty;
611-
bool __initializingText = false;
612-
public partial string Text { get => __initializingText ? field : (string)GetValue(TextProperty); }
598+
public partial string Text { get => (string)GetValue(TextProperty); }
613599
}
614600
""";
615601

@@ -654,21 +640,20 @@ public partial class {{defaultTestClassName}}
654640
/// Backing BindableProperty for the <see cref = "Position"/> property.
655641
/// </summary>
656642
public static readonly global::Microsoft.Maui.Controls.BindableProperty PositionProperty = global::Microsoft.Maui.Controls.BindableProperty.Create("Position", typeof(System.TimeSpan), typeof({{defaultTestNamespace}}.{{defaultTestClassName}}), global::System.TimeSpan.Zero, Microsoft.Maui.Controls.BindingMode.OneWay, null, null, null, null, null);
657-
bool __initializingPosition = false;
658-
public partial System.TimeSpan Position { get => __initializingPosition ? field : (System.TimeSpan)GetValue(PositionProperty); set => SetValue(PositionProperty, field = value); }
643+
public partial System.TimeSpan Position { get => (System.TimeSpan)GetValue(PositionProperty); set => SetValue(PositionProperty, value); }
659644
660645
/// <summary>
661646
/// Backing BindableProperty for the <see cref = "CustomDuration"/> property.
662647
/// </summary>
663648
public static readonly global::Microsoft.Maui.Controls.BindableProperty CustomDurationProperty = global::Microsoft.Maui.Controls.BindableProperty.Create("CustomDuration", typeof(System.TimeSpan), typeof({{defaultTestNamespace}}.{{defaultTestClassName}}), new global::System.TimeSpan(900000000), Microsoft.Maui.Controls.BindingMode.OneWay, null, null, null, null, null);
664-
bool __initializingCustomDuration = false;
665-
public partial System.TimeSpan CustomDuration { get => __initializingCustomDuration ? field : (System.TimeSpan)GetValue(CustomDurationProperty); set => SetValue(CustomDurationProperty, field = value); }
649+
public partial System.TimeSpan CustomDuration { get => (System.TimeSpan)GetValue(CustomDurationProperty); set => SetValue(CustomDurationProperty, value); }
666650
}
667651
""";
668652

669653
await VerifySourceGeneratorAsync(source, expectedGenerated);
670654
}
671655

656+
672657
[Fact]
673658
public async Task GenerateBindableProperty_WithInitializers_GeneratesCorrectCode()
674659
{
@@ -718,5 +703,4 @@ static object __createDefaultText(Microsoft.Maui.Controls.BindableObject bindabl
718703

719704
await VerifySourceGeneratorAsync(source, expectedGenerated);
720705
}
721-
722706
}

0 commit comments

Comments
 (0)