From 924170ee5b4ab0bbad89753ecdb2c11b6c27febe Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Sun, 6 Oct 2024 21:32:17 +1100 Subject: [PATCH 1/2] cleanup --- .../SymbolExtensions.cs | 19 ++++++++++++++++--- .../UsesVerifyGenerator.cs | 16 +++++++++------- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/Verify.MSTest.SourceGenerator/SymbolExtensions.cs b/src/Verify.MSTest.SourceGenerator/SymbolExtensions.cs index 87d0daae6..da8a020b8 100644 --- a/src/Verify.MSTest.SourceGenerator/SymbolExtensions.cs +++ b/src/Verify.MSTest.SourceGenerator/SymbolExtensions.cs @@ -23,13 +23,26 @@ public static bool HasAttributeOfType(this ISymbol symbol, string fullyQualified return true; } - typeSymbol = includeDerived ? typeSymbol.BaseType : null; + if (includeDerived) + { + typeSymbol = typeSymbol.BaseType; + continue; + } + + typeSymbol = null; } } return false; } - public static string? GetNamespaceOrDefault(this ISymbol symbol) => - symbol.ContainingNamespace.IsGlobalNamespace ? null : symbol.ContainingNamespace.ToString(); + public static string? GetNamespaceOrDefault(this ISymbol symbol) + { + if (symbol.ContainingNamespace.IsGlobalNamespace) + { + return null; + } + + return symbol.ContainingNamespace.ToString(); + } } diff --git a/src/Verify.MSTest.SourceGenerator/UsesVerifyGenerator.cs b/src/Verify.MSTest.SourceGenerator/UsesVerifyGenerator.cs index bf2aaca98..9d38c937e 100644 --- a/src/Verify.MSTest.SourceGenerator/UsesVerifyGenerator.cs +++ b/src/Verify.MSTest.SourceGenerator/UsesVerifyGenerator.cs @@ -8,7 +8,7 @@ public class UsesVerifyGenerator : IIncrementalGenerator public void Initialize(IncrementalGeneratorInitializationContext context) { - var markerAttributeClassesToGenerate = context.SyntaxProvider + var markerAttributesToGenerate = context.SyntaxProvider .ForAttributeWithMetadataName( fullyQualifiedMetadataName: MarkerAttributeName, predicate: IsSyntaxEligibleForGeneration, @@ -41,7 +41,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context) .WithTrackingName(TrackingNames.MarkerAttributeInitialTransform) .Collect(); - var assemblyAttributeClassesToGenerate = context.SyntaxProvider + var assemblyAttributesToGenerate = context.SyntaxProvider .CreateSyntaxProvider( predicate: IsSyntaxEligibleForGeneration, transform: static (context, cancel) => @@ -85,7 +85,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context) // Collect the classes to generate into a single collection so that we can write them to a single file and // avoid the issues of ambiguous hint names discussed in https://github.com/dotnet/roslyn/discussions/60272. - var classesToGenerate = markerAttributeClassesToGenerate.Combine(assemblyAttributeClassesToGenerate) + var classesToGenerate = markerAttributesToGenerate.Combine(assemblyAttributesToGenerate) .SelectMany((classes, _) => classes.Left.AddRange(classes.Right)) .WithTrackingName(TrackingNames.Merge) .Collect() @@ -94,17 +94,19 @@ public void Initialize(IncrementalGeneratorInitializationContext context) context.RegisterSourceOutput(classesToGenerate, Execute); } - static bool IsSyntaxEligibleForGeneration(SyntaxNode node, Cancel _) => node is ClassDeclarationSyntax; + static bool IsSyntaxEligibleForGeneration(SyntaxNode node, Cancel _) => + node is ClassDeclarationSyntax; - static bool IsAssemblyEligibleForGeneration(IAssemblySymbol assembly) => assembly.HasAttributeOfType(MarkerAttributeName, includeDerived: false); + static bool IsAssemblyEligibleForGeneration(IAssemblySymbol assembly) => + assembly.HasAttributeOfType(MarkerAttributeName, includeDerived: false); static bool HasParentWithMarkerAttribute(INamedTypeSymbol symbol) => symbol .GetBaseTypes() - .Any(parent => parent.HasAttributeOfType(MarkerAttributeName, includeDerived: false)); + .Any(_ => _.HasAttributeOfType(MarkerAttributeName, includeDerived: false)); static bool HasParentWithTestClassAttribute(INamedTypeSymbol symbol) => symbol .GetBaseTypes() - .Any(parent => parent.HasAttributeOfType(TestClassAttributeName, includeDerived: true)); + .Any(_ => _.HasAttributeOfType(TestClassAttributeName, includeDerived: true)); static void Execute(SourceProductionContext context, ImmutableArray classesToGenerate) { From e17812c4fb60a3ad1acfb50fa0c8c67f72f3a3fd Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Sun, 6 Oct 2024 21:39:48 +1100 Subject: [PATCH 2/2] cleanup --- .../{SymbolExtensions.cs => Extensions.cs} | 19 ++++++++++++++----- .../IncrementalValuesProviderExtensions.cs | 10 ---------- .../SyntaxExtensions.cs | 5 ----- .../UsesVerifyGenerator.cs | 18 +++++++++++------- 4 files changed, 25 insertions(+), 27 deletions(-) rename src/Verify.MSTest.SourceGenerator/{SymbolExtensions.cs => Extensions.cs} (63%) delete mode 100644 src/Verify.MSTest.SourceGenerator/IncrementalValuesProviderExtensions.cs delete mode 100644 src/Verify.MSTest.SourceGenerator/SyntaxExtensions.cs diff --git a/src/Verify.MSTest.SourceGenerator/SymbolExtensions.cs b/src/Verify.MSTest.SourceGenerator/Extensions.cs similarity index 63% rename from src/Verify.MSTest.SourceGenerator/SymbolExtensions.cs rename to src/Verify.MSTest.SourceGenerator/Extensions.cs index da8a020b8..8f45df519 100644 --- a/src/Verify.MSTest.SourceGenerator/SymbolExtensions.cs +++ b/src/Verify.MSTest.SourceGenerator/Extensions.cs @@ -1,8 +1,11 @@ -static class SymbolExtensions +static class Extensions { - public static IEnumerable GetBaseTypes(this ITypeSymbol? symbol) + public static string GetTypeNameWithGenericParameters(this TypeDeclarationSyntax syntax) => + syntax.Identifier.ToString() + syntax.TypeParameterList; + + public static IEnumerable GetBaseTypes(this ITypeSymbol symbol) { - var baseType = symbol?.BaseType; + var baseType = symbol.BaseType; while (baseType is not null) { @@ -11,6 +14,11 @@ public static IEnumerable GetBaseTypes(this ITypeSymbol? symbo } } + public static IncrementalValuesProvider WhereNotNull(this IncrementalValuesProvider source) where TSource : struct => + source + .Where(_ => _.HasValue) + .Select((item, _) => item!.Value); + public static bool HasAttributeOfType(this ISymbol symbol, string fullyQualifiedAttributeName, bool includeDerived) { foreach (var attribute in symbol.GetAttributes()) @@ -38,11 +46,12 @@ public static bool HasAttributeOfType(this ISymbol symbol, string fullyQualified public static string? GetNamespaceOrDefault(this ISymbol symbol) { - if (symbol.ContainingNamespace.IsGlobalNamespace) + var ns = symbol.ContainingNamespace; + if (ns.IsGlobalNamespace) { return null; } - return symbol.ContainingNamespace.ToString(); + return ns.ToString(); } } diff --git a/src/Verify.MSTest.SourceGenerator/IncrementalValuesProviderExtensions.cs b/src/Verify.MSTest.SourceGenerator/IncrementalValuesProviderExtensions.cs deleted file mode 100644 index b9a4dc8d5..000000000 --- a/src/Verify.MSTest.SourceGenerator/IncrementalValuesProviderExtensions.cs +++ /dev/null @@ -1,10 +0,0 @@ -static class IncrementalValuesProviderExtensions -{ - public static IncrementalValuesProvider WhereNotNull(this IncrementalValuesProvider source) where TSource : struct => - source.Where(_ => _.HasValue) - .Select((item, _) => item!.Value); - - public static IncrementalValuesProvider WhereNotNull(this IncrementalValuesProvider source) => - source.Where(_ => _ is not null) - .Select((item, _) => item!); -} diff --git a/src/Verify.MSTest.SourceGenerator/SyntaxExtensions.cs b/src/Verify.MSTest.SourceGenerator/SyntaxExtensions.cs deleted file mode 100644 index 7e2fa2c74..000000000 --- a/src/Verify.MSTest.SourceGenerator/SyntaxExtensions.cs +++ /dev/null @@ -1,5 +0,0 @@ -static class SyntaxExtensions -{ - public static string GetTypeNameWithGenericParameters(this TypeDeclarationSyntax syntax) => - syntax.Identifier.ToString() + syntax.TypeParameterList; -} diff --git a/src/Verify.MSTest.SourceGenerator/UsesVerifyGenerator.cs b/src/Verify.MSTest.SourceGenerator/UsesVerifyGenerator.cs index 9d38c937e..abb133c00 100644 --- a/src/Verify.MSTest.SourceGenerator/UsesVerifyGenerator.cs +++ b/src/Verify.MSTest.SourceGenerator/UsesVerifyGenerator.cs @@ -51,12 +51,13 @@ public void Initialize(IncrementalGeneratorInitializationContext context) return null; } - if (!IsAssemblyEligibleForGeneration(context.SemanticModel.Compilation.Assembly)) + var model = context.SemanticModel; + if (!IsAssemblyEligibleForGeneration(model.Compilation.Assembly)) { return null; } - if (context.SemanticModel.GetDeclaredSymbol(syntax, cancel) is not INamedTypeSymbol symbol) + if (model.GetDeclaredSymbol(syntax, cancel) is not INamedTypeSymbol symbol) { return null; } @@ -70,7 +71,8 @@ public void Initialize(IncrementalGeneratorInitializationContext context) // Only run generator for classes when the parent won't _also_ have generation. // Otherwise the generator will hide the base member. - if (HasParentWithTestClassAttribute(symbol) || HasParentWithMarkerAttribute(symbol)) + if (HasParentWithTestClassAttribute(symbol) || + HasParentWithMarkerAttribute(symbol)) { return null; } @@ -85,13 +87,13 @@ public void Initialize(IncrementalGeneratorInitializationContext context) // Collect the classes to generate into a single collection so that we can write them to a single file and // avoid the issues of ambiguous hint names discussed in https://github.com/dotnet/roslyn/discussions/60272. - var classesToGenerate = markerAttributesToGenerate.Combine(assemblyAttributesToGenerate) + var toGenerate = markerAttributesToGenerate.Combine(assemblyAttributesToGenerate) .SelectMany((classes, _) => classes.Left.AddRange(classes.Right)) .WithTrackingName(TrackingNames.Merge) .Collect() .WithTrackingName(TrackingNames.Complete); - context.RegisterSourceOutput(classesToGenerate, Execute); + context.RegisterSourceOutput(toGenerate, Execute); } static bool IsSyntaxEligibleForGeneration(SyntaxNode node, Cancel _) => @@ -100,11 +102,13 @@ static bool IsSyntaxEligibleForGeneration(SyntaxNode node, Cancel _) => static bool IsAssemblyEligibleForGeneration(IAssemblySymbol assembly) => assembly.HasAttributeOfType(MarkerAttributeName, includeDerived: false); - static bool HasParentWithMarkerAttribute(INamedTypeSymbol symbol) => symbol + static bool HasParentWithMarkerAttribute(INamedTypeSymbol symbol) => + symbol .GetBaseTypes() .Any(_ => _.HasAttributeOfType(MarkerAttributeName, includeDerived: false)); - static bool HasParentWithTestClassAttribute(INamedTypeSymbol symbol) => symbol + static bool HasParentWithTestClassAttribute(INamedTypeSymbol symbol) => + symbol .GetBaseTypes() .Any(_ => _.HasAttributeOfType(TestClassAttributeName, includeDerived: true));