Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Enhance PropertyInjectionSourceGenerator to skip non-public and unbou…
…nd generic types; deduplicate class sources for improved efficiency
  • Loading branch information
thomhurst committed Sep 9, 2025
commit fcce4d68a0a9c43a9a4b290a7d7d8d2e0c526823
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Collections.Immutable;
using System.Text;
using Microsoft.CodeAnalysis;
Expand Down Expand Up @@ -39,6 +38,19 @@ private static bool IsClassWithDataSourceProperties(SyntaxNode node)
return null;
}

// Skip types that are not publicly accessible to avoid accessibility issues
// Also check if the type is nested and ensure the containing types are also public
if (!IsPubliclyAccessible(typeSymbol))
{
return null;
}

// Skip open generic types (unbound type parameters) as they cannot be instantiated
if (typeSymbol.IsUnboundGenericType || typeSymbol.TypeParameters.Length > 0)
{
return null;
}

var propertiesWithDataSources = new List<PropertyWithDataSourceAttribute>();
var dataSourceInterface = semanticModel.Compilation.GetTypeByMetadataName("TUnit.Core.IDataSourceAttribute");

Expand Down Expand Up @@ -90,6 +102,36 @@ private static bool CanSetProperty(IPropertySymbol property)
return property.SetMethod != null || property.SetMethod?.IsInitOnly == true;
}

private static bool IsPubliclyAccessible(INamedTypeSymbol typeSymbol)
{
// Check if the type itself is public
if (typeSymbol.DeclaredAccessibility != Accessibility.Public)
{
return false;
}

// If it's a nested type, ensure all containing types are also public
// and don't have unbound type parameters
var containingType = typeSymbol.ContainingType;
while (containingType != null)
{
if (containingType.DeclaredAccessibility != Accessibility.Public)
{
return false;
}

// Check if the containing type has unbound type parameters
if (containingType.IsUnboundGenericType || containingType.TypeParameters.Length > 0)
{
return false;
}

containingType = containingType.ContainingType;
}

return true;
}

private static void GeneratePropertyInjectionSources(SourceProductionContext context, ImmutableArray<ClassWithDataSourceProperties> classes)
{
if (classes.IsEmpty)
Expand All @@ -101,17 +143,23 @@ private static void GeneratePropertyInjectionSources(SourceProductionContext con

WriteFileHeader(sourceBuilder);

// Deduplicate classes by symbol to prevent duplicate source generation
var uniqueClasses = classes
.GroupBy(c => c.ClassSymbol, SymbolEqualityComparer.Default)
.Select(g => g.First())
.ToImmutableArray();

// Generate all property sources first with stable names
var classNameMapping = new Dictionary<INamedTypeSymbol, string>(SymbolEqualityComparer.Default);
foreach (var classInfo in classes)
foreach (var classInfo in uniqueClasses)
{
var sourceClassName = GetPropertySourceClassName(classInfo.ClassSymbol);
classNameMapping[classInfo.ClassSymbol] = sourceClassName;
}

GenerateModuleInitializer(sourceBuilder, classes, classNameMapping);
GenerateModuleInitializer(sourceBuilder, uniqueClasses, classNameMapping);

foreach (var classInfo in classes)
foreach (var classInfo in uniqueClasses)
{
GeneratePropertySource(sourceBuilder, classInfo, classNameMapping[classInfo.ClassSymbol]);
}
Expand Down Expand Up @@ -310,9 +358,10 @@ private static string GetPropertyCastExpression(IPropertySymbol property, string

private static string GetPropertySourceClassName(INamedTypeSymbol classSymbol)
{
// Use a random GUID for uniqueness
var guid = Guid.NewGuid();
return $"PropertyInjectionSource_{guid:N}";
// Use a deterministic hash based on the fully qualified type name for uniqueness
var fullTypeName = classSymbol.ToDisplayString();
var hash = fullTypeName.GetHashCode();
return $"PropertyInjectionSource_{Math.Abs(hash):x}";
}

private static string FormatTypedConstant(TypedConstant constant)
Expand Down
1 change: 0 additions & 1 deletion TestProject.targets
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
<ItemGroup>
<ProjectReference Include="$(MSBuildThisFileDirectory)TUnit.Engine\TUnit.Engine.csproj" />
<ProjectReference Include="$(MSBuildThisFileDirectory)TUnit.Assertions\TUnit.Assertions.csproj" />
<PackageReference Include="Microsoft.Testing.Platform.MSBuild" />

<ProjectReference
Include="$(MSBuildThisFileDirectory)TUnit.Assertions.Analyzers\TUnit.Assertions.Analyzers.csproj"
Expand Down
Loading