|
| 1 | +// Copyright (c) 2024 .NET Foundation and Contributors. All rights reserved. |
| 2 | +// Licensed to the .NET Foundation under one or more agreements. |
| 3 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 4 | +// See the LICENSE file in the project root for full license information. |
| 5 | + |
| 6 | +using System; |
| 7 | +using Microsoft.CodeAnalysis; |
| 8 | +using ReactiveUI.SourceGenerators.Helpers; |
| 9 | + |
| 10 | +namespace ReactiveUI.SourceGenerators.Extensions; |
| 11 | + |
| 12 | +/// <summary> |
| 13 | +/// Extension methods for the <see cref="ITypeSymbol"/> type. |
| 14 | +/// </summary> |
| 15 | +internal static class ITypeSymbolExtensions |
| 16 | +{ |
| 17 | + /// <summary> |
| 18 | + /// Checks whether or not a given <see cref="ITypeSymbol"/> inherits from a specified type. |
| 19 | + /// </summary> |
| 20 | + /// <param name="typeSymbol">The target <see cref="ITypeSymbol"/> instance to check.</param> |
| 21 | + /// <param name="name">The full name of the type to check for inheritance.</param> |
| 22 | + /// <returns>Whether or not <paramref name="typeSymbol"/> inherits from <paramref name="name"/>.</returns> |
| 23 | + public static bool InheritsFromFullyQualifiedMetadataName(this ITypeSymbol typeSymbol, string name) |
| 24 | + { |
| 25 | + var baseType = typeSymbol.BaseType; |
| 26 | + |
| 27 | + while (baseType is not null) |
| 28 | + { |
| 29 | + if (baseType.HasFullyQualifiedMetadataName(name)) |
| 30 | + { |
| 31 | + return true; |
| 32 | + } |
| 33 | + |
| 34 | + baseType = baseType.BaseType; |
| 35 | + } |
| 36 | + |
| 37 | + return false; |
| 38 | + } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Checks whether or not a given <see cref="ITypeSymbol"/> has or inherits a specified attribute. |
| 42 | + /// </summary> |
| 43 | + /// <param name="typeSymbol">The target <see cref="ITypeSymbol"/> instance to check.</param> |
| 44 | + /// <param name="name">The name of the attribute to look for.</param> |
| 45 | + /// <returns>Whether or not <paramref name="typeSymbol"/> has an attribute with the specified type name.</returns> |
| 46 | + public static bool HasOrInheritsAttributeWithFullyQualifiedMetadataName(this ITypeSymbol typeSymbol, string name) |
| 47 | + { |
| 48 | + for (var currentType = typeSymbol; currentType is not null; currentType = currentType.BaseType) |
| 49 | + { |
| 50 | + if (currentType.HasAttributeWithFullyQualifiedMetadataName(name)) |
| 51 | + { |
| 52 | + return true; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + return false; |
| 57 | + } |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// Checks whether or not a given type symbol has a specified fully qualified metadata name. |
| 61 | + /// </summary> |
| 62 | + /// <param name="symbol">The input <see cref="ITypeSymbol"/> instance to check.</param> |
| 63 | + /// <param name="name">The full name to check.</param> |
| 64 | + /// <returns>Whether <paramref name="symbol"/> has a full name equals to <paramref name="name"/>.</returns> |
| 65 | + public static bool HasFullyQualifiedMetadataName(this ITypeSymbol symbol, string name) |
| 66 | + { |
| 67 | + using var builder = ImmutableArrayBuilder<char>.Rent(); |
| 68 | + |
| 69 | + symbol.AppendFullyQualifiedMetadataName(builder); |
| 70 | + |
| 71 | + return builder.WrittenSpan.StartsWith(name.AsSpan()); |
| 72 | + } |
| 73 | + |
| 74 | + /// <summary> |
| 75 | + /// Appends the fully qualified metadata name for a given symbol to a target builder. |
| 76 | + /// </summary> |
| 77 | + /// <param name="symbol">The input <see cref="ITypeSymbol"/> instance.</param> |
| 78 | + /// <param name="builder">The target <see cref="ImmutableArrayBuilder{T}"/> instance.</param> |
| 79 | + private static void AppendFullyQualifiedMetadataName(this ITypeSymbol symbol, ImmutableArrayBuilder<char> builder) |
| 80 | + { |
| 81 | + static void BuildFrom(ISymbol? symbol, ImmutableArrayBuilder<char> builder) |
| 82 | + { |
| 83 | + switch (symbol) |
| 84 | + { |
| 85 | + // Namespaces that are nested also append a leading '.' |
| 86 | + case INamespaceSymbol { ContainingNamespace.IsGlobalNamespace: false }: |
| 87 | + BuildFrom(symbol.ContainingNamespace, builder); |
| 88 | + builder.Add('.'); |
| 89 | + builder.AddRange(symbol.MetadataName.AsSpan()); |
| 90 | + break; |
| 91 | + |
| 92 | + // Other namespaces (ie. the one right before global) skip the leading '.' |
| 93 | + case INamespaceSymbol { IsGlobalNamespace: false }: |
| 94 | + builder.AddRange(symbol.MetadataName.AsSpan()); |
| 95 | + break; |
| 96 | + |
| 97 | + // Types with no namespace just have their metadata name directly written |
| 98 | + case ITypeSymbol { ContainingSymbol: INamespaceSymbol { IsGlobalNamespace: true } }: |
| 99 | + builder.AddRange(symbol.MetadataName.AsSpan()); |
| 100 | + break; |
| 101 | + |
| 102 | + // Types with a containing non-global namespace also append a leading '.' |
| 103 | + case ITypeSymbol { ContainingSymbol: INamespaceSymbol namespaceSymbol }: |
| 104 | + BuildFrom(namespaceSymbol, builder); |
| 105 | + builder.Add('.'); |
| 106 | + builder.AddRange(symbol.MetadataName.AsSpan()); |
| 107 | + break; |
| 108 | + |
| 109 | + // Nested types append a leading '+' |
| 110 | + case ITypeSymbol { ContainingSymbol: ITypeSymbol typeSymbol }: |
| 111 | + BuildFrom(typeSymbol, builder); |
| 112 | + builder.Add('+'); |
| 113 | + builder.AddRange(symbol.MetadataName.AsSpan()); |
| 114 | + break; |
| 115 | + default: |
| 116 | + break; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + BuildFrom(symbol, builder); |
| 121 | + } |
| 122 | +} |
0 commit comments