|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System.Collections.Immutable; |
| 6 | +using System.Linq; |
| 7 | +using CommunityToolkit.Mvvm.SourceGenerators.Diagnostics; |
| 8 | +using CommunityToolkit.Mvvm.SourceGenerators.Extensions; |
| 9 | +using CommunityToolkit.Mvvm.SourceGenerators.Input.Models; |
| 10 | +using Microsoft.CodeAnalysis; |
| 11 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 12 | +using static CommunityToolkit.Mvvm.SourceGenerators.Diagnostics.DiagnosticDescriptors; |
| 13 | + |
| 14 | +namespace CommunityToolkit.Mvvm.SourceGenerators; |
| 15 | + |
| 16 | +/// <summary> |
| 17 | +/// A source generator for the <c>INotifyPropertyChangedAttribute</c> type. |
| 18 | +/// </summary> |
| 19 | +[Generator(LanguageNames.CSharp)] |
| 20 | +public sealed class INotifyPropertyChangedGenerator2 : TransitiveMembersGenerator2<INotifyPropertyChangedInfo> |
| 21 | +{ |
| 22 | + /// <summary> |
| 23 | + /// Initializes a new instance of the <see cref="INotifyPropertyChangedGenerator2"/> class. |
| 24 | + /// </summary> |
| 25 | + public INotifyPropertyChangedGenerator2() |
| 26 | + : base("global::CommunityToolkit.Mvvm.ComponentModel.INotifyPropertyChangedAttribute") |
| 27 | + { |
| 28 | + } |
| 29 | + |
| 30 | + /// <inheritdoc/> |
| 31 | + protected override INotifyPropertyChangedInfo GetInfo(AttributeData attributeData) |
| 32 | + { |
| 33 | + if (attributeData.TryGetNamedArgument("IncludeAdditionalHelperMethods", out bool includeAdditionalHelperMethods)) |
| 34 | + { |
| 35 | + return new(includeAdditionalHelperMethods); |
| 36 | + } |
| 37 | + |
| 38 | + return new(false); |
| 39 | + } |
| 40 | + |
| 41 | + /// <inheritdoc/> |
| 42 | + protected override bool ValidateTargetType( |
| 43 | + INamedTypeSymbol typeSymbol, |
| 44 | + INotifyPropertyChangedInfo info, |
| 45 | + out ImmutableArray<Diagnostic> diagnostics) |
| 46 | + { |
| 47 | + ImmutableArray<Diagnostic>.Builder builder = ImmutableArray.CreateBuilder<Diagnostic>(); |
| 48 | + |
| 49 | + // Check if the type already implements INotifyPropertyChanged |
| 50 | + if (typeSymbol.AllInterfaces.Any(i => i.HasFullyQualifiedName("global::System.ComponentModel.INotifyPropertyChanged"))) |
| 51 | + { |
| 52 | + builder.Add(DuplicateINotifyPropertyChangedInterfaceForINotifyPropertyChangedAttributeError, typeSymbol, typeSymbol); |
| 53 | + |
| 54 | + diagnostics = builder.ToImmutable(); |
| 55 | + |
| 56 | + return false; |
| 57 | + } |
| 58 | + |
| 59 | + diagnostics = builder.ToImmutable(); |
| 60 | + |
| 61 | + return true; |
| 62 | + } |
| 63 | + |
| 64 | + /// <inheritdoc/> |
| 65 | + protected override ImmutableArray<MemberDeclarationSyntax> FilterDeclaredMembers(INotifyPropertyChangedInfo info, ClassDeclarationSyntax classDeclaration) |
| 66 | + { |
| 67 | + // If requested, only include the event and the basic methods to raise it, but not the additional helpers |
| 68 | + if (!info.IncludeAdditionalHelperMethods) |
| 69 | + { |
| 70 | + return classDeclaration.Members.Where( |
| 71 | + static member => member |
| 72 | + is EventFieldDeclarationSyntax |
| 73 | + or MethodDeclarationSyntax { Identifier.ValueText: "OnPropertyChanged" }).ToImmutableArray(); |
| 74 | + } |
| 75 | + |
| 76 | + return classDeclaration.Members.ToImmutableArray(); |
| 77 | + } |
| 78 | +} |
0 commit comments