Skip to content

Commit 839ddf9

Browse files
committed
Remove non incremental INotifyPropertyChangedGenerator
1 parent d3da266 commit 839ddf9

File tree

2 files changed

+31
-107
lines changed

2 files changed

+31
-107
lines changed

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

Lines changed: 0 additions & 78 deletions
This file was deleted.

CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/INotifyPropertyChangedGenerator.cs

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,75 +2,77 @@
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.Collections.Generic;
6-
using System.Diagnostics.CodeAnalysis;
5+
using System.Collections.Immutable;
76
using System.Linq;
7+
using CommunityToolkit.Mvvm.SourceGenerators.Diagnostics;
8+
using CommunityToolkit.Mvvm.SourceGenerators.Extensions;
9+
using CommunityToolkit.Mvvm.SourceGenerators.Input.Models;
810
using Microsoft.CodeAnalysis;
911
using Microsoft.CodeAnalysis.CSharp.Syntax;
10-
using CommunityToolkit.Mvvm.SourceGenerators.Extensions;
1112
using static CommunityToolkit.Mvvm.SourceGenerators.Diagnostics.DiagnosticDescriptors;
1213

1314
namespace CommunityToolkit.Mvvm.SourceGenerators;
1415

1516
/// <summary>
1617
/// A source generator for the <c>INotifyPropertyChangedAttribute</c> type.
1718
/// </summary>
18-
[Generator]
19-
public sealed class INotifyPropertyChangedGenerator : TransitiveMembersGenerator
19+
[Generator(LanguageNames.CSharp)]
20+
public sealed class INotifyPropertyChangedGenerator : TransitiveMembersGenerator2<INotifyPropertyChangedInfo>
2021
{
2122
/// <summary>
2223
/// Initializes a new instance of the <see cref="INotifyPropertyChangedGenerator"/> class.
2324
/// </summary>
2425
public INotifyPropertyChangedGenerator()
25-
: base("CommunityToolkit.Mvvm.ComponentModel.INotifyPropertyChangedAttribute")
26+
: base("global::CommunityToolkit.Mvvm.ComponentModel.INotifyPropertyChangedAttribute")
2627
{
2728
}
2829

2930
/// <inheritdoc/>
30-
protected override DiagnosticDescriptor TargetTypeErrorDescriptor => INotifyPropertyChangedGeneratorError;
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+
}
3140

3241
/// <inheritdoc/>
3342
protected override bool ValidateTargetType(
34-
GeneratorExecutionContext context,
35-
AttributeData attributeData,
36-
ClassDeclarationSyntax classDeclaration,
37-
INamedTypeSymbol classDeclarationSymbol,
38-
[NotNullWhen(false)] out DiagnosticDescriptor? descriptor)
43+
INamedTypeSymbol typeSymbol,
44+
INotifyPropertyChangedInfo info,
45+
out ImmutableArray<Diagnostic> diagnostics)
3946
{
40-
INamedTypeSymbol iNotifyPropertyChangedSymbol = context.Compilation.GetTypeByMetadataName("System.ComponentModel.INotifyPropertyChanged")!;
47+
ImmutableArray<Diagnostic>.Builder builder = ImmutableArray.CreateBuilder<Diagnostic>();
4148

4249
// Check if the type already implements INotifyPropertyChanged
43-
if (classDeclarationSymbol.AllInterfaces.Any(i => SymbolEqualityComparer.Default.Equals(i, iNotifyPropertyChangedSymbol)))
50+
if (typeSymbol.AllInterfaces.Any(i => i.HasFullyQualifiedName("global::System.ComponentModel.INotifyPropertyChanged")))
4451
{
45-
descriptor = DuplicateINotifyPropertyChangedInterfaceForINotifyPropertyChangedAttributeError;
52+
builder.Add(DuplicateINotifyPropertyChangedInterfaceForINotifyPropertyChangedAttributeError, typeSymbol, typeSymbol);
53+
54+
diagnostics = builder.ToImmutable();
4655

4756
return false;
4857
}
4958

50-
descriptor = null;
59+
diagnostics = builder.ToImmutable();
5160

5261
return true;
5362
}
5463

5564
/// <inheritdoc/>
56-
protected override IEnumerable<MemberDeclarationSyntax> FilterDeclaredMembers(
57-
GeneratorExecutionContext context,
58-
AttributeData attributeData,
59-
ClassDeclarationSyntax classDeclaration,
60-
INamedTypeSymbol classDeclarationSymbol,
61-
ClassDeclarationSyntax sourceDeclaration)
65+
protected override ImmutableArray<MemberDeclarationSyntax> FilterDeclaredMembers(INotifyPropertyChangedInfo info, ClassDeclarationSyntax classDeclaration)
6266
{
6367
// If requested, only include the event and the basic methods to raise it, but not the additional helpers
64-
if (attributeData.HasNamedArgument("IncludeAdditionalHelperMethods", false))
68+
if (!info.IncludeAdditionalHelperMethods)
6569
{
66-
return sourceDeclaration.Members.Where(static member =>
67-
{
68-
return member
70+
return classDeclaration.Members.Where(
71+
static member => member
6972
is EventFieldDeclarationSyntax
70-
or MethodDeclarationSyntax { Identifier: { ValueText: "OnPropertyChanged" } };
71-
});
73+
or MethodDeclarationSyntax { Identifier.ValueText: "OnPropertyChanged" }).ToImmutableArray();
7274
}
7375

74-
return sourceDeclaration.Members;
76+
return classDeclaration.Members.ToImmutableArray();
7577
}
7678
}

0 commit comments

Comments
 (0)