Fix [ObservableRecipient] with inherited base attributes

This commit is contained in:
Sergio Pedri
2022-02-22 20:00:26 +01:00
parent 3d7b45c97a
commit c1cb029bc2
3 changed files with 44 additions and 2 deletions
@@ -82,8 +82,8 @@ public sealed class ObservableRecipientGenerator : TransitiveMembersGenerator<Ob
// In order to use [ObservableRecipient], the target type needs to inherit from ObservableObject,
// or be annotated with [ObservableObject] or [INotifyPropertyChanged] (with additional helpers).
if (!typeSymbol.InheritsFrom("global::CommunityToolkit.Mvvm.ComponentModel.ObservableObject") &&
!typeSymbol.GetAttributes().Any(static a => a.AttributeClass?.HasFullyQualifiedName("global::CommunityToolkit.Mvvm.ComponentModel.ObservableObjectAttribute") == true) &&
!typeSymbol.GetAttributes().Any(static a =>
!typeSymbol.HasOrInheritsAttribute(static a => a.AttributeClass?.HasFullyQualifiedName("global::CommunityToolkit.Mvvm.ComponentModel.ObservableObjectAttribute") == true) &&
!typeSymbol.HasOrInheritsAttribute(static a =>
a.AttributeClass?.HasFullyQualifiedName("global::CommunityToolkit.Mvvm.ComponentModel.INotifyPropertyChangedAttribute") == true &&
!a.HasNamedArgument("IncludeAdditionalHelperMethods", false)))
{
@@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
@@ -62,4 +64,23 @@ internal static class INamedTypeSymbolExtensions
return false;
}
/// <summary>
/// Checks whether or not a given <see cref="INamedTypeSymbol"/> has or inherits a specified attribute.
/// </summary>
/// <param name="typeSymbol">The target <see cref="INamedTypeSymbol"/> instance to check.</param>
/// <param name="predicate">The predicate used to match available attributes.</param>
/// <returns>Whether or not <paramref name="typeSymbol"/> has an attribute matching <paramref name="predicate"/>.</returns>
public static bool HasOrInheritsAttribute(this INamedTypeSymbol typeSymbol, Func<AttributeData, bool> predicate)
{
for (INamedTypeSymbol? currentType = typeSymbol; currentType is not null; currentType = currentType.BaseType)
{
if (currentType.GetAttributes().Any(predicate))
{
return true;
}
}
return false;
}
}
@@ -260,4 +260,25 @@ public partial class Test_ObservableRecipientAttribute
OnDeactivatedResult = true;
}
}
// See https://github.com/CommunityToolkit/dotnet/issues/109
[TestMethod]
public void Test_ObservableRecipientAttribute_WorksWithBaseClassWithObservableObjectAttribute()
{
ViewModelWithOnlyObservableRecipientAttribute model = new();
// This test method really only needs the two classes below to compile at all
Assert.IsTrue(model is INotifyPropertyChanged); // From [ObservableObject]
Assert.IsFalse(model.IsActive); // From [ObservableRecipient]
}
[ObservableObject]
public partial class BaseViewModelWithObservableObjectAttribute
{
}
[ObservableRecipient]
public partial class ViewModelWithOnlyObservableRecipientAttribute : BaseViewModelWithObservableObjectAttribute
{
}
}