Skip to content

Commit b3eb5e5

Browse files
committed
Switch ObservableObjectGenerator to incremental
1 parent 839ddf9 commit b3eb5e5

File tree

3 files changed

+29
-23
lines changed

3 files changed

+29
-23
lines changed

CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/INotifyPropertyChangedGenerator.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,7 @@ protected override INotifyPropertyChangedInfo GetInfo(AttributeData attributeDat
3939
}
4040

4141
/// <inheritdoc/>
42-
protected override bool ValidateTargetType(
43-
INamedTypeSymbol typeSymbol,
44-
INotifyPropertyChangedInfo info,
45-
out ImmutableArray<Diagnostic> diagnostics)
42+
protected override bool ValidateTargetType(INamedTypeSymbol typeSymbol, INotifyPropertyChangedInfo info, out ImmutableArray<Diagnostic> diagnostics)
4643
{
4744
ImmutableArray<Diagnostic>.Builder builder = ImmutableArray.CreateBuilder<Diagnostic>();
4845

CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservableObjectGenerator.cs

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
using System.Diagnostics.CodeAnalysis;
5+
using System.Collections.Immutable;
66
using System.Linq;
7+
using CommunityToolkit.Mvvm.SourceGenerators.Diagnostics;
8+
using CommunityToolkit.Mvvm.SourceGenerators.Extensions;
79
using Microsoft.CodeAnalysis;
810
using Microsoft.CodeAnalysis.CSharp.Syntax;
911
using static CommunityToolkit.Mvvm.SourceGenerators.Diagnostics.DiagnosticDescriptors;
@@ -13,49 +15,56 @@ namespace CommunityToolkit.Mvvm.SourceGenerators;
1315
/// <summary>
1416
/// A source generator for the <c>ObservableObjectAttribute</c> type.
1517
/// </summary>
16-
[Generator]
17-
public sealed class ObservableObjectGenerator : TransitiveMembersGenerator
18+
[Generator(LanguageNames.CSharp)]
19+
public sealed class ObservableObjectGenerator : TransitiveMembersGenerator2<object?>
1820
{
1921
/// <summary>
2022
/// Initializes a new instance of the <see cref="ObservableObjectGenerator"/> class.
2123
/// </summary>
2224
public ObservableObjectGenerator()
23-
: base("CommunityToolkit.Mvvm.ComponentModel.ObservableObjectAttribute")
25+
: base("global::CommunityToolkit.Mvvm.ComponentModel.ObservableObjectAttribute")
2426
{
2527
}
2628

2729
/// <inheritdoc/>
28-
protected override DiagnosticDescriptor TargetTypeErrorDescriptor => ObservableObjectGeneratorError;
30+
protected override object? GetInfo(AttributeData attributeData)
31+
{
32+
return null;
33+
}
2934

3035
/// <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)
3737
{
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>();
4039

4140
// 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")))
4342
{
44-
descriptor = DuplicateINotifyPropertyChangedInterfaceForObservableObjectAttributeError;
43+
builder.Add(DuplicateINotifyPropertyChangedInterfaceForObservableObjectAttributeError, typeSymbol, typeSymbol);
44+
45+
diagnostics = builder.ToImmutable();
4546

4647
return false;
4748
}
4849

4950
// ...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")))
5152
{
52-
descriptor = DuplicateINotifyPropertyChangingInterfaceForObservableObjectAttributeError;
53+
builder.Add(DuplicateINotifyPropertyChangingInterfaceForObservableObjectAttributeError, typeSymbol, typeSymbol);
54+
55+
diagnostics = builder.ToImmutable();
5356

5457
return false;
5558
}
5659

57-
descriptor = null;
60+
diagnostics = builder.ToImmutable();
5861

5962
return true;
6063
}
64+
65+
/// <inheritdoc/>
66+
protected override ImmutableArray<MemberDeclarationSyntax> FilterDeclaredMembers(object? info, ClassDeclarationSyntax classDeclaration)
67+
{
68+
return classDeclaration.Members.ToImmutableArray();
69+
}
6170
}

CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/TransitiveMembersGenerator.Incremental.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace CommunityToolkit.Mvvm.SourceGenerators;
2323
/// </summary>
2424
/// <typeparam name="TInfo">The type of info gathered from parsed <see cref="AttributeData"/> instances.</typeparam>
2525
public abstract partial class TransitiveMembersGenerator2<TInfo> : IIncrementalGenerator
26-
where TInfo : class
26+
where TInfo : class?
2727
{
2828
/// <summary>
2929
/// The fully qualified name of the attribute type to look for.

0 commit comments

Comments
 (0)