|
| 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.Collections.Immutable; |
| 7 | +using System.IO; |
| 8 | +using System.Linq; |
| 9 | +using System.Reflection; |
| 10 | +using CommunityToolkit.Mvvm.SourceGenerators.Extensions; |
| 11 | +using Microsoft.CodeAnalysis; |
| 12 | +using Microsoft.CodeAnalysis.CSharp; |
| 13 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 14 | +using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; |
| 15 | + |
| 16 | +namespace CommunityToolkit.Mvvm.SourceGenerators; |
| 17 | + |
| 18 | +/// <inheritdoc/> |
| 19 | +partial class TransitiveMembersGenerator<TInfo> |
| 20 | +{ |
| 21 | + /// <summary> |
| 22 | + /// A container for all the logic for <see cref="TransitiveMembersGenerator{TInfo}"/>. |
| 23 | + /// </summary> |
| 24 | + internal static class Execute |
| 25 | + { |
| 26 | + /// <summary> |
| 27 | + /// Loads the source <see cref="ClassDeclarationSyntax"/> instance to get member declarations from. |
| 28 | + /// </summary> |
| 29 | + /// <param name="attributeType">The fully qualified name of the attribute type to look for.</param> |
| 30 | + /// <returns>The source <see cref="ClassDeclarationSyntax"/> instance to get member declarations from.</returns> |
| 31 | + public static ClassDeclarationSyntax LoadClassDeclaration(string attributeType) |
| 32 | + { |
| 33 | + string attributeTypeName = attributeType.Split('.').Last(); |
| 34 | + string filename = $"CommunityToolkit.Mvvm.SourceGenerators.EmbeddedResources.{attributeTypeName.Replace("Attribute", string.Empty)}.cs"; |
| 35 | + |
| 36 | + using Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(filename); |
| 37 | + using StreamReader reader = new(stream); |
| 38 | + |
| 39 | + string observableObjectSource = reader.ReadToEnd(); |
| 40 | + SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(observableObjectSource); |
| 41 | + |
| 42 | + return syntaxTree.GetRoot().DescendantNodes().OfType<ClassDeclarationSyntax>().First(); |
| 43 | + } |
| 44 | + |
| 45 | + /// <summary> |
| 46 | + /// Processes the sequence of member declarations to generate. |
| 47 | + /// </summary> |
| 48 | + /// <param name="generatorType">The type of generator being used.</param> |
| 49 | + /// <param name="memberDeclarations">The input sequence of member declarations to generate.</param> |
| 50 | + /// <param name="sealedMemberDeclarations">The resulting sequence of member declarations for sealed types.</param> |
| 51 | + /// <param name="nonSealedMemberDeclarations">The resulting sequence of member declarations for non sealed types.</param> |
| 52 | + public static void ProcessMemberDeclarations( |
| 53 | + Type generatorType, |
| 54 | + ImmutableArray<MemberDeclarationSyntax> memberDeclarations, |
| 55 | + out ImmutableArray<MemberDeclarationSyntax> sealedMemberDeclarations, |
| 56 | + out ImmutableArray<MemberDeclarationSyntax> nonSealedMemberDeclarations) |
| 57 | + { |
| 58 | + ImmutableArray<MemberDeclarationSyntax> annotatedMemberDeclarations = memberDeclarations.Select(member => |
| 59 | + { |
| 60 | + // [GeneratedCode] is always present |
| 61 | + member = |
| 62 | + member |
| 63 | + .WithoutLeadingTrivia() |
| 64 | + .AddAttributeLists(AttributeList(SingletonSeparatedList( |
| 65 | + Attribute(IdentifierName($"global::System.CodeDom.Compiler.GeneratedCode")) |
| 66 | + .AddArgumentListArguments( |
| 67 | + AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(generatorType.FullName))), |
| 68 | + AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(generatorType.Assembly.GetName().Version.ToString()))))))) |
| 69 | + .WithLeadingTrivia(member.GetLeadingTrivia()); |
| 70 | + |
| 71 | + // [DebuggerNonUserCode] is not supported over interfaces, events or fields |
| 72 | + if (member.Kind() is not SyntaxKind.InterfaceDeclaration and not SyntaxKind.EventFieldDeclaration and not SyntaxKind.FieldDeclaration) |
| 73 | + { |
| 74 | + member = member.AddAttributeLists(AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.Diagnostics.DebuggerNonUserCode"))))); |
| 75 | + } |
| 76 | + |
| 77 | + // [ExcludeFromCodeCoverage] is not supported on interfaces and fields |
| 78 | + if (member.Kind() is not SyntaxKind.InterfaceDeclaration and not SyntaxKind.FieldDeclaration) |
| 79 | + { |
| 80 | + member = member.AddAttributeLists(AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage"))))); |
| 81 | + } |
| 82 | + |
| 83 | + return member; |
| 84 | + }).ToImmutableArray(); |
| 85 | + |
| 86 | + // If the target class is sealed, make protected members private and remove the virtual modifier |
| 87 | + sealedMemberDeclarations = annotatedMemberDeclarations.Select(static member => |
| 88 | + { |
| 89 | + return |
| 90 | + member |
| 91 | + .ReplaceModifier(SyntaxKind.ProtectedKeyword, SyntaxKind.PrivateKeyword) |
| 92 | + .RemoveModifier(SyntaxKind.VirtualKeyword); |
| 93 | + }).ToImmutableArray(); |
| 94 | + |
| 95 | + nonSealedMemberDeclarations = annotatedMemberDeclarations; |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments