Skip to content

Commit

Permalink
feat: inline methods consisting of a single local variable declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus Hartmair authored and hartmair committed Jul 28, 2024
1 parent 93a9732 commit 14977e2
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,25 @@ public static class InlineExpressionMappingBuilder

var bodyExpression = methodSyntax switch
{
// => expression
MethodDeclarationSyntax { ExpressionBody: { } body, ParameterList.Parameters: [var sourceParameter1] } => body.Expression,

// { return expression; }
MethodDeclarationSyntax { Body.Statements: [ReturnStatementSyntax singleStatement] } => singleStatement.Expression,

// { var dest = expression; return dest; }
MethodDeclarationSyntax
{
Body.Statements: [
LocalDeclarationStatementSyntax
{
Declaration.Variables: [VariableDeclaratorSyntax { Initializer: { } variableInitializer } variableDeclarator]
},
ReturnStatementSyntax { Expression: IdentifierNameSyntax identifierName }
]
} when identifierName.Identifier.Value == variableDeclarator.Identifier.Value
=> variableInitializer.Value,

_ => null
};
if (bodyExpression == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private D MapToD(C v)
}

[Fact]
public Task ClassToClassNonInlinedMethod()
public Task ClassToClassInlinedSingleDeclaration()
{
var source = TestSourceBuilder.MapperWithBodyAndTypes(
"""
Expand All @@ -63,6 +63,29 @@ private D MapToD(C v)
return TestHelper.VerifyGenerator(source);
}

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

private D MapToD(C v)
{
var dest = new D();
dest.Value = v.Value + "-mapped";
return dest;
}
""",
"class A { public string StringValue { get; set; } public C NestedValue { get; set; } }",
"class B { public string StringValue { get; set; } public D NestedValue { get; set; } }",
"class C { public string Value { get; set; } }",
"class D { public string Value { get; set; } }"
);

return TestHelper.VerifyGenerator(source);
}

[Fact]
public Task ClassToClassUserImplementedOrdering()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//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 System.Linq.Queryable.Select(source, x => new global::B()
{
StringValue = x.StringValue,
NestedValue = new global::D { Value = x.NestedValue.Value + "-mapped" },
});
#nullable enable
}
}

0 comments on commit 14977e2

Please sign in to comment.