Skip to content

Commit

Permalink
fix: prevent generation of empty if not null statements (#576)
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyMakkison authored Jul 19, 2023
1 parent b238481 commit 8cbeff1
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,15 @@ public override IEnumerable<StatementSyntax> Build(TypeMappingBuildContext ctx,
if (!SourceType.IsNullable() && !TargetType.IsNullable())
return body;

var enumerated = body.ToArray();

// if body is empty don't generate an if statement
if (!enumerated.Any())
{
return Enumerable.Empty<StatementSyntax>();
}

// if (source != null && target != null) { body }
return new[] { IfStatement(IfNoneNull((SourceType, ctx.Source), (TargetType, target)), Block(body)), };
;
return new[] { IfStatement(IfNoneNull((SourceType, ctx.Source), (TargetType, target)), Block(enumerated)), };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,6 @@ public partial int ParseableInt(string value)
target.NestedNullableTargetNotNullable = MapToTestObjectNested(dto.NestedNullableTargetNotNullable);
target.StringNullableTargetNotNullable = dto.StringNullableTargetNotNullable;
target.SourceTargetSameObjectType = dto.SourceTargetSameObjectType;
if (dto.SpanValue != null)
{
}

target.MemoryValue = MapToStringArray(dto.MemoryValue.Span);
target.StackValue = new global::System.Collections.Generic.Stack<string>(global::System.Linq.Enumerable.Select(dto.StackValue, x => x.ToString()));
target.QueueValue = new global::System.Collections.Generic.Queue<string>(global::System.Linq.Enumerable.Select(dto.QueueValue, x => x.ToString()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,6 @@ public static partial int ParseableInt(string value)
target.NestedNullableTargetNotNullable = MapToTestObjectNested(dto.NestedNullableTargetNotNullable);
target.StringNullableTargetNotNullable = dto.StringNullableTargetNotNullable;
target.SourceTargetSameObjectType = dto.SourceTargetSameObjectType;
if (dto.SpanValue != null)
{
}

target.MemoryValue = MapToStringArray(dto.MemoryValue.Span);
target.StackValue = new global::System.Collections.Generic.Stack<string>(global::System.Linq.Enumerable.Select(dto.StackValue, x => x.ToString()));
target.QueueValue = new global::System.Collections.Generic.Queue<string>(global::System.Linq.Enumerable.Select(dto.QueueValue, x => x.ToString()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,6 @@ public partial int ParseableInt(string value)
target.NestedNullableTargetNotNullable = MapToTestObjectNested(dto.NestedNullableTargetNotNullable);
target.StringNullableTargetNotNullable = dto.StringNullableTargetNotNullable;
target.SourceTargetSameObjectType = dto.SourceTargetSameObjectType;
if (dto.SpanValue != null)
{
}

target.MemoryValue = MapToStringArray(dto.MemoryValue.Span);
target.StackValue = new global::System.Collections.Generic.Stack<string>(global::System.Linq.Enumerable.Select(dto.StackValue, x => x.ToString()));
target.QueueValue = new global::System.Collections.Generic.Queue<string>(global::System.Linq.Enumerable.Select(dto.QueueValue, x => x.ToString()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,6 @@ public static partial int ParseableInt(string value)
target.NestedNullableTargetNotNullable = MapToTestObjectNested(dto.NestedNullableTargetNotNullable);
target.StringNullableTargetNotNullable = dto.StringNullableTargetNotNullable;
target.SourceTargetSameObjectType = dto.SourceTargetSameObjectType;
if (dto.SpanValue != null)
{
}

target.MemoryValue = MapToStringArray(dto.MemoryValue.Span);
target.StackValue = new global::System.Collections.Generic.Stack<string>(global::System.Linq.Enumerable.Select(dto.StackValue, x => x.ToString()));
target.QueueValue = new global::System.Collections.Generic.Queue<string>(global::System.Linq.Enumerable.Select(dto.QueueValue, x => x.ToString()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,6 @@ public partial int ParseableInt(string value)
target.NestedNullableTargetNotNullable = MapToTestObjectNested(dto.NestedNullableTargetNotNullable);
target.StringNullableTargetNotNullable = dto.StringNullableTargetNotNullable;
target.SourceTargetSameObjectType = dto.SourceTargetSameObjectType;
if (dto.SpanValue != null)
{
}

target.MemoryValue = MapToStringArray(dto.MemoryValue.Span);
target.StackValue = new global::System.Collections.Generic.Stack<string>(global::System.Linq.Enumerable.Select(dto.StackValue, x => x.ToString()));
target.QueueValue = new global::System.Collections.Generic.Queue<string>(global::System.Linq.Enumerable.Select(dto.QueueValue, x => x.ToString()));
Expand Down Expand Up @@ -444,4 +440,4 @@ private string MapToString1(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumD
return target;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -275,10 +275,6 @@ public static partial int ParseableInt(string value)
target.NestedNullableTargetNotNullable = MapToTestObjectNested(dto.NestedNullableTargetNotNullable);
target.StringNullableTargetNotNullable = dto.StringNullableTargetNotNullable;
target.SourceTargetSameObjectType = dto.SourceTargetSameObjectType;
if (dto.SpanValue != null)
{
}

target.MemoryValue = MapToStringArray(dto.MemoryValue.Span);
target.StackValue = new global::System.Collections.Generic.Stack<string>(global::System.Linq.Enumerable.Select(dto.StackValue, x => x.ToString()));
target.QueueValue = new global::System.Collections.Generic.Queue<string>(global::System.Linq.Enumerable.Select(dto.QueueValue, x => x.ToString()));
Expand Down
36 changes: 36 additions & 0 deletions test/Riok.Mapperly.Tests/Mapping/NullableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,42 @@ public void WithExistingInstanceNullableSource()
);
}

[Fact]
public void ToTargetShouldNotGenerateEmptyIfStatement()
{
var source = TestSourceBuilder.MapperWithBodyAndTypes(
"partial B Map(A source)",
"class A { public C Value { get; set; } }",
"class B { public D? Value { get; } }",
"class C { public int Id { get; set; } }",
"class D { public int Id { get; } }"
);

TestHelper
.GenerateMapper(source, TestHelperOptions.AllowDiagnostics)
.Should()
.HaveSingleMethodBody(
"""
var target = new global::B();
return target;
"""
);
}

[Fact]
public void ExistingTargetShouldNotGenerateEmptyIfStatement()
{
var source = TestSourceBuilder.MapperWithBodyAndTypes(
"partial void Map(A source, B destination)",
"class A { public C Value { get; set; } }",
"class B { public D? Value { get; } }",
"class C { public int Id { get; set; } }",
"class D { public int Id { get; } }"
);

TestHelper.GenerateMapper(source, TestHelperOptions.AllowDiagnostics).Should().HaveSingleMethodBody("");
}

[Fact]
public Task ShouldUpgradeNullabilityInDisabledNullableContext()
{
Expand Down

0 comments on commit 8cbeff1

Please sign in to comment.