fix: support includeZero=true on #if helper (issue #285)#639
Merged
Conversation
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>
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Summary
Fixes #285
Two bugs are addressed:
Crash: passing any hash argument to
#if(e.g.{{#if value includeZero=true}}) threwSystem.InvalidOperationException: Sequence contains more than one elementbecauseConditionalBlockAccumulatorContextcalled.Single()on the argument list, which contains both the condition expression and theHashParametersExpression. Fixed by filtering outHashParametersExpressionbefore selecting the condition.Feature: implement
includeZero=trueto match Handlebars.js behaviour. When this literal hash param is present at compile time, the generated condition expression calls the newHandlebarsUtils.IsTruthyIncludingZeromethod instead ofIsTruthyOrNonEmpty, so that numeric zero is treated as truthy.Changes
ConditionalBlockAccumulatorContext.csincludeZeroat compile timeBoolishExpression.csIncludeZeroflagHandlebarsExpression.csincludeZerothroughBoolish()factoryHandlebarsExpressionVisitor.csIncludeZerowhen rebuilding aBoolishExpressionBoolishConverter.csIsTruthyIncludingZerowhen flag is setHandlebarsUtils.csIsTruthyIncludingZeromethodIssues/Issue285Tests.csTest plan
{{#if value includeZero=true}}withvalue = 0(int) renders block{{#if value includeZero=true}}withvalue = 0.0(double) renders block{{#if value includeZero=true}}withvalue = 1renders block{{#if value includeZero=false}}withvalue = 0does NOT render block{{#if value}}withvalue = 0still treats zero as falsy (no regression){{#if value includeZero=true}}withvalue = nullstill falsy{{#if value includeZero=true}}withvalue = ""still falsy{{#if value includeZero=true}}withvalue = falsestill falsy{{#if value includeZero=true}}withvalue = truerenders block🤖 Generated with Claude Code