From 747871656a906ba2032087bf460377b497fcb50b Mon Sep 17 00:00:00 2001 From: Oleksandr Liakhevych Date: Sat, 29 Jun 2024 10:39:58 +0300 Subject: [PATCH] Generate matching IServiceProvider parameter name --- .../GeneratedMethodTests.cs | 40 +++++++++++++++++++ .../DependencyInjectionGenerator.cs | 4 +- .../Model/MethodModel.cs | 4 ++ 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/ServiceScan.SourceGenerator.Tests/GeneratedMethodTests.cs b/ServiceScan.SourceGenerator.Tests/GeneratedMethodTests.cs index 89c8c91..210c6ac 100644 --- a/ServiceScan.SourceGenerator.Tests/GeneratedMethodTests.cs +++ b/ServiceScan.SourceGenerator.Tests/GeneratedMethodTests.cs @@ -219,6 +219,46 @@ public static partial IServiceCollection AddServices(this IServiceCollection ser Assert.Equal(expected, results.GeneratedTrees[1].ToString()); } + [Fact] + public void MethodWithCustomParameterName() + { + var compilation = CreateCompilation(Services, + """ + using ServiceScan.SourceGenerator; + using Microsoft.Extensions.DependencyInjection; + + namespace GeneratorTests; + + public static partial class ServicesExtensions + { + [GenerateServiceRegistrations(AssignableTo = typeof(IService))] + public static partial IServiceCollection AddServices(this IServiceCollection strangeServices); + } + """); + + var results = CSharpGeneratorDriver + .Create(_generator) + .RunGenerators(compilation) + .GetRunResult(); + + var expected = """ + using Microsoft.Extensions.DependencyInjection; + + namespace GeneratorTests; + + public static partial class ServicesExtensions + { + public static partial IServiceCollection AddServices(this IServiceCollection strangeServices) + { + return strangeServices + .AddTransient(); + } + } + """; + + Assert.Equal(expected, results.GeneratedTrees[1].ToString()); + } + private static Compilation CreateCompilation(params string[] source) { var path = Path.GetDirectoryName(typeof(object).Assembly.Location)!; diff --git a/ServiceScan.SourceGenerator/DependencyInjectionGenerator.cs b/ServiceScan.SourceGenerator/DependencyInjectionGenerator.cs index bf67db5..1d5c265 100644 --- a/ServiceScan.SourceGenerator/DependencyInjectionGenerator.cs +++ b/ServiceScan.SourceGenerator/DependencyInjectionGenerator.cs @@ -68,9 +68,9 @@ public void Initialize(IncrementalGeneratorInitializationContext context) {{method.TypeModifiers}} class {{method.TypeName}} { - {{method.MethodModifiers}} {{returnType}} {{method.MethodName}}({{(method.IsExtensionMethod ? "this" : "")}} IServiceCollection services) + {{method.MethodModifiers}} {{returnType}} {{method.MethodName}}({{(method.IsExtensionMethod ? "this" : "")}} IServiceCollection {{method.ParameterName}}) { - {{(method.ReturnsVoid ? "" : "return ")}}services + {{(method.ReturnsVoid ? "" : "return ")}}{{method.ParameterName}} {{sb.ToString().Trim()}}; } } diff --git a/ServiceScan.SourceGenerator/Model/MethodModel.cs b/ServiceScan.SourceGenerator/Model/MethodModel.cs index 2c411ec..9c74501 100644 --- a/ServiceScan.SourceGenerator/Model/MethodModel.cs +++ b/ServiceScan.SourceGenerator/Model/MethodModel.cs @@ -10,11 +10,14 @@ record MethodModel( string TypeModifiers, string MethodName, string MethodModifiers, + string ParameterName, bool IsExtensionMethod, bool ReturnsVoid) { public static MethodModel Create(IMethodSymbol method, SyntaxNode syntax) { + var parameterName = method.Parameters[0].Name; + return new MethodModel( Namespace: method.ContainingNamespace.IsGlobalNamespace ? null : method.ContainingNamespace.ToDisplayString(), TypeName: method.ContainingType.Name, @@ -22,6 +25,7 @@ public static MethodModel Create(IMethodSymbol method, SyntaxNode syntax) TypeModifiers: GetModifiers(GetTypeSyntax(syntax)), MethodName: method.Name, MethodModifiers: GetModifiers(syntax), + ParameterName: parameterName, IsExtensionMethod: method.IsExtensionMethod, ReturnsVoid: method.ReturnsVoid); }