diff --git a/src/Splat.DependencyInjection.SourceGenerator.Tests/ModuleInitializer.cs b/src/Splat.DependencyInjection.SourceGenerator.Tests/ModuleInitializer.cs index 45f8eb8..5303908 100644 --- a/src/Splat.DependencyInjection.SourceGenerator.Tests/ModuleInitializer.cs +++ b/src/Splat.DependencyInjection.SourceGenerator.Tests/ModuleInitializer.cs @@ -15,8 +15,5 @@ public static class ModuleInitializer /// Initializes the source generators. /// [ModuleInitializer] - public static void Init() - { - VerifySourceGenerators.Enable(); - } + public static void Init() => VerifySourceGenerators.Enable(); } diff --git a/src/Splat.DependencyInjection.SourceGenerator.Tests/RegisterLazySingletonTests.cs b/src/Splat.DependencyInjection.SourceGenerator.Tests/RegisterLazySingletonTests.cs index 86d9935..9bed710 100644 --- a/src/Splat.DependencyInjection.SourceGenerator.Tests/RegisterLazySingletonTests.cs +++ b/src/Splat.DependencyInjection.SourceGenerator.Tests/RegisterLazySingletonTests.cs @@ -32,15 +32,9 @@ public RegisterLazySingletonTests(ITestOutputHelper testOutputHelper) [InlineData(LazyThreadSafetyMode.None, "Test2")] public Task ConstructionAndMultiplePropertyInjectionWithLazyMode(LazyThreadSafetyMode mode, string contract) { - string arguments; - if (string.IsNullOrWhiteSpace(contract)) - { - arguments = $"LazyThreadSafetyMode.{mode}"; - } - else - { - arguments = $"\"{contract}\", LazyThreadSafetyMode.{mode}"; - } + string arguments = string.IsNullOrWhiteSpace(contract) ? + $"LazyThreadSafetyMode.{mode}" : + $"\"{contract}\", LazyThreadSafetyMode.{mode}"; var source = @$" using System; @@ -90,15 +84,9 @@ public interface IServiceProperty3 {{ }} [InlineData("Test2")] public Task LazyParameterRegisteredLazy(string contract) { - string arguments; - if (string.IsNullOrWhiteSpace(contract)) - { - arguments = string.Empty; - } - else - { - arguments = $"\"{contract}\""; - } + string arguments = string.IsNullOrWhiteSpace(contract) ? + string.Empty : + $"\"{contract}\""; var source = @$" using System; diff --git a/src/Splat.DependencyInjection.SourceGenerator.Tests/RegisterTests.LazyParameterConstantNotRegisteredLazyFail_contract=.02.verified.cs b/src/Splat.DependencyInjection.SourceGenerator.Tests/RegisterTests.LazyParameterConstantNotRegisteredLazyFail_contract=.02.verified.cs index 8f63377..afee3b4 100644 --- a/src/Splat.DependencyInjection.SourceGenerator.Tests/RegisterTests.LazyParameterConstantNotRegisteredLazyFail_contract=.02.verified.cs +++ b/src/Splat.DependencyInjection.SourceGenerator.Tests/RegisterTests.LazyParameterConstantNotRegisteredLazyFail_contract=.02.verified.cs @@ -7,7 +7,6 @@ internal static partial class SplatRegistrations { static partial void SetupIOCInternal( Splat.IDependencyResolver resolver) { - Splat.Locator.CurrentMutable.Register(() => new global::Test.Service1(), typeof(global::Test.Service1)); } } } \ No newline at end of file diff --git a/src/Splat.DependencyInjection.SourceGenerator.Tests/RegisterTests.LazyParameterConstantNotRegisteredLazyFail_contract=Test1.02.verified.cs b/src/Splat.DependencyInjection.SourceGenerator.Tests/RegisterTests.LazyParameterConstantNotRegisteredLazyFail_contract=Test1.02.verified.cs index 8f63377..afee3b4 100644 --- a/src/Splat.DependencyInjection.SourceGenerator.Tests/RegisterTests.LazyParameterConstantNotRegisteredLazyFail_contract=Test1.02.verified.cs +++ b/src/Splat.DependencyInjection.SourceGenerator.Tests/RegisterTests.LazyParameterConstantNotRegisteredLazyFail_contract=Test1.02.verified.cs @@ -7,7 +7,6 @@ internal static partial class SplatRegistrations { static partial void SetupIOCInternal( Splat.IDependencyResolver resolver) { - Splat.Locator.CurrentMutable.Register(() => new global::Test.Service1(), typeof(global::Test.Service1)); } } } \ No newline at end of file diff --git a/src/Splat.DependencyInjection.SourceGenerator.Tests/RegisterTests.LazyParameterConstantNotRegisteredLazyFail_contract=Test2.02.verified.cs b/src/Splat.DependencyInjection.SourceGenerator.Tests/RegisterTests.LazyParameterConstantNotRegisteredLazyFail_contract=Test2.02.verified.cs index 8f63377..afee3b4 100644 --- a/src/Splat.DependencyInjection.SourceGenerator.Tests/RegisterTests.LazyParameterConstantNotRegisteredLazyFail_contract=Test2.02.verified.cs +++ b/src/Splat.DependencyInjection.SourceGenerator.Tests/RegisterTests.LazyParameterConstantNotRegisteredLazyFail_contract=Test2.02.verified.cs @@ -7,7 +7,6 @@ internal static partial class SplatRegistrations { static partial void SetupIOCInternal( Splat.IDependencyResolver resolver) { - Splat.Locator.CurrentMutable.Register(() => new global::Test.Service1(), typeof(global::Test.Service1)); } } } \ No newline at end of file diff --git a/src/Splat.DependencyInjection.SourceGenerator.Tests/RegisterTests.cs b/src/Splat.DependencyInjection.SourceGenerator.Tests/RegisterTests.cs index 2c9ce3c..bebd11f 100644 --- a/src/Splat.DependencyInjection.SourceGenerator.Tests/RegisterTests.cs +++ b/src/Splat.DependencyInjection.SourceGenerator.Tests/RegisterTests.cs @@ -2,7 +2,6 @@ // ReactiveUI Association Incorporated licenses this file to you under the MIT license. // See the LICENSE file in the project root for full license information. -using System; using System.Threading.Tasks; using VerifyXunit; @@ -10,8 +9,6 @@ using Xunit; using Xunit.Abstractions; -using static ICSharpCode.Decompiler.IL.Transforms.Stepper; - namespace Splat.DependencyInjection.SourceGenerator.Tests { [UsesVerify] @@ -28,25 +25,13 @@ public RegisterTests(ITestOutputHelper testOutputHelper) [InlineData("Test2")] public Task LazyParameterConstantNotRegisteredLazyFail(string contract) { - string arguments; - if (string.IsNullOrWhiteSpace(contract)) - { - arguments = string.Empty; - } - else - { - arguments = $"\"{contract}\""; - } - - string constantArguments; - if (string.IsNullOrWhiteSpace(contract)) - { - constantArguments = "new Service1()"; - } - else - { - constantArguments = $"new Service1(), \"{contract}\""; - } + string arguments = string.IsNullOrWhiteSpace(contract) ? + string.Empty : + $"\"{contract}\""; + + string constantArguments = string.IsNullOrWhiteSpace(contract) ? + "new Service1()" : + $"new Service1(), \"{contract}\""; var source = @$" using System; @@ -84,15 +69,9 @@ public class Service1 {{ }} [InlineData("Test2")] public Task LazyParameterNotRegisteredLazyFail(string contract) { - string arguments; - if (string.IsNullOrWhiteSpace(contract)) - { - arguments = string.Empty; - } - else - { - arguments = $"\"{contract}\""; - } + string arguments = string.IsNullOrWhiteSpace(contract) ? + string.Empty : + $"\"{contract}\""; var source = @$" using System; diff --git a/src/Splat.DependencyInjection.SourceGenerator.Tests/TestBase.cs b/src/Splat.DependencyInjection.SourceGenerator.Tests/TestBase.cs index 686d367..0be5f21 100644 --- a/src/Splat.DependencyInjection.SourceGenerator.Tests/TestBase.cs +++ b/src/Splat.DependencyInjection.SourceGenerator.Tests/TestBase.cs @@ -18,19 +18,16 @@ protected TestBase(ITestOutputHelper testOutputHelper, string testMethod) { _testMethod = testMethod; - TestHelper = new TestHelper(testOutputHelper); + TestHelper = new(testOutputHelper); } protected TestHelper TestHelper { get; } - public Task InitializeAsync() - { - return TestHelper.InitializeAsync(); - } + public Task InitializeAsync() => TestHelper.InitializeAsync(); public Task DisposeAsync() { - TestHelper?.Dispose(); + TestHelper.Dispose(); return Task.CompletedTask; } @@ -649,7 +646,7 @@ protected virtual void Dispose(bool isDisposing) { if (isDisposing) { - TestHelper?.Dispose(); + TestHelper.Dispose(); } } } diff --git a/src/Splat.DependencyInjection.SourceGenerator.Tests/TestHelper.cs b/src/Splat.DependencyInjection.SourceGenerator.Tests/TestHelper.cs index 162a259..bc416d3 100644 --- a/src/Splat.DependencyInjection.SourceGenerator.Tests/TestHelper.cs +++ b/src/Splat.DependencyInjection.SourceGenerator.Tests/TestHelper.cs @@ -3,10 +3,7 @@ // See the LICENSE file in the project root for full license information. using System; -using System.Collections.Generic; -using System.Linq; using System.Runtime.CompilerServices; -using System.Text; using System.Threading; using System.Threading.Tasks; using Microsoft.CodeAnalysis; @@ -23,24 +20,20 @@ using Xunit; using Xunit.Abstractions; -using Xunit.Sdk; namespace Splat.DependencyInjection.SourceGenerator.Tests { - public class TestHelper : IDisposable + public sealed class TestHelper : IDisposable { #pragma warning disable CS0618 // Type or member is obsolete private static readonly LibraryRange _splatLibrary = new("Splat", VersionRange.AllStableFloating, LibraryDependencyTarget.Package); #pragma warning restore CS0618 // Type or member is obsolete - public TestHelper(ITestOutputHelper testOutput) - { - TestOutputHelper = testOutput ?? throw new ArgumentNullException(nameof(testOutput)); - } + public TestHelper(ITestOutputHelper testOutput) => TestOutputHelper = testOutput ?? throw new ArgumentNullException(nameof(testOutput)); - protected EventBuilderCompiler? EventCompiler { get; private set; } + private EventBuilderCompiler? EventCompiler { get; set; } - protected ITestOutputHelper TestOutputHelper { get; private set; } + private ITestOutputHelper TestOutputHelper { get; } public async Task InitializeAsync() { @@ -67,8 +60,7 @@ public Task TestFail(string source, string contractParameter, [CallerFilePath] s VerifySettings settings = new(); settings.UseParameters(contractParameter); - settings.AutoVerify(); - return Verifier.Verify(driver, settings, sourceFile: file); + return Verifier.Verify(driver, settings, file); } public Task TestPass(string source, string contractParameter, [CallerFilePath] string file = "") @@ -76,7 +68,7 @@ public Task TestPass(string source, string contractParameter, [CallerFilePath] s var driver = Generate(source); VerifySettings settings = new(); settings.UseParameters(contractParameter); - return Verifier.Verify(driver, settings, sourceFile: file); + return Verifier.Verify(driver, settings, file); } public Task TestPass(string source, string contractParameter, LazyThreadSafetyMode mode, [CallerFilePath] string file = "") @@ -85,22 +77,10 @@ public Task TestPass(string source, string contractParameter, LazyThreadSafetyMo VerifySettings settings = new(); settings.UseParameters(contractParameter, mode); - return Verifier.Verify(driver, settings, sourceFile: file); - } - - public void Dispose() - { - Dispose(true); - GC.SuppressFinalize(this); + return Verifier.Verify(driver, settings, file); } - protected virtual void Dispose(bool isDisposing) - { - if (isDisposing) - { - EventCompiler?.Dispose(); - } - } + public void Dispose() => EventCompiler?.Dispose(); private GeneratorDriver Generate(string source) { diff --git a/src/Splat.DependencyInjection.SourceGenerator/DiagnosticWarnings.cs b/src/Splat.DependencyInjection.SourceGenerator/DiagnosticWarnings.cs index 0b1e0dd..16de9d4 100644 --- a/src/Splat.DependencyInjection.SourceGenerator/DiagnosticWarnings.cs +++ b/src/Splat.DependencyInjection.SourceGenerator/DiagnosticWarnings.cs @@ -9,59 +9,59 @@ namespace Splat.DependencyInjection.SourceGenerator internal static class DiagnosticWarnings { internal static readonly DiagnosticDescriptor MultipleConstructorNeedAttribute = new( - id: "SPLATDI001", - title: "Can't find valid constructor", - messageFormat: "{0} has more than one constructor and one hasn't been marked with DependencyInjectionConstructorAttribute", - category: "Compiler", - defaultSeverity: DiagnosticSeverity.Error, - isEnabledByDefault: true); + "SPLATDI001", + "Can't find valid constructor", + "{0} has more than one constructor and one hasn't been marked with DependencyInjectionConstructorAttribute", + "Compiler", + DiagnosticSeverity.Error, + true); internal static readonly DiagnosticDescriptor PropertyMustPublicBeSettable = new( - id: "SPLATDI002", - title: "Property must be public/internal settable", - messageFormat: "{0} property marked with DependencyInjectionPropertyAttribute must have a public or internal setter", - category: "Compiler", - defaultSeverity: DiagnosticSeverity.Error, - isEnabledByDefault: true); + "SPLATDI002", + "Property must be public/internal settable", + "{0} property marked with DependencyInjectionPropertyAttribute must have a public or internal setter", + "Compiler", + DiagnosticSeverity.Error, + true); internal static readonly DiagnosticDescriptor MultipleConstructorsMarked = new( - id: "SPLATDI003", - title: "Multiple constructors have DependencyInjectionConstructorAttribute", - messageFormat: "{0} has multiple constructors marked with the DependencyInjectionConstructorAttribute attribute change so only one is marked", - category: "Compiler", - defaultSeverity: DiagnosticSeverity.Error, - isEnabledByDefault: true); + "SPLATDI003", + "Multiple constructors have DependencyInjectionConstructorAttribute", + "{0} has multiple constructors marked with the DependencyInjectionConstructorAttribute attribute change so only one is marked", + "Compiler", + DiagnosticSeverity.Error, + true); internal static readonly DiagnosticDescriptor ConstructorsMustBePublic = new( - id: "SPLATDI004", - title: "Constructors not public or internal", - messageFormat: "{0} constructor declared with DependencyInjectionConstructorAttribute attribute must be public or internal", - category: "Compiler", - defaultSeverity: DiagnosticSeverity.Error, - isEnabledByDefault: true); + "SPLATDI004", + "Constructors not public or internal", + "{0} constructor declared with DependencyInjectionConstructorAttribute attribute must be public or internal", + "Compiler", + DiagnosticSeverity.Error, + true); internal static readonly DiagnosticDescriptor ConstructorsMustNotHaveCircularDependency = new( - id: "SPLATDI005", - title: "Constructors must not have a circular dependency", - messageFormat: "Constructor parameters must not have a circular dependency to another registered resource", - category: "Compiler", - defaultSeverity: DiagnosticSeverity.Error, - isEnabledByDefault: true); + "SPLATDI005", + "Constructors must not have a circular dependency", + "Constructor parameters must not have a circular dependency to another registered resource", + "Compiler", + DiagnosticSeverity.Error, + true); internal static readonly DiagnosticDescriptor InterfaceRegisteredMultipleTimes = new( - id: "SPLATDI006", - title: "Interface has been registered before", - messageFormat: "{0} has been registered in multiple places", - category: "Compiler", - defaultSeverity: DiagnosticSeverity.Warning, - isEnabledByDefault: true); + "SPLATDI006", + "Interface has been registered before", + "{0} has been registered in multiple places", + "Compiler", + DiagnosticSeverity.Warning, + true); internal static readonly DiagnosticDescriptor LazyParameterNotRegisteredLazy = new( - id: "SPLATDI007", - title: "Constructor has a lazy parameter", - messageFormat: "{0} constructor has a lazy parameter {1} which is not registered with RegisterLazySingleton", - category: "Compiler", - defaultSeverity: DiagnosticSeverity.Error, - isEnabledByDefault: true); + "SPLATDI007", + "Constructor has a lazy parameter", + "{0} constructor has a lazy parameter {1} which is not registered with RegisterLazySingleton", + "Compiler", + DiagnosticSeverity.Error, + true); } } diff --git a/src/Splat.DependencyInjection.SourceGenerator/Metadata/DependencyMetadata.cs b/src/Splat.DependencyInjection.SourceGenerator/Metadata/DependencyMetadata.cs index cba3ecd..6c80317 100644 --- a/src/Splat.DependencyInjection.SourceGenerator/Metadata/DependencyMetadata.cs +++ b/src/Splat.DependencyInjection.SourceGenerator/Metadata/DependencyMetadata.cs @@ -16,7 +16,7 @@ protected DependencyMetadata(ITypeSymbol type) TypeName = type.ToDisplayString(RoslynCommonHelpers.TypeFormat); } - public ITypeSymbol Type { get; init; } + public ITypeSymbol Type { get; } public string TypeName { get; } } diff --git a/src/Splat.DependencyInjection.SourceGenerator/Metadata/MethodMetadata.cs b/src/Splat.DependencyInjection.SourceGenerator/Metadata/MethodMetadata.cs index 5adb148..7cb44b0 100644 --- a/src/Splat.DependencyInjection.SourceGenerator/Metadata/MethodMetadata.cs +++ b/src/Splat.DependencyInjection.SourceGenerator/Metadata/MethodMetadata.cs @@ -13,7 +13,7 @@ namespace Splat.DependencyInjection.SourceGenerator.Metadata { internal abstract record MethodMetadata { - public MethodMetadata(IMethodSymbol method, ITypeSymbol interfaceType, ITypeSymbol concreteType, InvocationExpressionSyntax methodInvocation, bool isLazy, IReadOnlyList constructorDependencies, IReadOnlyList properties, IReadOnlyList registerParameterValues) + protected MethodMetadata(IMethodSymbol method, ITypeSymbol interfaceType, ITypeSymbol concreteType, InvocationExpressionSyntax methodInvocation, bool isLazy, IReadOnlyList constructorDependencies, IReadOnlyList properties, IReadOnlyList registerParameterValues) { Method = method; MethodInvocation = methodInvocation; @@ -27,17 +27,17 @@ public MethodMetadata(IMethodSymbol method, ITypeSymbol interfaceType, ITypeSymb RegisterParameterValues = registerParameterValues; } - public IMethodSymbol Method { get; init; } + public IMethodSymbol Method { get; } - public InvocationExpressionSyntax MethodInvocation { get; init; } + public InvocationExpressionSyntax MethodInvocation { get; } - public bool IsLazy { get; init; } + public bool IsLazy { get; } - public IReadOnlyList ConstructorDependencies { get; init; } + public IReadOnlyList ConstructorDependencies { get; } - public IReadOnlyList Properties { get; init; } + public IReadOnlyList Properties { get; } - public IReadOnlyList RegisterParameterValues { get; init; } + public IReadOnlyList RegisterParameterValues { get; } public ITypeSymbol ConcreteType { get; } diff --git a/src/Splat.DependencyInjection.SourceGenerator/Metadata/PropertyDependencyMetadata.cs b/src/Splat.DependencyInjection.SourceGenerator/Metadata/PropertyDependencyMetadata.cs index 3d45553..5e63dd3 100644 --- a/src/Splat.DependencyInjection.SourceGenerator/Metadata/PropertyDependencyMetadata.cs +++ b/src/Splat.DependencyInjection.SourceGenerator/Metadata/PropertyDependencyMetadata.cs @@ -16,7 +16,7 @@ public PropertyDependencyMetadata(IPropertySymbol property) Name = Property.Name; } - public IPropertySymbol Property { get; init; } + public IPropertySymbol Property { get; } public string Name { get; } } diff --git a/src/Splat.DependencyInjection.SourceGenerator/Metadata/RegisterConstantMetadata.cs b/src/Splat.DependencyInjection.SourceGenerator/Metadata/RegisterConstantMetadata.cs index 6f02700..4c88f3f 100644 --- a/src/Splat.DependencyInjection.SourceGenerator/Metadata/RegisterConstantMetadata.cs +++ b/src/Splat.DependencyInjection.SourceGenerator/Metadata/RegisterConstantMetadata.cs @@ -3,9 +3,6 @@ // See the LICENSE file in the project root for full license information. using System; -using System.Collections.Generic; -using System.Text; - using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; diff --git a/src/Splat.DependencyInjection.SourceGenerator/MetadataExtractor.cs b/src/Splat.DependencyInjection.SourceGenerator/MetadataExtractor.cs index c6024d8..afa2612 100644 --- a/src/Splat.DependencyInjection.SourceGenerator/MetadataExtractor.cs +++ b/src/Splat.DependencyInjection.SourceGenerator/MetadataExtractor.cs @@ -7,6 +7,7 @@ using System.Linq; using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using ReactiveMarbles.RoslynHelpers; @@ -62,13 +63,13 @@ public static IEnumerable GetValidMethods(GeneratorExecutionCont try { var semanticModel = compilation.GetSemanticModel(invocationExpression.SyntaxTree); - if (semanticModel.GetSymbolInfo(invocationExpression).Symbol is not IMethodSymbol methodSymbol) + if (ModelExtensions.GetSymbolInfo(semanticModel, invocationExpression).Symbol is not IMethodSymbol methodSymbol) { // Produce a diagnostic error. return null; } - if (methodSymbol.Parameters.Length == 0) + if (methodSymbol.Parameters.Length is 0 or > 2) { return null; } @@ -95,7 +96,7 @@ public static IEnumerable GetValidMethods(GeneratorExecutionCont try { var semanticModel = compilation.GetSemanticModel(invocationExpression.SyntaxTree); - if (semanticModel.GetSymbolInfo(invocationExpression).Symbol is not IMethodSymbol methodSymbol) + if (ModelExtensions.GetSymbolInfo(semanticModel, invocationExpression).Symbol is not IMethodSymbol methodSymbol) { // Produce a diagnostic error. return null; @@ -162,7 +163,7 @@ private static IEnumerable GetRegisterParameters(IMethodSymbo } else { - var mode = semanticModel.GetSymbolInfo(expression); + var mode = ModelExtensions.GetSymbolInfo(semanticModel, expression); if (mode.Symbol is not null) { @@ -172,7 +173,7 @@ private static IEnumerable GetRegisterParameters(IMethodSymbo } } - private static IEnumerable GetConstructorDependencies(ITypeSymbol concreteTarget, InvocationExpressionSyntax invocationExpression) + private static IEnumerable GetConstructorDependencies(INamespaceOrTypeSymbol concreteTarget, CSharpSyntaxNode invocationExpression) { var constructors = concreteTarget .GetMembers() diff --git a/src/Splat.DependencyInjection.SourceGenerator/SourceGeneratorHelpers.cs b/src/Splat.DependencyInjection.SourceGenerator/SourceGeneratorHelpers.cs index 9809163..7d17bf2 100644 --- a/src/Splat.DependencyInjection.SourceGenerator/SourceGeneratorHelpers.cs +++ b/src/Splat.DependencyInjection.SourceGenerator/SourceGeneratorHelpers.cs @@ -50,19 +50,15 @@ public static string Generate(GeneratorExecutionContext context, Compilation com {compilationUnit.ToFullString()}"; } - private static IEnumerable Generate(IEnumerable methodMetadatas) + private static IEnumerable Generate(IEnumerable methodMetadataEnumerable) { - foreach (var methodMetadata in methodMetadatas) + foreach (var methodMetadata in methodMetadataEnumerable) { - var typeConstructorArguments = new List(); - - foreach (var parameter in methodMetadata.ConstructorDependencies) - { - var parameterType = parameter.Type; - var parameterTypeName = parameterType.ToDisplayString(RoslynCommonHelpers.TypeFormat); - - typeConstructorArguments.Add(Argument(GetSplatService(parameterTypeName))); - } + var typeConstructorArguments = methodMetadata.ConstructorDependencies + .Select(parameter => parameter.Type) + .Select(parameterType => parameterType.ToDisplayString(RoslynCommonHelpers.TypeFormat)) + .Select(parameterTypeName => Argument(GetSplatService(parameterTypeName))) + .ToList(); var contractParameter = methodMetadata.RegisterParameterValues.FirstOrDefault(x => x.ParameterName == "contract"); @@ -78,24 +74,27 @@ private static IEnumerable Generate(IEnumerable ObjectCreationExpression(methodMetadata.ConcreteTypeName, typeConstructorArguments) : ObjectCreationExpression(methodMetadata.ConcreteTypeName, typeConstructorArguments, initializer); - if (methodMetadata.IsLazy) + switch (methodMetadata) { - yield return GetLazyBlock(methodMetadata, call, contract); - } - else - { - yield return GenerateLocatorSetService(Argument(ParenthesizedLambdaExpression(call)), methodMetadata.InterfaceTypeName, contract); + case RegisterLazySingletonMetadata lazyMetadata: + yield return GetLazyBlock(lazyMetadata, call, contract); + break; + case RegisterMetadata registerMetadata: + yield return GenerateLocatorSetService(Argument(ParenthesizedLambdaExpression(call)), registerMetadata.InterfaceTypeName, contract); + break; } } } private static InitializerExpressionSyntax? GetPropertyInitializer(IEnumerable properties) { - var propertySet = new List(); - foreach (var property in properties) - { - propertySet.Add(AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, property.Name, GetSplatService(property.TypeName))); - } + var propertySet = properties + .Select(property => + AssignmentExpression( + SyntaxKind.SimpleAssignmentExpression, + property.Name, + GetSplatService(property.TypeName))) + .ToList(); return propertySet.Count > 0 ? InitializerExpression(SyntaxKind.ObjectInitializerExpression, propertySet) : null; } diff --git a/src/Splat.DependencyInjection.SourceGenerator/SyntaxReceiver.cs b/src/Splat.DependencyInjection.SourceGenerator/SyntaxReceiver.cs index 3529c73..e417cde 100644 --- a/src/Splat.DependencyInjection.SourceGenerator/SyntaxReceiver.cs +++ b/src/Splat.DependencyInjection.SourceGenerator/SyntaxReceiver.cs @@ -24,14 +24,14 @@ public void OnVisitSyntaxNode(SyntaxNode syntaxNode) return; } - if (invocationExpression.Expression is MemberAccessExpressionSyntax memberAccess) + switch (invocationExpression.Expression) { - HandleSimpleName(memberAccess.Name, invocationExpression); - } - - if (invocationExpression.Expression is MemberBindingExpressionSyntax bindingAccess) - { - HandleSimpleName(bindingAccess.Name, invocationExpression); + case MemberAccessExpressionSyntax memberAccess: + HandleSimpleName(memberAccess.Name, invocationExpression); + break; + case MemberBindingExpressionSyntax bindingAccess: + HandleSimpleName(bindingAccess.Name, invocationExpression); + break; } }