Skip to content

Commit

Permalink
Merge branch 'main' into split-params-docs
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Oct 6, 2024
2 parents 6ec256f + e17812c commit 6bb6d94
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 63 deletions.
57 changes: 57 additions & 0 deletions src/Verify.MSTest.SourceGenerator/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
static class Extensions
{
public static string GetTypeNameWithGenericParameters(this TypeDeclarationSyntax syntax) =>
syntax.Identifier.ToString() + syntax.TypeParameterList;

public static IEnumerable<INamedTypeSymbol> GetBaseTypes(this ITypeSymbol symbol)
{
var baseType = symbol.BaseType;

while (baseType is not null)
{
yield return baseType;
baseType = baseType.BaseType;
}
}

public static IncrementalValuesProvider<TSource> WhereNotNull<TSource>(this IncrementalValuesProvider<TSource?> 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())
{
var typeSymbol = attribute.AttributeClass;
while (typeSymbol is not null)
{
if (typeSymbol.ToDisplayString() == fullyQualifiedAttributeName)
{
return true;
}

if (includeDerived)
{
typeSymbol = typeSymbol.BaseType;
continue;
}

typeSymbol = null;
}
}

return false;
}

public static string? GetNamespaceOrDefault(this ISymbol symbol)
{
var ns = symbol.ContainingNamespace;
if (ns.IsGlobalNamespace)
{
return null;
}

return ns.ToString();
}
}

This file was deleted.

35 changes: 0 additions & 35 deletions src/Verify.MSTest.SourceGenerator/SymbolExtensions.cs

This file was deleted.

5 changes: 0 additions & 5 deletions src/Verify.MSTest.SourceGenerator/SyntaxExtensions.cs

This file was deleted.

32 changes: 19 additions & 13 deletions src/Verify.MSTest.SourceGenerator/UsesVerifyGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) =>
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -85,26 +87,30 @@ 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 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 _) => 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
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
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<ClassToGenerate> classesToGenerate)
{
Expand Down

0 comments on commit 6bb6d94

Please sign in to comment.