Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add nested types #17

Merged
merged 1 commit into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
36 changes: 36 additions & 0 deletions ServiceScan.SourceGenerator.Tests/AddServicesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,42 @@ public class MyService: IServiceA, IServiceB {}
Assert.Equal(Sources.GetMethodImplementation(registrations), results.GeneratedTrees[1].ToString());
}

[Fact]
public void AddNestedTypes()
{
var attribute = "[GenerateServiceRegistrations(AssignableTo = typeof(IService))]";
var compilation = CreateCompilation(Sources.MethodWithAttribute(attribute),
"""
namespace GeneratorTests;

public interface IService { }

public class ParentType1
{
public class MyService1 : IService { }
public class MyService2 : IService { }
}

public class ParentType2
{
public class MyService1 : IService { }
}
""");

var results = CSharpGeneratorDriver
.Create(_generator)
.RunGenerators(compilation)
.GetRunResult();

var registrations = $"""
return services
.AddTransient<GeneratorTests.IService, GeneratorTests.ParentType1.MyService1>()
.AddTransient<GeneratorTests.IService, GeneratorTests.ParentType1.MyService2>()
.AddTransient<GeneratorTests.IService, GeneratorTests.ParentType2.MyService1>();
""";
Assert.Equal(Sources.GetMethodImplementation(registrations), results.GeneratedTrees[1].ToString());
}

[Fact]
public void AddAsKeyedServices_GenericMethod()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,20 @@ private static bool IsAssignableTo(INamedTypeSymbol type, INamedTypeSymbol assig
private static IEnumerable<INamedTypeSymbol> GetTypesFromAssembly(IAssemblySymbol assemblySymbol)
{
var @namespace = assemblySymbol.GlobalNamespace;
return GetTypesFromNamespace(@namespace);
return GetTypesFromNamespaceOrType(@namespace);

static IEnumerable<INamedTypeSymbol> GetTypesFromNamespace(INamespaceSymbol namespaceSymbol)
static IEnumerable<INamedTypeSymbol> GetTypesFromNamespaceOrType(INamespaceOrTypeSymbol symbol)
{
foreach (var member in namespaceSymbol.GetMembers())
foreach (var member in symbol.GetMembers())
{
if (member is INamedTypeSymbol namedType)
if (member is INamespaceOrTypeSymbol namespaceOrType)
{
yield return namedType;
}
else if (member is INamespaceSymbol nestedNamespace)
{
foreach (var type in GetTypesFromNamespace(nestedNamespace))
if (member is INamedTypeSymbol namedType)
{
yield return namedType;
}

foreach (var type in GetTypesFromNamespaceOrType(namespaceOrType))
{
yield return type;
}
Expand Down
Loading