Skip to content

Commit 840e7a5

Browse files
committed
More bug fixes to new diagnostics
1 parent 0f7dfe6 commit 840e7a5

File tree

3 files changed

+34
-25
lines changed

3 files changed

+34
-25
lines changed

CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservablePropertyGenerator.Execute.cs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -157,17 +157,17 @@ private static bool TryGatherDependentPropertyChangedNames(
157157
}
158158
}
159159

160-
ValidProperty:
161-
162-
continue;
163-
164160
InvalidProperty:
165161

166162
diagnostics.Add(
167163
AlsoNotifyChangeForInvalidTargetError,
168164
fieldSymbol,
169165
dependentPropertyName ?? "",
170166
fieldSymbol.ContainingType);
167+
168+
ValidProperty:
169+
170+
continue;
171171
}
172172

173173
return true;
@@ -201,13 +201,21 @@ private static bool TryGatherDependentCommandNames(
201201

202202
// Each target must be a string matching the name of a property from the containing type of the annotated field, and the
203203
// property must be of type IRelayCommand, or any type that implements that interface (to avoid generating invalid code).
204-
if (fieldSymbol.ContainingType.GetMembers(commandName).OfType<IPropertySymbol>().FirstOrDefault() is IPropertySymbol propertySymbol &&
205-
propertySymbol is INamedTypeSymbol typeSymbol &&
206-
typeSymbol.HasInterfaceWithFullyQualifiedName("global::CommunityToolkit.Mvvm.Input.IRelayCommand"))
204+
if (fieldSymbol.ContainingType.GetMembers(commandName).OfType<IPropertySymbol>().FirstOrDefault() is IPropertySymbol propertySymbol)
207205
{
208-
notifiedCommandNames.Add(commandName);
206+
// If there is a property member with the specified name, check that it's valid. If it isn't, the
207+
// target is definitely not valid, and the additional checks below can just be skipped. The property
208+
// is valid if it's of type IRelayCommand, or it has IRelayCommand in the set of all interfaces.
209+
if (propertySymbol.Type is INamedTypeSymbol typeSymbol &&
210+
(typeSymbol.HasFullyQualifiedName("global::CommunityToolkit.Mvvm.Input.IRelayCommand") ||
211+
typeSymbol.HasInterfaceWithFullyQualifiedName("global::CommunityToolkit.Mvvm.Input.IRelayCommand")))
212+
{
213+
notifiedCommandNames.Add(commandName);
209214

210-
goto ValidProperty;
215+
goto ValidProperty;
216+
}
217+
218+
goto InvalidProperty;
211219
}
212220

213221
// Check for generated commands too
@@ -223,17 +231,17 @@ propertySymbol is INamedTypeSymbol typeSymbol &&
223231
}
224232
}
225233

226-
ValidProperty:
227-
228-
continue;
229-
230234
InvalidProperty:
231235

232236
diagnostics.Add(
233237
AlsoNotifyCanExecuteForInvalidTargetError,
234238
fieldSymbol,
235239
commandName ?? "",
236240
fieldSymbol.ContainingType);
241+
242+
ValidProperty:
243+
244+
continue;
237245
}
238246

239247
return true;

tests/CommunityToolkit.Mvvm.SourceGenerators.UnitTests/Test_SourceGeneratorsDiagnostics.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ public partial class SampleViewModel : ObservableObject
648648
{
649649
[ObservableProperty]
650650
[AlsoNotifyChangeFor(null)]
651-
private string Name;
651+
private string name;
652652
}
653653
}";
654654

@@ -668,7 +668,7 @@ public partial class SampleViewModel : ObservableObject
668668
{
669669
[ObservableProperty]
670670
[AlsoNotifyChangeFor(nameof(Name))]
671-
private string Name;
671+
private string name;
672672
}
673673
}";
674674

@@ -688,7 +688,7 @@ public partial class SampleViewModel : ObservableObject
688688
{
689689
[ObservableProperty]
690690
[AlsoNotifyChangeFor(""FooBar"")]
691-
private string Name;
691+
private string name;
692692
}
693693
}";
694694

@@ -708,7 +708,7 @@ public partial class SampleViewModel : ObservableObject
708708
{
709709
[ObservableProperty]
710710
[AlsoNotifyChangeFor(nameof(Foo))]
711-
private string Name;
711+
private string name;
712712
713713
public void Foo()
714714
{
@@ -732,7 +732,7 @@ public partial class SampleViewModel : ObservableObject
732732
{
733733
[ObservableProperty]
734734
[AlsoNotifyCanExecuteFor(null)]
735-
private string Name;
735+
private string name;
736736
}
737737
}";
738738

@@ -752,7 +752,7 @@ public partial class SampleViewModel : ObservableObject
752752
{
753753
[ObservableProperty]
754754
[AlsoNotifyCanExecuteFor(""FooBar"")]
755-
private string Name;
755+
private string name;
756756
}
757757
}";
758758

@@ -772,7 +772,7 @@ public partial class SampleViewModel : ObservableObject
772772
{
773773
[ObservableProperty]
774774
[AlsoNotifyCanExecuteFor(nameof(Foo))]
775-
private string Name;
775+
private string name;
776776
777777
public void Foo()
778778
{
@@ -796,7 +796,7 @@ public partial class SampleViewModel : ObservableObject
796796
{
797797
[ObservableProperty]
798798
[AlsoNotifyCanExecuteFor(nameof(Foo))]
799-
private string Name;
799+
private string name;
800800
801801
public string Foo { get; }
802802
}
@@ -819,7 +819,7 @@ public partial class SampleViewModel : ObservableObject
819819
{
820820
[ObservableProperty]
821821
[AlsoNotifyCanExecuteFor(nameof(FooCommand))]
822-
private string Name;
822+
private string name;
823823
824824
public ICommand FooCommand { get; }
825825
}

tests/CommunityToolkit.Mvvm.UnitTests/Test_ObservablePropertyAttribute.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.ComponentModel.DataAnnotations;
99
using System.Linq;
1010
using System.Reflection;
11+
using System.Threading.Tasks;
1112
using CommunityToolkit.Mvvm.ComponentModel;
1213
using CommunityToolkit.Mvvm.Input;
1314
using Microsoft.VisualStudio.TestTools.UnitTesting;
@@ -282,7 +283,7 @@ public void Test_AlsoNotifyChangeFor_GeneratedCommand()
282283
int canExecuteRequests = 0;
283284

284285
model.PropertyChanged += (s, e) => propertyNames.Add(e.PropertyName);
285-
model.MyCommand.CanExecuteChanged += (s, e) => canExecuteRequests++;
286+
model.TestFromMethodCommand.CanExecuteChanged += (s, e) => canExecuteRequests++;
286287

287288
model.Text = "Ross";
288289

@@ -430,7 +431,7 @@ public sealed partial class DependentPropertyModel3
430431

431432
public string Alias => "";
432433

433-
public IRelayCommand MyCommand { get; } = null!;
434+
public IRelayCommand MyCommand { get; } = new RelayCommand(() => { });
434435
}
435436

436437
[INotifyPropertyChanged]
@@ -445,7 +446,7 @@ public sealed partial class DependentPropertyModel4
445446

446447
public string Alias => "";
447448

448-
public IAsyncRelayCommand<string> MyCommand { get; } = null!;
449+
public IAsyncRelayCommand<string> MyCommand { get; } = new AsyncRelayCommand<string>(_ => Task.CompletedTask);
449450
}
450451

451452
public partial class MyFormViewModel : ObservableValidator

0 commit comments

Comments
 (0)