2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
// See the LICENSE file in the project root for more information.
4
4
5
- using System . Diagnostics . CodeAnalysis ;
5
+ using System . Collections . Immutable ;
6
6
using System . Linq ;
7
+ using CommunityToolkit . Mvvm . SourceGenerators . Diagnostics ;
8
+ using CommunityToolkit . Mvvm . SourceGenerators . Extensions ;
7
9
using Microsoft . CodeAnalysis ;
8
10
using Microsoft . CodeAnalysis . CSharp . Syntax ;
9
11
using static CommunityToolkit . Mvvm . SourceGenerators . Diagnostics . DiagnosticDescriptors ;
@@ -13,49 +15,56 @@ namespace CommunityToolkit.Mvvm.SourceGenerators;
13
15
/// <summary>
14
16
/// A source generator for the <c>ObservableObjectAttribute</c> type.
15
17
/// </summary>
16
- [ Generator ]
17
- public sealed class ObservableObjectGenerator : TransitiveMembersGenerator
18
+ [ Generator ( LanguageNames . CSharp ) ]
19
+ public sealed class ObservableObjectGenerator : TransitiveMembersGenerator2 < object ? >
18
20
{
19
21
/// <summary>
20
22
/// Initializes a new instance of the <see cref="ObservableObjectGenerator"/> class.
21
23
/// </summary>
22
24
public ObservableObjectGenerator ( )
23
- : base ( "CommunityToolkit.Mvvm.ComponentModel.ObservableObjectAttribute" )
25
+ : base ( "global:: CommunityToolkit.Mvvm.ComponentModel.ObservableObjectAttribute" )
24
26
{
25
27
}
26
28
27
29
/// <inheritdoc/>
28
- protected override DiagnosticDescriptor TargetTypeErrorDescriptor => ObservableObjectGeneratorError ;
30
+ protected override object ? GetInfo ( AttributeData attributeData )
31
+ {
32
+ return null ;
33
+ }
29
34
30
35
/// <inheritdoc/>
31
- protected override bool ValidateTargetType (
32
- GeneratorExecutionContext context ,
33
- AttributeData attributeData ,
34
- ClassDeclarationSyntax classDeclaration ,
35
- INamedTypeSymbol classDeclarationSymbol ,
36
- [ NotNullWhen ( false ) ] out DiagnosticDescriptor ? descriptor )
36
+ protected override bool ValidateTargetType ( INamedTypeSymbol typeSymbol , object ? info , out ImmutableArray < Diagnostic > diagnostics )
37
37
{
38
- INamedTypeSymbol iNotifyPropertyChangedSymbol = context . Compilation . GetTypeByMetadataName ( "System.ComponentModel.INotifyPropertyChanged" ) ! ;
39
- INamedTypeSymbol iNotifyPropertyChangingSymbol = context . Compilation . GetTypeByMetadataName ( "System.ComponentModel.INotifyPropertyChanging" ) ! ;
38
+ ImmutableArray < Diagnostic > . Builder builder = ImmutableArray . CreateBuilder < Diagnostic > ( ) ;
40
39
41
40
// Check if the type already implements INotifyPropertyChanged...
42
- if ( classDeclarationSymbol . AllInterfaces . Any ( i => SymbolEqualityComparer . Default . Equals ( i , iNotifyPropertyChangedSymbol ) ) )
41
+ if ( typeSymbol . AllInterfaces . Any ( i => i . HasFullyQualifiedName ( "global::System.ComponentModel.INotifyPropertyChanged" ) ) )
43
42
{
44
- descriptor = DuplicateINotifyPropertyChangedInterfaceForObservableObjectAttributeError ;
43
+ builder . Add ( DuplicateINotifyPropertyChangedInterfaceForObservableObjectAttributeError , typeSymbol , typeSymbol ) ;
44
+
45
+ diagnostics = builder . ToImmutable ( ) ;
45
46
46
47
return false ;
47
48
}
48
49
49
50
// ...or INotifyPropertyChanging
50
- if ( classDeclarationSymbol . AllInterfaces . Any ( i => SymbolEqualityComparer . Default . Equals ( i , iNotifyPropertyChangingSymbol ) ) )
51
+ if ( typeSymbol . AllInterfaces . Any ( i => i . HasFullyQualifiedName ( "global::System.ComponentModel.INotifyPropertyChanging" ) ) )
51
52
{
52
- descriptor = DuplicateINotifyPropertyChangingInterfaceForObservableObjectAttributeError ;
53
+ builder . Add ( DuplicateINotifyPropertyChangingInterfaceForObservableObjectAttributeError , typeSymbol , typeSymbol ) ;
54
+
55
+ diagnostics = builder . ToImmutable ( ) ;
53
56
54
57
return false ;
55
58
}
56
59
57
- descriptor = null ;
60
+ diagnostics = builder . ToImmutable ( ) ;
58
61
59
62
return true ;
60
63
}
64
+
65
+ /// <inheritdoc/>
66
+ protected override ImmutableArray < MemberDeclarationSyntax > FilterDeclaredMembers ( object ? info , ClassDeclarationSyntax classDeclaration )
67
+ {
68
+ return classDeclaration . Members . ToImmutableArray ( ) ;
69
+ }
61
70
}
0 commit comments