// 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 Microsoft.CodeAnalysis; namespace CommunityToolkit.Mvvm.SourceGenerators.Extensions; /// /// Extension methods for the type. /// internal static class SyntaxNodeExtensions { /// /// Checks whether a given represents the first (partial) declaration of a given symbol. /// /// The input instance. /// The target instance to check the syntax declaration for. /// Whether is the first (partial) declaration for . /// /// This extension can be used to avoid accidentally generating repeated members for types that have multiple partial declarations. /// In order to keep this check efficient and without the need to collect all items and build some sort of hashset from them to /// remove duplicates, each syntax node is symply compared against the available declaring syntax references for the target symbol. /// If the syntax node matches the first syntax reference for the symbol, it is kept, otherwise it is considered a duplicate. /// public static bool IsFirstSyntaxDeclarationForSymbol(this SyntaxNode syntaxNode, ISymbol symbol) { return symbol.DeclaringSyntaxReferences.Length > 0 && symbol.DeclaringSyntaxReferences[0] is SyntaxReference syntaxReference && syntaxReference.SyntaxTree == syntaxNode.SyntaxTree && syntaxReference.Span == syntaxNode.Span; } }