Skip to content

Commit c8835d3

Browse files
committed
Analyze default values even if callback is present
1 parent 36c1995 commit c8835d3

File tree

2 files changed

+109
-2
lines changed

2 files changed

+109
-2
lines changed

components/DependencyPropertyGenerator/CommunityToolkit.DependencyPropertyGenerator.SourceGenerators/Diagnostics/Analyzers/UseGeneratedDependencyPropertyOnManualPropertyAnalyzer.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,10 +511,15 @@ void HandleSetAccessor(IPropertySymbol propertySymbol, PropertyFlags propertyFla
511511
return;
512512
}
513513

514-
// If we have a second argument, a 'null' literal is the only supported value for it
514+
// If we have a second argument, a 'null' literal is the only supported value for it. If that's not the case,
515+
// we mark the propertry as not valid, but don't stop here. The reason is that even if we do have a callback,
516+
// meaning we cannot enable the code fixer, we still want to analyze the default value argument.
515517
if (objectCreationOperation.Arguments is not ([_] or [_, { Value.ConstantValue: { HasValue: true, Value: null } }]))
516518
{
517-
return;
519+
if (fieldFlags is not null)
520+
{
521+
fieldFlags.HasAnyDiagnostics = true;
522+
}
518523
}
519524

520525
bool isDependencyPropertyUnsetValue = false;

components/DependencyPropertyGenerator/CommunityToolkit.DependencyPropertyGenerator.Tests/Test_Analyzers.cs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2332,6 +2332,108 @@ public Visibility VolumeVisible
23322332
await CSharpAnalyzerTest<UseGeneratedDependencyPropertyOnManualPropertyAnalyzer>.VerifyAnalyzerAsync(source, LanguageVersion.CSharp13);
23332333
}
23342334

2335+
// Regression test for a case found in the Microsoft Store
2336+
[TestMethod]
2337+
[DataRow("default(float)")]
2338+
[DataRow("1.0F")]
2339+
[DataRow("(float)1.0")]
2340+
public async Task UseGeneratedDependencyPropertyOnManualPropertyAnalyzer_InvalidRegisterArguments_WCTDPG0030_WithMismatchedNullableUnderlyingType_DoesNotWarn(string defaultValue)
2341+
{
2342+
string source = $$"""
2343+
using Windows.UI.Xaml;
2344+
2345+
namespace MyApp;
2346+
2347+
public partial class MyObject : DependencyObject
2348+
{
2349+
public static readonly DependencyProperty Value1Property = DependencyProperty.Register(
2350+
nameof(Value1),
2351+
typeof(int?),
2352+
typeof(MyObject),
2353+
new PropertyMetadata({|WCTDPG0032:{{defaultValue}}|}));
2354+
2355+
public static readonly DependencyProperty Value2Property = DependencyProperty.Register(
2356+
nameof(Value2),
2357+
{|WCTDPG0030:typeof(int?)|},
2358+
typeof(MyObject),
2359+
new PropertyMetadata({|WCTDPG0032:{{defaultValue}}|}));
2360+
2361+
public static readonly DependencyProperty Value3Property = DependencyProperty.Register(
2362+
"Value3",
2363+
typeof(int?),
2364+
typeof(MyObject),
2365+
new PropertyMetadata({|WCTDPG0032:{{defaultValue}}|}));
2366+
2367+
public int? {|WCTDPG0017:Value1|}
2368+
{
2369+
get => (int?)GetValue(Value1Property);
2370+
set => SetValue(Value1Property, value);
2371+
}
2372+
2373+
public float Value2
2374+
{
2375+
get => (float)GetValue(Value2Property);
2376+
set => SetValue(Value2Property, value);
2377+
}
2378+
}
2379+
""";
2380+
2381+
await CSharpAnalyzerTest<UseGeneratedDependencyPropertyOnManualPropertyAnalyzer>.VerifyAnalyzerAsync(source, LanguageVersion.CSharp13);
2382+
}
2383+
2384+
// Same as above, but with property changed callbacks too
2385+
[TestMethod]
2386+
[DataRow("default(float)")]
2387+
[DataRow("1.0F")]
2388+
[DataRow("(float)1.0")]
2389+
public async Task UseGeneratedDependencyPropertyOnManualPropertyAnalyzer_InvalidRegisterArguments_WCTDPG0030_WithMismatchedNullableUnderlyingType_WithCallbacks_DoesNotWarn(string defaultValue)
2390+
{
2391+
string source = $$"""
2392+
using Windows.UI.Xaml;
2393+
2394+
namespace MyApp;
2395+
2396+
public partial class MyObject : DependencyObject
2397+
{
2398+
public static readonly DependencyProperty Value1Property = DependencyProperty.Register(
2399+
nameof(Value1),
2400+
typeof(int?),
2401+
typeof(MyObject),
2402+
new PropertyMetadata({|WCTDPG0032:{{defaultValue}}|}, ItemSourcePropertyChanged));
2403+
2404+
public static readonly DependencyProperty Value2Property = DependencyProperty.Register(
2405+
nameof(Value2),
2406+
{|WCTDPG0030:typeof(int?)|},
2407+
typeof(MyObject),
2408+
new PropertyMetadata({|WCTDPG0032:{{defaultValue}}|}, ItemSourcePropertyChanged));
2409+
2410+
public static readonly DependencyProperty Value3Property = DependencyProperty.Register(
2411+
"Value3",
2412+
typeof(int?),
2413+
typeof(MyObject),
2414+
new PropertyMetadata({|WCTDPG0032:{{defaultValue}}|}, ItemSourcePropertyChanged));
2415+
2416+
public int? Value1
2417+
{
2418+
get => (int?)GetValue(Value1Property);
2419+
set => SetValue(Value1Property, value);
2420+
}
2421+
2422+
public float Value2
2423+
{
2424+
get => (float)GetValue(Value2Property);
2425+
set => SetValue(Value2Property, value);
2426+
}
2427+
2428+
private static void ItemSourcePropertyChanged(object sender, DependencyPropertyChangedEventArgs args)
2429+
{
2430+
}
2431+
}
2432+
""";
2433+
2434+
await CSharpAnalyzerTest<UseGeneratedDependencyPropertyOnManualPropertyAnalyzer>.VerifyAnalyzerAsync(source, LanguageVersion.CSharp13);
2435+
}
2436+
23352437
[TestMethod]
23362438
[DataRow("\"Name\"", "typeof(string)", "typeof(string)", "null")]
23372439
[DataRow("\"Name\"", "typeof(string)", "typeof(Control)", "null")]

0 commit comments

Comments
 (0)