Skip to content

fix: support includeZero=true on #if helper (issue #285)#639

Merged
rexm merged 2 commits into
masterfrom
fix/issue-285
Jun 20, 2026
Merged

fix: support includeZero=true on #if helper (issue #285)#639
rexm merged 2 commits into
masterfrom
fix/issue-285

Conversation

@rexm

@rexm rexm commented Jun 20, 2026

Copy link
Copy Markdown
Member

Summary

Fixes #285

Two bugs are addressed:

  • Crash: passing any hash argument to #if (e.g. {{#if value includeZero=true}}) threw System.InvalidOperationException: Sequence contains more than one element because ConditionalBlockAccumulatorContext called .Single() on the argument list, which contains both the condition expression and the HashParametersExpression. Fixed by filtering out HashParametersExpression before selecting the condition.

  • Feature: implement includeZero=true to match Handlebars.js behaviour. When this literal hash param is present at compile time, the generated condition expression calls the new HandlebarsUtils.IsTruthyIncludingZero method instead of IsTruthyOrNonEmpty, so that numeric zero is treated as truthy.

Changes

File What changed
ConditionalBlockAccumulatorContext.cs Filter hash params from arg list; detect includeZero at compile time
BoolishExpression.cs Add IncludeZero flag
HandlebarsExpression.cs Propagate includeZero through Boolish() factory
HandlebarsExpressionVisitor.cs Preserve IncludeZero when rebuilding a BoolishExpression
BoolishConverter.cs Call IsTruthyIncludingZero when flag is set
HandlebarsUtils.cs Add IsTruthyIncludingZero method
Issues/Issue285Tests.cs 10 regression tests

Test plan

  • {{#if value includeZero=true}} with value = 0 (int) renders block
  • {{#if value includeZero=true}} with value = 0.0 (double) renders block
  • {{#if value includeZero=true}} with value = 1 renders block
  • {{#if value includeZero=false}} with value = 0 does NOT render block
  • {{#if value}} with value = 0 still treats zero as falsy (no regression)
  • {{#if value includeZero=true}} with value = null still falsy
  • {{#if value includeZero=true}} with value = "" still falsy
  • {{#if value includeZero=true}} with value = false still falsy
  • {{#if value includeZero=true}} with value = true renders block
  • Full suite: 1756 tests pass, 0 failures

🤖 Generated with Claude Code

Passing any hash argument to #if crashed with InvalidOperationException
("Sequence contains more than one element") because the argument list
included a HashParametersExpression alongside the condition and the
compiler called .Single() on it.

Fix the crash by filtering HashParametersExpression out of the argument
list before taking the condition expression. Then implement the
Handlebars.js `includeZero=true` option: when that literal hash param
is present, generate a call to the new HandlebarsUtils.IsTruthyIncludingZero
helper instead of IsTruthyOrNonEmpty, so that numeric zero is treated as
truthy rather than falsy.

The BoolishExpression AST node carries the new IncludeZero flag so the
choice of method can flow through HandlebarsExpressionVisitor into
BoolishConverter without any broader refactoring.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@rexm rexm enabled auto-merge June 20, 2026 21:18
@sonarqubecloud

Copy link
Copy Markdown

@rexm rexm merged commit d040d30 into master Jun 20, 2026
7 checks passed
@rexm rexm deleted the fix/issue-285 branch June 20, 2026 21:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

How to treat 0 as truthy

1 participant