From c291aea3f6f3a22e63d169b15b136edd5d0724e2 Mon Sep 17 00:00:00 2001 From: Jan Trejbal Date: Mon, 20 Nov 2023 15:59:52 +0100 Subject: [PATCH] fix: extension method parameters --- .../Syntax/SyntaxFactoryHelper.Invocation.cs | 2 +- .../Mapping/ExtensionMethodTest.cs | 29 +++++++++++++++++++ ...onMapMethodShouldWork#Mapper.g.verified.cs | 12 ++++++++ ...pdateMethodShouldWork#Mapper.g.verified.cs | 10 +++++++ 4 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 test/Riok.Mapperly.Tests/Mapping/ExtensionMethodTest.cs create mode 100644 test/Riok.Mapperly.Tests/_snapshots/ExtensionMethodTest.ExtensionMapMethodShouldWork#Mapper.g.verified.cs create mode 100644 test/Riok.Mapperly.Tests/_snapshots/ExtensionMethodTest.ExtensionUpdateMethodShouldWork#Mapper.g.verified.cs diff --git a/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.Invocation.cs b/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.Invocation.cs index 63fab7d578..7f14269a7d 100644 --- a/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.Invocation.cs +++ b/src/Riok.Mapperly/Emit/Syntax/SyntaxFactoryHelper.Invocation.cs @@ -84,7 +84,7 @@ public static ParameterListSyntax ParameterList(bool extensionMethod, params Met .WhereNotNull() .DistinctBy(x => x.Ordinal) .OrderBy(x => x.Ordinal) - .Select(p => Parameter(extensionMethod, p)); + .Select((p, i) => Parameter(extensionMethod && i == 0, p)); return SyntaxFactory.ParameterList(CommaSeparatedList(parameterSyntaxes)); } diff --git a/test/Riok.Mapperly.Tests/Mapping/ExtensionMethodTest.cs b/test/Riok.Mapperly.Tests/Mapping/ExtensionMethodTest.cs new file mode 100644 index 0000000000..6c97057d91 --- /dev/null +++ b/test/Riok.Mapperly.Tests/Mapping/ExtensionMethodTest.cs @@ -0,0 +1,29 @@ +namespace Riok.Mapperly.Tests.Mapping; + +[UsesVerify] +public class ExtensionMethodTest +{ + [Fact] + public Task ExtensionMapMethodShouldWork() + { + var source = TestSourceBuilder.MapperWithBodyAndTypes( + "static partial B MapToB(this A source);", + "class A { public int Value { get; set; } }", + "class B { public int Value { get; set; } }" + ); + + return TestHelper.VerifyGenerator(source); + } + + [Fact] + public Task ExtensionUpdateMethodShouldWork() + { + var source = TestSourceBuilder.MapperWithBodyAndTypes( + "static partial void MapToB(this A source, B target);", + "class A { public int Value { get; set; } }", + "class B { public int Value { get; set; } }" + ); + + return TestHelper.VerifyGenerator(source); + } +} diff --git a/test/Riok.Mapperly.Tests/_snapshots/ExtensionMethodTest.ExtensionMapMethodShouldWork#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/ExtensionMethodTest.ExtensionMapMethodShouldWork#Mapper.g.verified.cs new file mode 100644 index 0000000000..3c4b21be11 --- /dev/null +++ b/test/Riok.Mapperly.Tests/_snapshots/ExtensionMethodTest.ExtensionMapMethodShouldWork#Mapper.g.verified.cs @@ -0,0 +1,12 @@ +//HintName: Mapper.g.cs +// +#nullable enable +public partial class Mapper +{ + static partial global::B MapToB(this global::A source) + { + var target = new global::B(); + target.Value = source.Value; + return target; + } +} diff --git a/test/Riok.Mapperly.Tests/_snapshots/ExtensionMethodTest.ExtensionUpdateMethodShouldWork#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/ExtensionMethodTest.ExtensionUpdateMethodShouldWork#Mapper.g.verified.cs new file mode 100644 index 0000000000..4270d7a1a4 --- /dev/null +++ b/test/Riok.Mapperly.Tests/_snapshots/ExtensionMethodTest.ExtensionUpdateMethodShouldWork#Mapper.g.verified.cs @@ -0,0 +1,10 @@ +//HintName: Mapper.g.cs +// +#nullable enable +public partial class Mapper +{ + static partial void MapToB(this global::A source, global::B target) + { + target.Value = source.Value; + } +}