Skip to content

Commit 9283f2c

Browse files
committed
Switched to fully qualified names to resolve all symbols
1 parent 806b6da commit 9283f2c

17 files changed

+103
-81
lines changed

Microsoft.Toolkit.Mvvm.SourceGenerators/ComponentModel/INotifyPropertyChangedGenerator.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,29 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System.Collections.Generic;
6-
using System.ComponentModel;
76
using System.Diagnostics.CodeAnalysis;
87
using System.Linq;
98
using Microsoft.CodeAnalysis;
109
using Microsoft.CodeAnalysis.CSharp.Syntax;
11-
using Microsoft.Toolkit.Mvvm.ComponentModel;
1210
using Microsoft.Toolkit.Mvvm.SourceGenerators.Extensions;
1311
using static Microsoft.Toolkit.Mvvm.SourceGenerators.Diagnostics.DiagnosticDescriptors;
1412

1513
namespace Microsoft.Toolkit.Mvvm.SourceGenerators
1614
{
1715
/// <summary>
18-
/// A source generator for the <see cref="INotifyPropertyChangedAttribute"/> type.
16+
/// A source generator for the <c>INotifyPropertyChangedAttribute</c> type.
1917
/// </summary>
2018
[Generator]
21-
public sealed class INotifyPropertyChangedGenerator : TransitiveMembersGenerator<INotifyPropertyChangedAttribute>
19+
public sealed class INotifyPropertyChangedGenerator : TransitiveMembersGenerator
2220
{
21+
/// <summary>
22+
/// Initializes a new instance of the <see cref="INotifyPropertyChangedGenerator"/> class.
23+
/// </summary>
24+
public INotifyPropertyChangedGenerator()
25+
: base("Microsoft.Toolkit.Mvvm.ComponentModel.INotifyPropertyChangedAttribute")
26+
{
27+
}
28+
2329
/// <inheritdoc/>
2430
protected override DiagnosticDescriptor TargetTypeErrorDescriptor => INotifyPropertyChangedGeneratorError;
2531

@@ -31,7 +37,7 @@ protected override bool ValidateTargetType(
3137
INamedTypeSymbol classDeclarationSymbol,
3238
[NotNullWhen(false)] out DiagnosticDescriptor? descriptor)
3339
{
34-
INamedTypeSymbol iNotifyPropertyChangedSymbol = context.Compilation.GetTypeByMetadataName(typeof(INotifyPropertyChanged).FullName)!;
40+
INamedTypeSymbol iNotifyPropertyChangedSymbol = context.Compilation.GetTypeByMetadataName("System.ComponentModel.INotifyPropertyChanged")!;
3541

3642
// Check if the type already implements INotifyPropertyChanged
3743
if (classDeclarationSymbol.AllInterfaces.Any(i => SymbolEqualityComparer.Default.Equals(i, iNotifyPropertyChangedSymbol)))
@@ -55,7 +61,7 @@ protected override IEnumerable<MemberDeclarationSyntax> FilterDeclaredMembers(
5561
ClassDeclarationSyntax sourceDeclaration)
5662
{
5763
// If requested, only include the event and the basic methods to raise it, but not the additional helpers
58-
if (attributeData.HasNamedArgument(nameof(INotifyPropertyChangedAttribute.IncludeAdditionalHelperMethods), false))
64+
if (attributeData.HasNamedArgument("IncludeAdditionalHelperMethods", false))
5965
{
6066
return sourceDeclaration.Members.Where(static member =>
6167
{

Microsoft.Toolkit.Mvvm.SourceGenerators/ComponentModel/ObservableObjectGenerator.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,28 @@
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.ComponentModel;
65
using System.Diagnostics.CodeAnalysis;
76
using System.Linq;
87
using Microsoft.CodeAnalysis;
98
using Microsoft.CodeAnalysis.CSharp.Syntax;
10-
using Microsoft.Toolkit.Mvvm.ComponentModel;
119
using static Microsoft.Toolkit.Mvvm.SourceGenerators.Diagnostics.DiagnosticDescriptors;
1210

1311
namespace Microsoft.Toolkit.Mvvm.SourceGenerators
1412
{
1513
/// <summary>
16-
/// A source generator for the <see cref="ObservableObjectAttribute"/> type.
14+
/// A source generator for the <c>ObservableObjectAttribute</c> type.
1715
/// </summary>
1816
[Generator]
19-
public sealed class ObservableObjectGenerator : TransitiveMembersGenerator<ObservableObjectAttribute>
17+
public sealed class ObservableObjectGenerator : TransitiveMembersGenerator
2018
{
19+
/// <summary>
20+
/// Initializes a new instance of the <see cref="ObservableObjectGenerator"/> class.
21+
/// </summary>
22+
public ObservableObjectGenerator()
23+
: base("Microsoft.Toolkit.Mvvm.ComponentModel.ObservableObjectAttribute")
24+
{
25+
}
26+
2127
/// <inheritdoc/>
2228
protected override DiagnosticDescriptor TargetTypeErrorDescriptor => ObservableObjectGeneratorError;
2329

@@ -30,8 +36,8 @@ protected override bool ValidateTargetType(
3036
[NotNullWhen(false)] out DiagnosticDescriptor? descriptor)
3137
{
3238
INamedTypeSymbol
33-
iNotifyPropertyChangedSymbol = context.Compilation.GetTypeByMetadataName(typeof(INotifyPropertyChanged).FullName)!,
34-
iNotifyPropertyChangingSymbol = context.Compilation.GetTypeByMetadataName(typeof(INotifyPropertyChanging).FullName)!;
39+
iNotifyPropertyChangedSymbol = context.Compilation.GetTypeByMetadataName("System.ComponentModel.INotifyPropertyChanged")!,
40+
iNotifyPropertyChangingSymbol = context.Compilation.GetTypeByMetadataName("System.ComponentModel.INotifyPropertyChanging")!;
3541

3642
// Check if the type already implements INotifyPropertyChanged...
3743
if (classDeclarationSymbol.AllInterfaces.Any(i => SymbolEqualityComparer.Default.Equals(i, iNotifyPropertyChangedSymbol)))

Microsoft.Toolkit.Mvvm.SourceGenerators/ComponentModel/ObservablePropertyGenerator.SyntaxReceiver.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using Microsoft.CodeAnalysis;
88
using Microsoft.CodeAnalysis.CSharp;
99
using Microsoft.CodeAnalysis.CSharp.Syntax;
10-
using Microsoft.Toolkit.Mvvm.ComponentModel;
1110

1211
namespace Microsoft.Toolkit.Mvvm.SourceGenerators
1312
{
@@ -33,7 +32,7 @@ private sealed class SyntaxReceiver : ISyntaxContextReceiver
3332
public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
3433
{
3534
if (context.Node is FieldDeclarationSyntax { AttributeLists: { Count: > 0 } } fieldDeclaration &&
36-
context.SemanticModel.Compilation.GetTypeByMetadataName(typeof(ObservablePropertyAttribute).FullName) is INamedTypeSymbol attributeSymbol)
35+
context.SemanticModel.Compilation.GetTypeByMetadataName("Microsoft.Toolkit.Mvvm.ComponentModel.ObservablePropertyAttribute") is INamedTypeSymbol attributeSymbol)
3736
{
3837
SyntaxTriviaList leadingTrivia = fieldDeclaration.GetLeadingTrivia();
3938

Microsoft.Toolkit.Mvvm.SourceGenerators/ComponentModel/ObservablePropertyGenerator.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
using Microsoft.CodeAnalysis.CSharp;
1212
using Microsoft.CodeAnalysis.CSharp.Syntax;
1313
using Microsoft.CodeAnalysis.Text;
14-
using Microsoft.Toolkit.Mvvm.ComponentModel;
1514
using Microsoft.Toolkit.Mvvm.SourceGenerators.Diagnostics;
1615
using Microsoft.Toolkit.Mvvm.SourceGenerators.Extensions;
1716
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
@@ -21,7 +20,7 @@
2120
namespace Microsoft.Toolkit.Mvvm.SourceGenerators
2221
{
2322
/// <summary>
24-
/// A source generator for the <see cref="ObservablePropertyAttribute"/> type.
23+
/// A source generator for the <c>ObservablePropertyAttribute</c> type.
2524
/// </summary>
2625
[Generator]
2726
public sealed partial class ObservablePropertyGenerator : ISourceGenerator
@@ -86,9 +85,9 @@ private static void OnExecuteForProperties(
8685
ICollection<string> propertyChangingNames)
8786
{
8887
INamedTypeSymbol
89-
iNotifyPropertyChangingSymbol = context.Compilation.GetTypeByMetadataName(typeof(INotifyPropertyChanging).FullName)!,
88+
iNotifyPropertyChangingSymbol = context.Compilation.GetTypeByMetadataName("System.ComponentModel.INotifyPropertyChanging")!,
9089
observableObjectSymbol = context.Compilation.GetTypeByMetadataName("Microsoft.Toolkit.Mvvm.ComponentModel.ObservableObject")!,
91-
observableObjectAttributeSymbol = context.Compilation.GetTypeByMetadataName(typeof(ObservableObjectAttribute).FullName)!,
90+
observableObjectAttributeSymbol = context.Compilation.GetTypeByMetadataName("Microsoft.Toolkit.Mvvm.ComponentModel.ObservableObjectAttribute")!,
9291
observableValidatorSymbol = context.Compilation.GetTypeByMetadataName("Microsoft.Toolkit.Mvvm.ComponentModel.ObservableValidator")!;
9392

9493
// Check whether the current type implements INotifyPropertyChanging and whether it inherits from ObservableValidator
@@ -149,7 +148,7 @@ private static void OnExecuteForProperties(
149148
.ToFullString();
150149

151150
// Add the partial type
152-
context.AddSource($"[{typeof(ObservablePropertyAttribute).Name}]_[{classDeclarationSymbol.GetFullMetadataNameForFileName()}].cs", SourceText.From(source, Encoding.UTF8));
151+
context.AddSource($"[ObservablePropertyAttribute]_[{classDeclarationSymbol.GetFullMetadataNameForFileName()}].cs", SourceText.From(source, Encoding.UTF8));
153152
}
154153

155154
/// <summary>
@@ -178,7 +177,7 @@ private static PropertyDeclarationSyntax CreatePropertyDeclaration(
178177
typeName = fieldSymbol.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat),
179178
propertyName = GetGeneratedPropertyName(fieldSymbol);
180179

181-
INamedTypeSymbol alsoNotifyChangeForAttributeSymbol = context.Compilation.GetTypeByMetadataName(typeof(AlsoNotifyChangeForAttribute).FullName)!;
180+
INamedTypeSymbol alsoNotifyChangeForAttributeSymbol = context.Compilation.GetTypeByMetadataName("Microsoft.Toolkit.Mvvm.ComponentModel.AlsoNotifyChangeForAttribute")!;
182181
INamedTypeSymbol? validationAttributeSymbol = context.Compilation.GetTypeByMetadataName("System.ComponentModel.DataAnnotations.ValidationAttribute");
183182

184183
List<StatementSyntax> dependentPropertyNotificationStatements = new();
@@ -474,8 +473,8 @@ public void OnExecuteForPropertyArgs(GeneratorExecutionContext context, IReadOnl
474473
}
475474

476475
INamedTypeSymbol
477-
propertyChangedEventArgsSymbol = context.Compilation.GetTypeByMetadataName(typeof(PropertyChangedEventArgs).FullName)!,
478-
propertyChangingEventArgsSymbol = context.Compilation.GetTypeByMetadataName(typeof(PropertyChangingEventArgs).FullName)!;
476+
propertyChangedEventArgsSymbol = context.Compilation.GetTypeByMetadataName("System.ComponentModel.PropertyChangedEventArgs")!,
477+
propertyChangingEventArgsSymbol = context.Compilation.GetTypeByMetadataName("System.ComponentModel.PropertyChangingEventArgs")!;
479478

480479
// Create a static method to validate all properties in a given class.
481480
// This code takes a class symbol and produces a compilation unit as follows:
@@ -529,7 +528,7 @@ public void OnExecuteForPropertyArgs(GeneratorExecutionContext context, IReadOnl
529528
.ToFullString();
530529

531530
// Add the partial type
532-
context.AddSource($"[{typeof(ObservablePropertyAttribute).Name}]_[__KnownINotifyPropertyChangedOrChangingArgs].cs", SourceText.From(source, Encoding.UTF8));
531+
context.AddSource($"[ObservablePropertyAttribute]_[__KnownINotifyPropertyChangedOrChangingArgs].cs", SourceText.From(source, Encoding.UTF8));
533532
}
534533

535534
/// <summary>

Microsoft.Toolkit.Mvvm.SourceGenerators/ComponentModel/ObservableRecipientGenerator.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,31 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System.Collections.Generic;
6-
using System.ComponentModel;
76
using System.Diagnostics.CodeAnalysis;
87
using System.Linq;
98
using Microsoft.CodeAnalysis;
109
using Microsoft.CodeAnalysis.CSharp;
1110
using Microsoft.CodeAnalysis.CSharp.Syntax;
12-
using Microsoft.Toolkit.Mvvm.ComponentModel;
1311
using Microsoft.Toolkit.Mvvm.SourceGenerators.Extensions;
1412
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
1513
using static Microsoft.Toolkit.Mvvm.SourceGenerators.Diagnostics.DiagnosticDescriptors;
1614

1715
namespace Microsoft.Toolkit.Mvvm.SourceGenerators
1816
{
1917
/// <summary>
20-
/// A source generator for the <see cref="ObservableRecipientAttribute"/> type.
18+
/// A source generator for the <c>ObservableRecipientAttribute</c> type.
2119
/// </summary>
2220
[Generator]
23-
public sealed class ObservableRecipientGenerator : TransitiveMembersGenerator<ObservableRecipientAttribute>
21+
public sealed class ObservableRecipientGenerator : TransitiveMembersGenerator
2422
{
23+
/// <summary>
24+
/// Initializes a new instance of the <see cref="ObservableRecipientGenerator"/> class.
25+
/// </summary>
26+
public ObservableRecipientGenerator()
27+
: base("Microsoft.Toolkit.Mvvm.ComponentModel.ObservableRecipientAttribute")
28+
{
29+
}
30+
2531
/// <inheritdoc/>
2632
protected override DiagnosticDescriptor TargetTypeErrorDescriptor => ObservableRecipientGeneratorError;
2733

@@ -37,7 +43,7 @@ protected override bool ValidateTargetType(
3743
observableRecipientSymbol = context.Compilation.GetTypeByMetadataName("Microsoft.Toolkit.Mvvm.ComponentModel.ObservableRecipient")!,
3844
observableObjectSymbol = context.Compilation.GetTypeByMetadataName("Microsoft.Toolkit.Mvvm.ComponentModel.ObservableObject")!,
3945
observableObjectAttributeSymbol = context.Compilation.GetTypeByMetadataName("Microsoft.Toolkit.Mvvm.ComponentModel.ObservableObjectAttribute")!,
40-
iNotifyPropertyChangedSymbol = context.Compilation.GetTypeByMetadataName(typeof(INotifyPropertyChanged).FullName)!;
46+
iNotifyPropertyChangedSymbol = context.Compilation.GetTypeByMetadataName("System.ComponentModel.INotifyPropertyChanged")!;
4147

4248
// Check if the type already inherits from ObservableRecipient
4349
if (classDeclarationSymbol.InheritsFrom(observableRecipientSymbol))
@@ -53,7 +59,7 @@ protected override bool ValidateTargetType(
5359
!classDeclarationSymbol.GetAttributes().Any(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, observableObjectAttributeSymbol)) &&
5460
!classDeclarationSymbol.GetAttributes().Any(a =>
5561
SymbolEqualityComparer.Default.Equals(a.AttributeClass, iNotifyPropertyChangedSymbol) &&
56-
!a.HasNamedArgument(nameof(INotifyPropertyChangedAttribute.IncludeAdditionalHelperMethods), false)))
62+
!a.HasNamedArgument("IncludeAdditionalHelperMethods", false)))
5763
{
5864
descriptor = MissingBaseObservableObjectFunctionalityError;
5965

Microsoft.Toolkit.Mvvm.SourceGenerators/ComponentModel/TransitiveMembersGenerator.SyntaxReceiver.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,33 @@
1010

1111
namespace Microsoft.Toolkit.Mvvm.SourceGenerators
1212
{
13-
/// <inheritdoc cref="TransitiveMembersGenerator{TAttribute}"/>
14-
public abstract partial class TransitiveMembersGenerator<TAttribute>
13+
/// <inheritdoc cref="TransitiveMembersGenerator"/>
14+
public abstract partial class TransitiveMembersGenerator
1515
{
1616
/// <summary>
1717
/// An <see cref="ISyntaxContextReceiver"/> that selects candidate nodes to process.
1818
/// </summary>
1919
private sealed class SyntaxReceiver : ISyntaxContextReceiver
2020
{
21+
/// <summary>
22+
/// The fully qualified name of the attribute type to look for.
23+
/// </summary>
24+
private readonly string attributeTypeFullName;
25+
2126
/// <summary>
2227
/// The list of info gathered during exploration.
2328
/// </summary>
2429
private readonly List<Item> gatheredInfo = new();
2530

31+
/// <summary>
32+
/// Initializes a new instance of the <see cref="SyntaxReceiver"/> class.
33+
/// </summary>
34+
/// <param name="attributeTypeFullName">The fully qualified name of the attribute type to look for.</param>
35+
public SyntaxReceiver(string attributeTypeFullName)
36+
{
37+
this.attributeTypeFullName = attributeTypeFullName;
38+
}
39+
2640
/// <summary>
2741
/// Gets the collection of gathered info to process.
2842
/// </summary>
@@ -33,7 +47,7 @@ public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
3347
{
3448
if (context.Node is ClassDeclarationSyntax { AttributeLists: { Count: > 0 } } classDeclaration &&
3549
context.SemanticModel.GetDeclaredSymbol(classDeclaration) is INamedTypeSymbol classSymbol &&
36-
context.SemanticModel.Compilation.GetTypeByMetadataName(typeof(TAttribute).FullName) is INamedTypeSymbol attributeSymbol &&
50+
context.SemanticModel.Compilation.GetTypeByMetadataName(this.attributeTypeFullName) is INamedTypeSymbol attributeSymbol &&
3751
classSymbol.GetAttributes().FirstOrDefault(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, attributeSymbol)) is AttributeData attributeData &&
3852
attributeData.ApplicationSyntaxReference is SyntaxReference syntaxReference &&
3953
syntaxReference.GetSyntax() is AttributeSyntax attributeSyntax)

Microsoft.Toolkit.Mvvm.SourceGenerators/ComponentModel/TransitiveMembersGenerator.cs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
using Microsoft.CodeAnalysis.CSharp;
1515
using Microsoft.CodeAnalysis.CSharp.Syntax;
1616
using Microsoft.CodeAnalysis.Text;
17-
using Microsoft.Toolkit.Mvvm.ComponentModel;
1817
using Microsoft.Toolkit.Mvvm.SourceGenerators.Diagnostics;
1918
using Microsoft.Toolkit.Mvvm.SourceGenerators.Extensions;
2019
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
@@ -23,12 +22,30 @@
2322
namespace Microsoft.Toolkit.Mvvm.SourceGenerators
2423
{
2524
/// <summary>
26-
/// A source generator for the <see cref="ObservableObjectAttribute"/> type.
25+
/// A source generator for a given attribute type.
2726
/// </summary>
28-
/// <typeparam name="TAttribute">The type of the source attribute to look for.</typeparam>
29-
public abstract partial class TransitiveMembersGenerator<TAttribute> : ISourceGenerator
30-
where TAttribute : Attribute
27+
public abstract partial class TransitiveMembersGenerator : ISourceGenerator
3128
{
29+
/// <summary>
30+
/// The fully qualified name of the attribute type to look for.
31+
/// </summary>
32+
private readonly string attributeTypeFullName;
33+
34+
/// <summary>
35+
/// The name of the attribute type to look for.
36+
/// </summary>
37+
private readonly string attributeTypeName;
38+
39+
/// <summary>
40+
/// Initializes a new instance of the <see cref="TransitiveMembersGenerator"/> class.
41+
/// </summary>
42+
/// <param name="attributeTypeFullName">The fully qualified name of the attribute type to look for.</param>
43+
protected TransitiveMembersGenerator(string attributeTypeFullName)
44+
{
45+
this.attributeTypeFullName = attributeTypeFullName;
46+
this.attributeTypeName = attributeTypeFullName.Split('.').Last();
47+
}
48+
3249
/// <summary>
3350
/// Gets a <see cref="DiagnosticDescriptor"/> indicating when the generation failed for a given type.
3451
/// </summary>
@@ -37,7 +54,7 @@ public abstract partial class TransitiveMembersGenerator<TAttribute> : ISourceGe
3754
/// <inheritdoc/>
3855
public void Initialize(GeneratorInitializationContext context)
3956
{
40-
context.RegisterForSyntaxNotifications(static () => new SyntaxReceiver());
57+
context.RegisterForSyntaxNotifications(() => new SyntaxReceiver(this.attributeTypeFullName));
4158
}
4259

4360
/// <inheritdoc/>
@@ -78,9 +95,9 @@ public void Execute(GeneratorExecutionContext context)
7895
/// </summary>
7996
/// <returns>The syntax tree with the elements to emit in the generated code.</returns>
8097
[Pure]
81-
private static SyntaxTree LoadSourceSyntaxTree()
98+
private SyntaxTree LoadSourceSyntaxTree()
8299
{
83-
string filename = $"Microsoft.Toolkit.Mvvm.SourceGenerators.EmbeddedResources.{typeof(TAttribute).Name.Replace("Attribute", string.Empty)}.cs";
100+
string filename = $"Microsoft.Toolkit.Mvvm.SourceGenerators.EmbeddedResources.{this.attributeTypeName.Replace("Attribute", string.Empty)}.cs";
84101

85102
Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(filename);
86103
StreamReader reader = new(stream);
@@ -166,7 +183,7 @@ private void OnExecute(
166183
.ToFullString();
167184

168185
// Add the partial type
169-
context.AddSource($"[{typeof(TAttribute).Name}]_[{classDeclarationSymbol.GetFullMetadataNameForFileName()}].cs", SourceText.From(source, Encoding.UTF8));
186+
context.AddSource($"[{this.attributeTypeName}]_[{classDeclarationSymbol.GetFullMetadataNameForFileName()}].cs", SourceText.From(source, Encoding.UTF8));
170187
}
171188

172189
/// <summary>

0 commit comments

Comments
 (0)