-
Notifications
You must be signed in to change notification settings - Fork 385
Expand file tree
/
Copy pathObservableObjectGenerator.cs
More file actions
67 lines (55 loc) · 2.87 KB
/
ObservableObjectGenerator.cs
File metadata and controls
67 lines (55 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using CommunityToolkit.Mvvm.SourceGenerators.Extensions;
using CommunityToolkit.Mvvm.SourceGenerators.Models;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System.Collections.Immutable;
using static CommunityToolkit.Mvvm.SourceGenerators.Diagnostics.DiagnosticDescriptors;
namespace CommunityToolkit.Mvvm.SourceGenerators;
/// <summary>
/// A source generator for the <c>ObservableObjectAttribute</c> type.
/// </summary>
[Generator(LanguageNames.CSharp)]
public sealed class ObservableObjectGenerator : TransitiveMembersGenerator<int>
{
/// <summary>
/// Initializes a new instance of the <see cref="ObservableObjectGenerator"/> class.
/// </summary>
public ObservableObjectGenerator()
: base("CommunityToolkit.Mvvm.ComponentModel.ObservableObjectAttribute")
{
}
/// <inheritdoc/>
private protected override int ValidateTargetTypeAndGetInfo(INamedTypeSymbol typeSymbol, AttributeData attributeData, Compilation compilation, out ImmutableArray<DiagnosticInfo> diagnostics)
{
diagnostics = ImmutableArray<DiagnosticInfo>.Empty;
// Check if the type already implements INotifyPropertyChanged...
if (typeSymbol.ImplementsInterfaceMember("System.ComponentModel.INotifyPropertyChanged"))
{
diagnostics = ImmutableArray.Create(DiagnosticInfo.Create(DuplicateINotifyPropertyChangedInterfaceForObservableObjectAttributeError, typeSymbol, typeSymbol));
goto End;
}
// ...or INotifyPropertyChanging
if (typeSymbol.ImplementsInterfaceMember("System.ComponentModel.INotifyPropertyChanging"))
{
diagnostics = ImmutableArray.Create(DiagnosticInfo.Create(DuplicateINotifyPropertyChangingInterfaceForObservableObjectAttributeError, typeSymbol, typeSymbol));
goto End;
}
// Check if the type uses [INotifyPropertyChanged] or [ObservableObject] already (in the type hierarchy too)
if (typeSymbol.InheritsAttributeWithFullyQualifiedMetadataName("CommunityToolkit.Mvvm.ComponentModel.ObservableObjectAttribute") ||
typeSymbol.HasOrInheritsAttributeWithFullyQualifiedMetadataName("CommunityToolkit.Mvvm.ComponentModel.INotifyPropertyChangedAttribute"))
{
diagnostics = ImmutableArray.Create(DiagnosticInfo.Create(InvalidAttributeCombinationForObservableObjectAttributeError, typeSymbol, typeSymbol));
goto End;
}
End:
return 0;
}
/// <inheritdoc/>
protected override ImmutableArray<MemberDeclarationSyntax> FilterDeclaredMembers(int info, ImmutableArray<MemberDeclarationSyntax> memberDeclarations)
{
return memberDeclarations;
}
}