Skip to content

Commit

Permalink
nested types can be annoying, so lets fix that (#1343)
Browse files Browse the repository at this point in the history
  • Loading branch information
david-driscoll authored Nov 28, 2024
1 parent 1b44247 commit 204d4e9
Show file tree
Hide file tree
Showing 82 changed files with 1,118 additions and 297 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ static bool TypeInfoFilterFunc(TypeInfoFilter typeFilter, INamedTypeSymbol type)
TypeInfoFilter.Sealed => type.IsSealed,
TypeInfoFilter.Visible => type.DeclaredAccessibility == Accessibility.Public,
TypeInfoFilter.ValueType => type.IsValueType,
TypeInfoFilter.Static => type.IsStatic,
// TypeInfoFilter.Nested => type.ContainingType is {},
TypeInfoFilter.Unknown => throw new NotSupportedException(typeFilter.ToString()),
_ => throw new NotSupportedException(typeFilter.ToString()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,9 @@ internal enum TypeInfoFilter
// /// The type is a generic type definition.
// /// </summary>
// GenericTypeDefinition = 7,

/// <summary>
/// The type is static.
/// </summary>
Static = -2,
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ internal static ImmutableArray<Item> GetTypeDetails(
List<ITypeFilterDescriptor> typeFilters =
[
new TypeKindFilterDescriptor(true, ImmutableHashSet.Create(TypeKind.Class)),
new TypeInfoFilterDescriptor(false, ImmutableHashSet.Create(TypeInfoFilter.Abstract)),
new TypeInfoFilterDescriptor(false, ImmutableHashSet.Create(TypeInfoFilter.Abstract, TypeInfoFilter.Static)),
];
List<IServiceTypeDescriptor> serviceDescriptors = new();
var classFilter = ClassFilter.All;
Expand Down
43 changes: 3 additions & 40 deletions src/DependencyInjection.Analyzers/StatementGeneration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ string lifetime
IdentifierName("a"),
GenericName("GetRequiredService")
.WithTypeArgumentList(
TypeArgumentList(SingletonSeparatedList<TypeSyntax>(IdentifierName(Helpers.GetGenericDisplayName(implementationType))))
TypeArgumentList(
SingletonSeparatedList<TypeSyntax>(IdentifierName(Helpers.GetGenericDisplayName(implementationType).Replace("+", ".")))
)
)
)
)
Expand Down Expand Up @@ -101,61 +103,22 @@ string lifetime
)
{
var isServiceTypeAccessible = compilation.IsSymbolAccessibleWithin(serviceType, compilation.Assembly);

/* Unmerged change from project 'Rocket.Surgery.DependencyInjection.Analyzers.roslyn4.8'
Before:
return ( isServiceTypeAccessible, serviceTypeExpression, implementationTypeExpression ) switch
{
(true, TypeOfExpressionSyntax { Type: { } serviceTypeSyntax }, TypeOfExpressionSyntax { Type: { } implementationTypeSyntax })
After:
return ( isServiceTypeAccessible, serviceTypeExpression, implementationTypeExpression) switch
{
(true, TypeOfExpressionSyntax { Type: { } serviceTypeSyntax }, TypeOfExpressionSyntax { Type: { } implementationTypeSyntax })
*/
return ( isServiceTypeAccessible, serviceTypeExpression, implementationTypeExpression ) switch
{
(true, TypeOfExpressionSyntax { Type: { } serviceTypeSyntax }, TypeOfExpressionSyntax { Type: { } implementationTypeSyntax })
when !IsOpenGenericType(serviceType)
&& !IsOpenGenericType(implementationType)
&& compilation.IsSymbolAccessibleWithin(implementationType, compilation.Assembly)
=> InvocationExpression(
/* Unmerged change from project 'Rocket.Surgery.DependencyInjection.Analyzers.roslyn4.8'
Before:
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
After:
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
*/
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName("ServiceDescriptor"),
GenericName(lifetime)
.WithTypeArgumentList(TypeArgumentList(SeparatedList([serviceTypeSyntax, implementationTypeSyntax])))
/* Unmerged change from project 'Rocket.Surgery.DependencyInjection.Analyzers.roslyn4.8'
Before:
)
),
(true, TypeOfExpressionSyntax { Type: { } serviceTypeSyntax }, SimpleLambdaExpressionSyntax { ExpressionBody: {} })
After:
)
),
(true, TypeOfExpressionSyntax { Type: { } serviceTypeSyntax }, SimpleLambdaExpressionSyntax { ExpressionBody: {} })
*/
)
),
(true, TypeOfExpressionSyntax { Type: { } serviceTypeSyntax }, SimpleLambdaExpressionSyntax { ExpressionBody: { } })
when !IsOpenGenericType(serviceType) =>
/* Unmerged change from project 'Rocket.Surgery.DependencyInjection.Analyzers.roslyn4.8'
Before:
InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
After:
InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
*/
InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,9 @@ public enum TypeInfoFilter
// /// The type is a generic type definition.
// /// </summary>
// GenericTypeDefinition = 7,
}

/// <summary>
/// The type is static.
/// </summary>
Static = -2,
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public static class CompiledTypeProviderServiceCollectionExtensions
/// <returns></returns>
public static IServiceCollection AddCompiledServiceRegistrations(this IServiceCollection services, ICompiledTypeProvider provider)
{
// This is implied to ignore abstract and static classes.
return provider.Scan(
services,
z => z
Expand Down
24 changes: 24 additions & 0 deletions test/DependencyInjection.Analyzers.Tests/GetTypesTestsData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,30 @@ public static IEnumerable<Func<GetTypesItem>> GetTypesData()
.NotStartsWith("Polyfill")
)
);
yield return TestMethod(
z => z
.FromAssemblies()
.NotFromAssemblyOf<ServiceRegistrationAttribute>()
.GetTypes(
x => x
.NotAssignableTo<ICompiledTypeProvider>()
.InfoOf(TypeInfoFilter.Static)
.NotInNamespaces("JetBrains.Annotations", "Polyfills", "System")
.NotStartsWith("Polyfill")
)
);
yield return TestMethod(
z => z
.FromAssemblies()
.NotFromAssemblyOf<ServiceRegistrationAttribute>()
.GetTypes(
x => x
.NotAssignableTo<ICompiledTypeProvider>()
.NotInfoOf(TypeInfoFilter.Static)
.NotInNamespaces("JetBrains.Annotations", "Polyfills", "System")
.NotStartsWith("Polyfill")
)
);
yield return TestMethod(
z => z
.FromAssemblies()
Expand Down
Loading

0 comments on commit 204d4e9

Please sign in to comment.