Skip to content

Commit 4f6495f

Browse files
committed
Fix build break in Builder with no fields.
1 parent b3393be commit 4f6495f

File tree

2 files changed

+22
-12
lines changed
  • ImmutableObjectGraph.CodeGeneration
  • ImmutableObjectGraph.CodeGeneration.Tests/TestSources

2 files changed

+22
-12
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
[ImmutableObjectGraph.CodeGeneration.GenerateImmutable]
1+
[ImmutableObjectGraph.CodeGeneration.GenerateImmutable(GenerateBuilder = true)]
22
partial class Empty { }

ImmutableObjectGraph.CodeGeneration/CodeGen.cs

+21-11
Original file line numberDiff line numberDiff line change
@@ -860,18 +860,28 @@ protected IReadOnlyList<MemberDeclarationSyntax> CreateMutableProperties()
860860

861861
protected MethodDeclarationSyntax CreateToImmutableMethod()
862862
{
863-
// return this.immutable = this.immutable.With(...)
863+
ExpressionSyntax returnExpression;
864+
if (this.generator.applyToMetaType.AllFields.Any())
865+
{
866+
// this.immutable = this.immutable.With(...)
867+
returnExpression = SyntaxFactory.AssignmentExpression(
868+
SyntaxKind.SimpleAssignmentExpression,
869+
Syntax.ThisDot(ImmutableFieldName),
870+
SyntaxFactory.InvocationExpression(
871+
SyntaxFactory.MemberAccessExpression(
872+
SyntaxKind.SimpleMemberAccessExpression,
873+
Syntax.ThisDot(ImmutableFieldName),
874+
WithMethodName),
875+
this.generator.CreateArgumentList(this.generator.applyToMetaType.AllFields, ArgSource.Property, OptionalStyle.Always)));
876+
}
877+
else
878+
{
879+
// this.immutable
880+
returnExpression = Syntax.ThisDot(ImmutableFieldName);
881+
}
882+
864883
var body = SyntaxFactory.Block(
865-
SyntaxFactory.ReturnStatement(
866-
SyntaxFactory.AssignmentExpression(
867-
SyntaxKind.SimpleAssignmentExpression,
868-
Syntax.ThisDot(ImmutableFieldName),
869-
SyntaxFactory.InvocationExpression(
870-
SyntaxFactory.MemberAccessExpression(
871-
SyntaxKind.SimpleMemberAccessExpression,
872-
Syntax.ThisDot(ImmutableFieldName),
873-
WithMethodName),
874-
this.generator.CreateArgumentList(this.generator.applyToMetaType.AllFields, ArgSource.Property, OptionalStyle.Always)))));
884+
SyntaxFactory.ReturnStatement(returnExpression));
875885

876886
// public TemplateType ToImmutable() { ... }
877887
var method = SyntaxFactory.MethodDeclaration(

0 commit comments

Comments
 (0)