|
| 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; |
| 6 | +using System.Linq; |
| 7 | +using Microsoft.CodeAnalysis; |
| 8 | + |
| 9 | +namespace CommunityToolkit.Mvvm.SourceGenerators.Extensions; |
| 10 | + |
| 11 | +/// <summary> |
| 12 | +/// Extension methods for the <see cref="ITypeSymbol"/> type. |
| 13 | +/// </summary> |
| 14 | +internal static class ITypeSymbolExtensions |
| 15 | +{ |
| 16 | + /// <summary> |
| 17 | + /// Checks whether or not a given <see cref="ITypeSymbol"/> inherits from a specified type. |
| 18 | + /// </summary> |
| 19 | + /// <param name="typeSymbol">The target <see cref="ITypeSymbol"/> instance to check.</param> |
| 20 | + /// <param name="name">The full name of the type to check for inheritance.</param> |
| 21 | + /// <returns>Whether or not <paramref name="typeSymbol"/> inherits from <paramref name="name"/>.</returns> |
| 22 | + public static bool InheritsFromFullyQualifiedName(this ITypeSymbol typeSymbol, string name) |
| 23 | + { |
| 24 | + INamedTypeSymbol? baseType = typeSymbol.BaseType; |
| 25 | + |
| 26 | + while (baseType != null) |
| 27 | + { |
| 28 | + if (baseType.HasFullyQualifiedName(name)) |
| 29 | + { |
| 30 | + return true; |
| 31 | + } |
| 32 | + |
| 33 | + baseType = baseType.BaseType; |
| 34 | + } |
| 35 | + |
| 36 | + return false; |
| 37 | + } |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Checks whether or not a given <see cref="ITypeSymbol"/> implements an interface with a specied name. |
| 41 | + /// </summary> |
| 42 | + /// <param name="typeSymbol">The target <see cref="ITypeSymbol"/> instance to check.</param> |
| 43 | + /// <param name="name">The full name of the type to check for interface implementation.</param> |
| 44 | + /// <returns>Whether or not <paramref name="typeSymbol"/> has an interface with the specified name.</returns> |
| 45 | + public static bool HasInterfaceWithFullyQualifiedName(this ITypeSymbol typeSymbol, string name) |
| 46 | + { |
| 47 | + foreach (INamedTypeSymbol interfaceType in typeSymbol.AllInterfaces) |
| 48 | + { |
| 49 | + if (interfaceType.HasFullyQualifiedName(name)) |
| 50 | + { |
| 51 | + return true; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + return false; |
| 56 | + } |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Checks whether or not a given <see cref="ITypeSymbol"/> has or inherits a specified attribute. |
| 60 | + /// </summary> |
| 61 | + /// <param name="typeSymbol">The target <see cref="ITypeSymbol"/> instance to check.</param> |
| 62 | + /// <param name="predicate">The predicate used to match available attributes.</param> |
| 63 | + /// <returns>Whether or not <paramref name="typeSymbol"/> has an attribute matching <paramref name="predicate"/>.</returns> |
| 64 | + public static bool HasOrInheritsAttribute(this ITypeSymbol typeSymbol, Func<AttributeData, bool> predicate) |
| 65 | + { |
| 66 | + for (ITypeSymbol? currentType = typeSymbol; currentType is not null; currentType = currentType.BaseType) |
| 67 | + { |
| 68 | + if (currentType.GetAttributes().Any(predicate)) |
| 69 | + { |
| 70 | + return true; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return false; |
| 75 | + } |
| 76 | + |
| 77 | + /// <summary> |
| 78 | + /// Checks whether or not a given <see cref="ITypeSymbol"/> has or inherits a specified attribute. |
| 79 | + /// </summary> |
| 80 | + /// <param name="typeSymbol">The target <see cref="ITypeSymbol"/> instance to check.</param> |
| 81 | + /// <param name="name">The name of the attribute to look for.</param> |
| 82 | + /// <returns>Whether or not <paramref name="typeSymbol"/> has an attribute with the specified type name.</returns> |
| 83 | + public static bool HasOrInheritsAttributeWithFullyQualifiedName(this ITypeSymbol typeSymbol, string name) |
| 84 | + { |
| 85 | + for (ITypeSymbol? currentType = typeSymbol; currentType is not null; currentType = currentType.BaseType) |
| 86 | + { |
| 87 | + if (currentType.HasAttributeWithFullyQualifiedName(name)) |
| 88 | + { |
| 89 | + return true; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return false; |
| 94 | + } |
| 95 | +} |
0 commit comments