Skip to content

Commit 9a543e8

Browse files
committed
Minor code refactoring
1 parent a2ac073 commit 9a543e8

File tree

3 files changed

+38
-11
lines changed

3 files changed

+38
-11
lines changed

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ private sealed class SyntaxReceiver : ISyntaxContextReceiver
2121
/// <summary>
2222
/// The list of info gathered during exploration.
2323
/// </summary>
24-
private readonly List<(ClassDeclarationSyntax Class, AttributeSyntax Attribute, AttributeData Data)> gatheredInfo = new();
24+
private readonly List<Item> gatheredInfo = new();
2525

2626
/// <summary>
2727
/// Gets the collection of gathered info to process.
2828
/// </summary>
29-
public IReadOnlyCollection<(ClassDeclarationSyntax Class, AttributeSyntax Attribute, AttributeData Data)> GatheredInfo => this.gatheredInfo;
29+
public IReadOnlyCollection<Item> GatheredInfo => this.gatheredInfo;
3030

3131
/// <inheritdoc/>
3232
public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
@@ -38,9 +38,22 @@ public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
3838
attributeData.ApplicationSyntaxReference is SyntaxReference syntaxReference &&
3939
syntaxReference.GetSyntax() is AttributeSyntax attributeSyntax)
4040
{
41-
this.gatheredInfo.Add((classDeclaration, attributeSyntax, attributeData));
41+
this.gatheredInfo.Add(new Item(classDeclaration, classSymbol, attributeSyntax, attributeData));
4242
}
4343
}
44+
45+
/// <summary>
46+
/// A model for a group of item representing a discovered type to process.
47+
/// </summary>
48+
/// <param name="ClassDeclaration">The <see cref="ClassDeclarationSyntax"/> instance for the target class declaration.</param>
49+
/// <param name="ClassSymbol">The <see cref="INamedTypeSymbol"/> instance for <paramref name="ClassDeclaration"/>.</param>
50+
/// <param name="AttributeSyntax">The <see cref="AttributeSyntax"/> instance for the target attribute over <paramref name="ClassDeclaration"/>.</param>
51+
/// <param name="AttributeData">The <see cref="AttributeData"/> instance for <paramref name="AttributeSyntax"/>.</param>
52+
public sealed record Item(
53+
ClassDeclarationSyntax ClassDeclaration,
54+
INamedTypeSymbol ClassSymbol,
55+
AttributeSyntax AttributeSyntax,
56+
AttributeData AttributeData);
4457
}
4558
}
4659
}

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,22 @@ public void Execute(GeneratorExecutionContext context)
5353
// Load the syntax tree with the members to generate
5454
SyntaxTree sourceSyntaxTree = LoadSourceSyntaxTree();
5555

56-
foreach (var info in syntaxReceiver.GatheredInfo)
56+
foreach (SyntaxReceiver.Item item in syntaxReceiver.GatheredInfo)
5757
{
58-
SemanticModel semanticModel = context.Compilation.GetSemanticModel(info.Class.SyntaxTree);
59-
INamedTypeSymbol classDeclarationSymbol = semanticModel.GetDeclaredSymbol(info.Class)!;
60-
61-
if (!ValidateTargetType(context, info.Data, info.Class, classDeclarationSymbol, out var descriptor))
58+
if (!ValidateTargetType(context, item.AttributeData, item.ClassDeclaration, item.ClassSymbol, out var descriptor))
6259
{
63-
context.ReportDiagnostic(descriptor, info.Attribute, classDeclarationSymbol);
60+
context.ReportDiagnostic(descriptor, item.AttributeSyntax, item.ClassSymbol);
6461

6562
continue;
6663
}
6764

6865
try
6966
{
70-
OnExecute(context, info.Data, info.Class, classDeclarationSymbol, sourceSyntaxTree);
67+
OnExecute(context, item.AttributeData, item.ClassDeclaration, item.ClassSymbol, sourceSyntaxTree);
7168
}
7269
catch
7370
{
74-
context.ReportDiagnostic(TargetTypeErrorDescriptor, info.Attribute, classDeclarationSymbol);
71+
context.ReportDiagnostic(TargetTypeErrorDescriptor, item.AttributeSyntax, item.ClassSymbol);
7572
}
7673
}
7774
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.ComponentModel;
6+
7+
namespace System.Runtime.CompilerServices
8+
{
9+
/// <summary>
10+
/// Reserved to be used by the compiler for tracking metadata.
11+
/// This class should not be used by developers in source code.
12+
/// </summary>
13+
[EditorBrowsable(EditorBrowsableState.Never)]
14+
internal static class IsExternalInit
15+
{
16+
}
17+
}

0 commit comments

Comments
 (0)