Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Riok.Mapperly/Descriptors/InlineExpressionRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,18 @@ public override SyntaxNode VisitTypeOfExpression(TypeOfExpressionSyntax node)
return node;
}

public override SyntaxNode? VisitParenthesizedLambdaExpression(ParenthesizedLambdaExpressionSyntax node)
{
// the lambda symbol keeps the nullable annotation of an explicit return type,
// the type info of the return type syntax does not.
var returnType = node.ReturnType == null ? null : (semanticModel.GetSymbolInfo(node).Symbol as IMethodSymbol)?.ReturnType;
node = (ParenthesizedLambdaExpressionSyntax)base.VisitParenthesizedLambdaExpression(node)!;
if (returnType == null || node.ReturnType == null)
return node;

return node.WithReturnType(FullyQualifiedIdentifier(returnType).WithTriviaFrom(node.ReturnType));
Comment thread
latonz marked this conversation as resolved.
Resolved
}

public override SyntaxNode? VisitInvocationExpression(InvocationExpressionSyntax node)
{
// expand static method invocation type names
Expand Down
38 changes: 38 additions & 0 deletions test/Riok.Mapperly.Tests/Helpers/InlineExpressionRewriterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,44 @@ record A;
result.ShouldBe("global::System.Array.Empty<global::AnotherAssembly.A>()");
}

[Theory]
[InlineData("DateTime? (x) => x.DateCreated", "global::System.DateTime? (x) => x.DateCreated")]
[InlineData("B (x) => x.Value", "global::AnotherAssembly.B (x) => x.Value")]
[InlineData("B? (x) => x.Value", "global::AnotherAssembly.B? (x) => x.Value")]
[InlineData("List<B> (x) => x.Values", "global::System.Collections.Generic.List<global::AnotherAssembly.B> (x) => x.Values")]
[InlineData("B[] (x) => x.Values.ToArray()", "global::AnotherAssembly.B[] (x) => x.Values.ToArray()")]
[InlineData(
"List<B?> (x) => x.NullableValues",
"global::System.Collections.Generic.List<global::AnotherAssembly.B?> (x) => x.NullableValues"
)]
[InlineData("object (x) => (B)x.Value", "object (x) => (global::AnotherAssembly.B)x.Value")]
[InlineData("(x) => x.DateCreated", "(x) => x.DateCreated")]
public void RewriteExpressionContainingLambdaWithExplicitReturnType(string lambda, string inlinedLambda)
{
var (result, inlineOk) = Rewrite(
$$"""
#nullable enable
using AnotherAssembly;
using System;
using System.Collections.Generic;
using System.Linq;

public class Test
{
public int MapExpression(List<A> values) => values.Select({{lambda}}).Count();
}

namespace AnotherAssembly {
record A(DateTime DateCreated, B Value, List<B> Values, List<B?> NullableValues);
record B;
}
"""
);

inlineOk.ShouldBeTrue();
result.ShouldBe($"global::System.Linq.Enumerable.Count(global::System.Linq.Enumerable.Select(values, {inlinedLambda}))");
}

private (string Result, bool CanBeInlined) Rewrite([StringSyntax(StringSyntax.CSharp)] string source)
{
var compilation = TestHelper.BuildCompilation(source);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,24 @@ private List<C> Order(List<C> v)
return TestHelper.VerifyGenerator(source);
}

[Fact]
public Task ClassToClassUserImplementedLambdaWithExplicitReturnType()
{
var source = TestSourceBuilder.MapperWithBodyAndTypes(
"""
private partial System.Linq.IQueryable<B> Map(System.Linq.IQueryable<A> source);

private DateTime? LastCreated(List<C> v)
=> v.Select(DateTime? (x) => x.DateCreated).Max();
""",
"class A { public string StringValue { get; set; } public List<C> Created { get; set; } }",
"class B { public string StringValue { get; set; } public DateTime? Created { get; set; } }",
"class C { public DateTime DateCreated { get; set; } }"
);

return TestHelper.VerifyGenerator(source);
}

[Fact]
public Task ClassToClassUserImplementedOrderingWithMappingAndParameterHiding()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//HintName: Mapper.g.cs
// <auto-generated />
#nullable enable
public partial class Mapper
{
[global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "0.0.1.0")]
private partial global::System.Linq.IQueryable<global::B> Map(global::System.Linq.IQueryable<global::A> source)
{
#nullable disable
return global::System.Linq.Queryable.Select(
source,
x => new global::B()
{
StringValue = x.StringValue,
Created = global::System.Linq.Enumerable.Max(global::System.Linq.Enumerable.Select(x.Created, global::System.DateTime? (x1) => x1.DateCreated)),
}
);
#nullable enable
}
}
Loading