Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent generation of empty if not null statements #576

Merged
merged 1 commit into from
Jul 19, 2023
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
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