using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace ComputeSharp.SourceGeneration.Extensions;
///
/// A with some extension methods for C# syntax nodes.
///
internal static partial class SyntaxNodeExtensions
{
///
/// Checks whether a given is a given type declaration with or potentially with any base types, using only syntax.
///
/// The type of declaration to check for.
/// The input to check.
/// Whether is a given type declaration with or potentially with any base types.
public static bool IsTypeDeclarationWithOrPotentiallyWithBaseTypes(this SyntaxNode node)
where T : TypeDeclarationSyntax
{
// Immediately bail if the node is not a type declaration of the specified type
if (node is not T typeDeclaration)
{
return false;
}
// If the base types list is not empty, the type can definitely has implemented interfaces
if (typeDeclaration.BaseList is { Types.Count: > 0 })
{
return true;
}
// If the base types list is empty, check if the type is partial. If it is, it means
// that there could be another partial declaration with a non-empty base types list.
return typeDeclaration.Modifiers.Any(SyntaxKind.PartialKeyword);
}
}