// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System.Collections.Generic; using System.Linq; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; namespace CommunityToolkit.Mvvm.SourceGenerators; /// public abstract partial class TransitiveMembersGenerator { /// /// An that selects candidate nodes to process. /// private sealed class SyntaxReceiver : ISyntaxContextReceiver { /// /// The fully qualified name of the attribute type to look for. /// private readonly string attributeTypeFullName; /// /// The list of info gathered during exploration. /// private readonly List gatheredInfo = new(); /// /// Initializes a new instance of the class. /// /// The fully qualified name of the attribute type to look for. public SyntaxReceiver(string attributeTypeFullName) { this.attributeTypeFullName = attributeTypeFullName; } /// /// Gets the collection of gathered info to process. /// public IReadOnlyCollection GatheredInfo => this.gatheredInfo; /// public void OnVisitSyntaxNode(GeneratorSyntaxContext context) { if (context.Node is ClassDeclarationSyntax { AttributeLists: { Count: > 0 } } classDeclaration && context.SemanticModel.GetDeclaredSymbol(classDeclaration) is INamedTypeSymbol classSymbol && context.SemanticModel.Compilation.GetTypeByMetadataName(this.attributeTypeFullName) is INamedTypeSymbol attributeSymbol && classSymbol.GetAttributes().FirstOrDefault(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, attributeSymbol)) is AttributeData attributeData && attributeData.ApplicationSyntaxReference is SyntaxReference syntaxReference && syntaxReference.GetSyntax() is AttributeSyntax attributeSyntax) { this.gatheredInfo.Add(new Item(classDeclaration, classSymbol, attributeSyntax, attributeData)); } } /// /// A model for a group of item representing a discovered type to process. /// /// The instance for the target class declaration. /// The instance for . /// The instance for the target attribute over . /// The instance for . public sealed record Item( ClassDeclarationSyntax ClassDeclaration, INamedTypeSymbol ClassSymbol, AttributeSyntax AttributeSyntax, AttributeData AttributeData); } }