Skip to content

Commit 8056f63

Browse files
committed
Enforce BuilderStep Before constraint at step application.
When a step declares Before = X, reject applying it if X is already in AppliedOrder so partial-order constraints are fully enforced at runtime.
1 parent 6087775 commit 8056f63

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

DesignPatterns.SourceGenerators/Syntax/GenerateBuilderSyntaxFactory.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,22 @@ private static void AppendConstraintGuards(
303303
}
304304
}
305305

306+
if (!string.IsNullOrEmpty(step.Before))
307+
{
308+
var before = allSteps.FirstOrDefault(s =>
309+
string.Equals(s.MethodName, step.Before, StringComparison.Ordinal)
310+
|| string.Equals(s.BindingName, step.Before, StringComparison.OrdinalIgnoreCase));
311+
if (before.MethodName is not null)
312+
{
313+
sb.Append(indent).AppendLine(
314+
$" if (state.AppliedOrder.Contains(\"{before.MethodName}\"))");
315+
sb.Append(indent).AppendLine(" {");
316+
sb.Append(indent).AppendLine(
317+
$" throw new InvalidOperationException(\"Step '{step.MethodName}' violates an After/Before constraint relative to '{before.MethodName}'.\");");
318+
sb.Append(indent).AppendLine(" }");
319+
}
320+
}
321+
306322
foreach (var predecessor in allSteps.Where(s =>
307323
!string.IsNullOrEmpty(s.Before)
308324
&& (string.Equals(s.Before, step.MethodName, StringComparison.Ordinal)

0 commit comments

Comments
 (0)