Repository navigation
fix(markdown-template): preserve parent scope in conditional and optional blocks - #683
Merged
Merged
Conversation
…onal blocks
Variables inside {{#if}} and primitive {{#optional}} blocks were resolved
against the guarded property instead of the template model, so templates
such as `{{#if age}}You are {{age}}{{/if}}` failed with "Unknown property".
The {{else}} branches had no model at all.
Both branches now keep the parent scope. {{this}} still resolves to the
guarded property, and complex optionals still scope to the nested class.
Ported to TypeVisitor.ts from #647.
Co-authored-by: Yash Goel <162511050+yashhzd@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Matt Roberts <code@rbrts.uk>
Coverage Report for CI Build 38133185014Coverage increased (+3.9%) to 82.866%Details
Uncovered ChangesNo uncovered changes found. Coverage RegressionsNo coverage regressions found. Coverage Stats💛 - Coveralls |
mttrbrts
added a commit
that referenced
this pull request
Oct 11, 2026
property.isPrimitive() is false for enum fields, so {{#optional billingFrequency}}
on an enum property still scoped its body to the enum declaration and
rejected {{billingFrequency}}, {{this}} and sibling variables. Treat enum optionals
as scalars, like primitives.
Follow-up to #683, raised in review of #647.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Matt Roberts <code@rbrts.uk>
mttrbrts
added a commit
that referenced
this pull request
Oct 11, 2026
property.isPrimitive() is false for enum fields, so {{#optional billingFrequency}}
on an enum property still scoped its body to the enum declaration and
rejected {{billingFrequency}}, {{this}} and sibling variables. Treat enum optionals
as scalars, like primitives.
Follow-up to #683, raised in review of #647.
Signed-off-by: Matt Roberts <code@rbrts.uk>
Co-authored-by: Claude Opus 5.5 <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.
Ports the fix from #647 by @yashhzd to the TypeScript
TypeVisitor.tsintroduced in #677. #647 can't merge now because it edits the removedsrc/TypeVisitor.jsandlib/TypeVisitor.js. The commit credits @yashhzd as co-author.Fixes accordproject/template-playground#2 (Cannot use variables inside an #if block)
Problem
TypeVisitorset the scope of an{{#if}}body, and of a primitive{{#optional}}body, to the guarded field itself. A field has no properties, so any named variable inside the block failed type-checking:The
{{else}}branches (whenFalseandwhenNone) were visited withmodel: null, so every variable in them failed as well.Changes
All changes are in
packages/markdown-template/src/TypeVisitor.ts.ConditionalDefinition: both branches keep the parent model.OptionalDefinition:whenSometo the nested class.{{this}}: a newthisPropertyparameter carries the guarded field.{{this}}inside{{#if age}}or{{#optional age}}therefore still resolves toInteger, as it does today.One difference from #647: #647 dropped the
{{this}}handling for#if, and this PR keeps it.Tests
New Jest cases in
src/TemplateMarkTransformer.test.tscover:#if,#optionaland their{{else}}branches#if{{this}}in#ifand in a primitive#optional#optionalstill scoping to the nested classResults:
TypeVisitor.tschange reverted, 8 of the new tests fail.markdown-templatepasses: 26 tests.markdown-transformpasses: 31 tests.tsc --noEmitis clean.🤖 Generated with Claude Code