Skip to content

Commit a103969

Browse files
authored
fix: Verify types are accessible (#38)
1 parent 1aa340d commit a103969

File tree

5 files changed

+16
-4
lines changed

5 files changed

+16
-4
lines changed

ServiceScan.SourceGenerator.Tests/AddServicesTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,7 @@ public class MyService2 : IService { }
956956
public class ParentType2
957957
{
958958
public class MyService1 : IService { }
959+
private class NestedPrivateService : IService { } // Shouldn't be added as non-accessible
959960
}
960961
""");
961962

ServiceScan.SourceGenerator.Tests/TestServices.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@
33
public interface IExternalService;
44
public class ExternalService1 : IExternalService { }
55
public class ExternalService2 : IExternalService { }
6+
7+
// Shouldn't be added as type is not accessible from other assembly
8+
internal class InternalExternalService2 : IExternalService { }

ServiceScan.SourceGenerator/DependencyInjectionGenerator.FilterTypes.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ public partial class DependencyInjectionGenerator
1313
private static IEnumerable<(INamedTypeSymbol Type, INamedTypeSymbol[]? MatchedAssignableTypes)> FilterTypes
1414
(Compilation compilation, AttributeModel attribute, INamedTypeSymbol containingType)
1515
{
16+
var semanticModel = compilation.GetSemanticModel(attribute.Location.SourceTree);
17+
var position = attribute.Location.SourceSpan.Start;
18+
1619
var assemblies = GetAssembliesToScan(compilation, attribute, containingType);
1720

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

82+
if (!semanticModel.IsAccessible(position, type))
83+
continue;
84+
7985
yield return (type, matchedTypes);
8086
}
8187
}

ServiceScan.SourceGenerator/DependencyInjectionGenerator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
1515
{
1616
context.RegisterPostInitializationOutput(context =>
1717
{
18-
context.AddSource("ServiceScanAttributes.Generated.cs", SourceText.From(GenerateAttributeSource.Source, Encoding.UTF8));
18+
context.AddSource("ServiceScanAttributes.Generated.cs", SourceText.From(GenerateAttributeInfo.Source, Encoding.UTF8));
1919
});
2020

2121
var methodProvider = context.SyntaxProvider.ForAttributeWithMetadataName(
22-
"ServiceScan.SourceGenerator.GenerateServiceRegistrationsAttribute",
22+
GenerateAttributeInfo.MetadataName,
2323
predicate: static (syntaxNode, ct) => syntaxNode is MethodDeclarationSyntax methodSyntax,
2424
transform: static (context, ct) => ParseRegisterMethodModel(context))
2525
.Where(method => method != null);

ServiceScan.SourceGenerator/GenerateAttributeSource.cs renamed to ServiceScan.SourceGenerator/GenerateAttributeInfo.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
namespace ServiceScan.SourceGenerator;
22

3-
internal static class GenerateAttributeSource
3+
internal static class GenerateAttributeInfo
44
{
5-
public static string Source => """
5+
public const string MetadataName = "ServiceScan.SourceGenerator.GenerateServiceRegistrationsAttribute";
6+
7+
public const string Source = """
68
#nullable enable
79
810
using System;

0 commit comments

Comments
 (0)