Skip to content
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
1 change: 1 addition & 0 deletions ServiceScan.SourceGenerator.Tests/AddServicesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,7 @@ public class MyService2 : IService { }
public class ParentType2
{
public class MyService1 : IService { }
private class NestedPrivateService : IService { } // Shouldn't be added as non-accessible
}
""");

Expand Down
3 changes: 3 additions & 0 deletions ServiceScan.SourceGenerator.Tests/TestServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
public interface IExternalService;
public class ExternalService1 : IExternalService { }
public class ExternalService2 : IExternalService { }

// Shouldn't be added as type is not accessible from other assembly
internal class InternalExternalService2 : IExternalService { }
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public partial class DependencyInjectionGenerator
private static IEnumerable<(INamedTypeSymbol Type, INamedTypeSymbol[]? MatchedAssignableTypes)> FilterTypes
(Compilation compilation, AttributeModel attribute, INamedTypeSymbol containingType)
{
var semanticModel = compilation.GetSemanticModel(attribute.Location.SourceTree);
var position = attribute.Location.SourceSpan.Start;

var assemblies = GetAssembliesToScan(compilation, attribute, containingType);

var assignableToType = attribute.AssignableToTypeName is null
Expand Down Expand Up @@ -76,6 +79,9 @@ public partial class DependencyInjectionGenerator
if (assignableToType != null && !IsAssignableTo(type, assignableToType, out matchedTypes))
continue;

if (!semanticModel.IsAccessible(position, type))
continue;

yield return (type, matchedTypes);
}
}
Expand Down
4 changes: 2 additions & 2 deletions ServiceScan.SourceGenerator/DependencyInjectionGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
{
context.RegisterPostInitializationOutput(context =>
{
context.AddSource("ServiceScanAttributes.Generated.cs", SourceText.From(GenerateAttributeSource.Source, Encoding.UTF8));
context.AddSource("ServiceScanAttributes.Generated.cs", SourceText.From(GenerateAttributeInfo.Source, Encoding.UTF8));
});

var methodProvider = context.SyntaxProvider.ForAttributeWithMetadataName(
"ServiceScan.SourceGenerator.GenerateServiceRegistrationsAttribute",
GenerateAttributeInfo.MetadataName,
predicate: static (syntaxNode, ct) => syntaxNode is MethodDeclarationSyntax methodSyntax,
transform: static (context, ct) => ParseRegisterMethodModel(context))
.Where(method => method != null);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
namespace ServiceScan.SourceGenerator;

internal static class GenerateAttributeSource
internal static class GenerateAttributeInfo
{
public static string Source => """
public const string MetadataName = "ServiceScan.SourceGenerator.GenerateServiceRegistrationsAttribute";

public const string Source = """
#nullable enable

using System;
Expand Down
Loading