Skip to content

Commit

Permalink
Fixes a problem when the "using" directive is not added to the genera…
Browse files Browse the repository at this point in the history
…ted code for extension methods when using them in factories.
  • Loading branch information
Nikolay Pyanikov committed Jul 28, 2023
1 parent 59560e1 commit 69d9de3
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 16 deletions.
43 changes: 27 additions & 16 deletions src/Pure.DI.Core/Core/NamespacesSyntaxWalker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,36 @@ public override void VisitGenericName(GenericNameSyntax node)

private void AddNamespace(ISymbol? symbol)
{
if (symbol is not INamedTypeSymbol type)
var depth = 256;
while (depth-- > 0)
{
return;
}
switch (symbol)
{
case INamedTypeSymbol type when type.ContainingNamespace.IsGlobalNamespace:
return;

if (type.ContainingNamespace.IsGlobalNamespace)
{
return;
}
case INamedTypeSymbol type:
{
_namespaces.Add(type.ContainingNamespace.ToString());
if (!type.IsGenericType)
{
return;
}

_namespaces.Add(type.ContainingNamespace.ToString());
if (!type.IsGenericType)
{
return;
}

foreach (var typeSymbol in type.TypeArguments)
{
AddNamespace(typeSymbol);
foreach (var typeSymbol in type.TypeArguments)
{
AddNamespace(typeSymbol);
}

break;
}

case IMethodSymbol methodSymbol:
symbol = methodSymbol.ContainingSymbol;
continue;
}

break;
}
}

Expand Down
66 changes: 66 additions & 0 deletions tests/Pure.DI.IntegrationTests/NamespaceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,70 @@ public static void Main()
result.Success.ShouldBeTrue(result);
result.StdOut.ShouldBe(ImmutableArray.Create(".+"), result);
}

[Fact]
public async Task ShouldSupportNamespaceFromExtensionMethod()
{
// Given

// When
var result = await """
using System;
using Pure.DI;

namespace OtherNamespace
{
static class Utils
{
public static Sample.IDependency DoSomething(this Sample.IDependency dep) => dep;
}
}

namespace Sample
{
using OtherNamespace;

interface IDependency {}

class Dependency: IDependency {}

interface IService
{
IDependency Dep { get; }
}

class Service: IService
{
public Service(IDependency dep)
{
Dep = dep;
}

public IDependency Dep { get; }
}

static class Setup
{
private static void SetupComposition()
{
DI.Setup("Composition")
.Bind<IDependency>().To<IDependency>(_ => new Dependency().DoSomething())
.Bind<IService>().To<Service>()
.Root<IService>("Service");
}
}

public class Program
{
public static void Main()
{
var composition = new Composition();
}
}
}
""".RunAsync();

// Then
result.Success.ShouldBeTrue(result);
}
}

0 comments on commit 69d9de3

Please sign in to comment.