Skip to content

fix(markdown-template): set elementType on join and list blocks over primitive arrays - #687

Open
rajanpanth wants to merge 1 commit into
accordproject:mainfrom
rajanpanth:fix/type-primitive-array-blocks
Open

rajanpanth wants to merge 1 commit into
accordproject:mainfrom
rajanpanth:fix/type-primitive-array-blocks

Conversation

@rajanpanth

Copy link
Copy Markdown

TypeVisitor only sets elementType on JoinDefinition, ListBlockDefinition and ForeachDefinition when the array holds concepts. Over a primitive array (String[], Integer[], DateTime[], ...) the node has no elementType, so template-engine can't pick a drafter for the items.

Drafting with template-engine main (519ee9d) and the published @accordproject/markdown-template@2.0.0:

Template 2.0.0 This PR
{{#join counts}}{{/join}} (Integer[] = [1, 2, 3]) throws Iterable yielded 1 which is not a string 1, 2, and 3
{{#join rates}}{{/join}} (Double[] = [1.5, 2]) throws Iterable yielded 1.5 which is not a string 1.5 and 2.0
{{#join flags}}{{/join}} (Boolean[]) throws Iterable yielded true which is not a string true and false
{{#join dates}}{{/join}} (DateTime[]) 2024-03-05T00:00:00.000Z and 2024-04-01T00:00:00.000Z 03/05/2024 and 04/01/2024
{{#join names}}{{/join}} (String[]) Ann and Ben Ann and Ben

The throw comes from Intl.ListFormat, which only accepts strings. Without an element type, template-engine passes the raw numbers and booleans through.

Changes

  • TypeVisitor: set elementType from the property for primitive arrays as well. The child scope is unchanged (still the property for primitives, the class declaration for concepts).
  • Tests in TemplateMarkTransformer.test.ts: #join over Integer[], DateTime[] and String[]; #ulist/#olist over primitive arrays; and arrays of concepts, which keep their class type. The four primitive-array tests fail without the change.

Flags

  • The TemplateMark DOM gains elementType on join and list nodes over primitive arrays. With this build, template-engine's suite passes except one snapshot (TemplateMarkToJavaScriptCompiler.test.ts), which gains "elementType": "String" on its String[] join and list nodes. That snapshot needs updating when template-engine bumps the dependency.
  • {{#foreach}} isn't covered by a test: it isn't parsed into a ForeachDefinition today (it comes out as text), and template-engine skips its foreach template as broken. The case here sets the field for it the same way.

Author Checklist

  • Ensure you provide a DCO sign-off for your commits using the --signoff option of git commit.
  • Vital features and changes captured in unit and/or integration tests
  • Commits messages follow AP format
  • Extend the documentation, if necessary
  • Merging to main from fork:branchname

All workspace tests pass locally (npm run build && npm test) with no snapshot changes in this repo.

🤖 Generated with Claude Code

…primitive arrays

TypeVisitor only set elementType on JoinDefinition, ListBlockDefinition
and ForeachDefinition when the array held concepts. Over a primitive
array (String[], Integer[], DateTime[], ...) the node had no
elementType, so template-engine could not pick a drafter for the items:
joining an Integer[], Double[] or Boolean[] throws "Iterable yielded 1
which is not a string" from Intl.ListFormat, and a DateTime[] is joined
as raw ISO strings instead of formatted dates.

Set elementType from the property for primitive arrays too. The scope
for the children is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: rajanpanth <rajan.pantha@samriddhicollege.edu.np>

This branch has not been deployed

No deployments
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.

1 participant