Skip to content

Commit 0d949db

Browse files
Ensure whitespace-only if branches still execute assign side effects (#923)
* Initial plan * Fix if whitespace-only branch skipping assign execution Co-authored-by: sebastienros <1165805+sebastienros@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: sebastienros <1165805+sebastienros@users.noreply.github.com>
1 parent a731a03 commit 0d949db

File tree

2 files changed

+43
-11
lines changed

2 files changed

+43
-11
lines changed

Fluid.Tests/IfStatementTests.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,48 @@ public async Task IfProcessAwaitedAndElse(bool async)
210210

211211
Assert.Equal("c", sw.ToString());
212212
}
213+
214+
[Fact]
215+
public async Task IfExecutesAssignInElseBranchWhenWhitespaceOrCommentOnly()
216+
{
217+
var e = new IfStatement(
218+
BooleanExpression(false, async: false),
219+
TEXT("a"),
220+
new ElseStatement(new List<Statement>
221+
{
222+
new AssignStatement("x", new LiteralExpression(new StringValue("value")))
223+
})
224+
);
225+
226+
var context = new TemplateContext();
227+
228+
await e.WriteToAsync(new StringWriter(), HtmlEncoder.Default, context);
229+
230+
Assert.Equal("value", context.GetValue("x").ToStringValue());
231+
}
232+
233+
[Fact]
234+
public async Task IfExecutesAssignInElseIfBranchWhenWhitespaceOrCommentOnly()
235+
{
236+
var e = new IfStatement(
237+
BooleanExpression(false, async: false),
238+
TEXT("a"),
239+
null,
240+
new List<ElseIfStatement>
241+
{
242+
new ElseIfStatement(BooleanExpression(true, async: false), new List<Statement>
243+
{
244+
new AssignStatement("x", new LiteralExpression(new StringValue("value")))
245+
})
246+
}
247+
);
248+
249+
var context = new TemplateContext();
250+
251+
await e.WriteToAsync(new StringWriter(), HtmlEncoder.Default, context);
252+
253+
Assert.Equal("value", context.GetValue("x").ToStringValue());
254+
}
213255
}
214256

215257
sealed class AwaitedExpression : Expression

Fluid/Ast/IfStatement.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,6 @@ public override ValueTask<Completion> WriteToAsync(IFluidOutput output, TextEnco
127127

128128
if (elseIfConditionTask.Result.ToBooleanValue())
129129
{
130-
if (elseIf.IsWhitespaceOrCommentOnly)
131-
{
132-
return Statement.NormalCompletion;
133-
}
134-
135130
var writeTask = elseIf.WriteToAsync(output, encoder, context);
136131
if (!writeTask.IsCompletedSuccessfully)
137132
{
@@ -144,11 +139,6 @@ public override ValueTask<Completion> WriteToAsync(IFluidOutput output, TextEnco
144139

145140
if (Else != null)
146141
{
147-
if (Else.IsWhitespaceOrCommentOnly)
148-
{
149-
return Statement.NormalCompletion;
150-
}
151-
152142
return Else.WriteToAsync(output, encoder, context);
153143
}
154144
}
@@ -244,4 +234,4 @@ private async ValueTask<Completion> AwaitedElseBranch(
244234

245235
protected internal override Statement Accept(AstVisitor visitor) => visitor.VisitIfStatement(this);
246236
}
247-
}
237+
}

0 commit comments

Comments
 (0)