From d5cb56aec931e8aeaa643e75dd7dbaf0ef32caaa Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 28 Mar 2021 18:25:51 +0200 Subject: [PATCH] Minor code refactoring --- .../TransitiveMembersGenerator.cs | 24 +++---------------- .../EmbeddedResources/ObservableRecipient.cs | 2 +- 2 files changed, 4 insertions(+), 22 deletions(-) diff --git a/Microsoft.Toolkit.Mvvm.SourceGenerators/ComponentModel/TransitiveMembersGenerator.cs b/Microsoft.Toolkit.Mvvm.SourceGenerators/ComponentModel/TransitiveMembersGenerator.cs index bb11cd04..2f6ae7d0 100644 --- a/Microsoft.Toolkit.Mvvm.SourceGenerators/ComponentModel/TransitiveMembersGenerator.cs +++ b/Microsoft.Toolkit.Mvvm.SourceGenerators/ComponentModel/TransitiveMembersGenerator.cs @@ -107,20 +107,6 @@ namespace Microsoft.Toolkit.Mvvm.SourceGenerators { ClassDeclarationSyntax sourceDeclaration = sourceSyntaxTree.GetRoot().DescendantNodes().OfType().First(); UsingDirectiveSyntax[] usingDirectives = sourceSyntaxTree.GetRoot().DescendantNodes().OfType().ToArray(); - BaseListSyntax? baseListSyntax = BaseList(SeparatedList( - sourceDeclaration.BaseList?.Types - .OfType() - .Select(static t => t.Type) - .OfType() - .Where(static t => t.Identifier.ValueText.StartsWith("I")) - .Select(static t => SimpleBaseType(t)) - .ToArray() - ?? Array.Empty())); - - if (baseListSyntax.Types.Count == 0) - { - baseListSyntax = null; - } // Create the class declaration for the user type. This will produce a tree as follows: // @@ -131,7 +117,7 @@ namespace Microsoft.Toolkit.Mvvm.SourceGenerators var classDeclarationSyntax = ClassDeclaration(classDeclaration.Identifier.Text) .WithModifiers(classDeclaration.Modifiers) - .WithBaseList(baseListSyntax) + .WithBaseList(sourceDeclaration.BaseList) .AddMembers(FilterDeclaredMembers(context, attributeData, classDeclaration, classDeclarationSymbol, sourceDeclaration).ToArray()); TypeDeclarationSyntax typeDeclarationSyntax = classDeclarationSyntax; @@ -151,16 +137,12 @@ namespace Microsoft.Toolkit.Mvvm.SourceGenerators // From this, we can finally generate the source code to output. var namespaceName = classDeclarationSymbol.ContainingNamespace.ToDisplayString(new(typeQualificationStyle: NameAndContainingTypesAndNamespaces)); + // Create the final compilation unit to generate (with using directives and the full type declaration) var source = CompilationUnit() .AddMembers(NamespaceDeclaration(IdentifierName(namespaceName)) .AddMembers(typeDeclarationSyntax)) - .AddUsings(usingDirectives.First().WithLeadingTrivia(TriviaList( - Comment("// Licensed to the .NET Foundation under one or more agreements."), - Comment("// The .NET Foundation licenses this file to you under the MIT license."), - Comment("// See the LICENSE file in the project root for more information."), - Trivia(PragmaWarningDirectiveTrivia(Token(SyntaxKind.DisableKeyword), true))))) - .AddUsings(usingDirectives.Skip(1).ToArray()) + .AddUsings(usingDirectives) .NormalizeWhitespace() .ToFullString(); diff --git a/Microsoft.Toolkit.Mvvm.SourceGenerators/EmbeddedResources/ObservableRecipient.cs b/Microsoft.Toolkit.Mvvm.SourceGenerators/EmbeddedResources/ObservableRecipient.cs index 7896e6b9..7398d933 100644 --- a/Microsoft.Toolkit.Mvvm.SourceGenerators/EmbeddedResources/ObservableRecipient.cs +++ b/Microsoft.Toolkit.Mvvm.SourceGenerators/EmbeddedResources/ObservableRecipient.cs @@ -18,7 +18,7 @@ namespace Microsoft.Toolkit.Mvvm.ComponentModel /// A base class for observable objects that also acts as recipients for messages. This class is an extension of /// which also provides built-in support to use the type. /// - public abstract class ObservableRecipient : ObservableObject + public abstract class ObservableRecipient { /// /// Initializes a new instance of the class.