From 508930a4ec37f1a33c4007492d61330c91ab3c57 Mon Sep 17 00:00:00 2001 From: rajanpanth Date: Sat, 7 Nov 2026 19:31:06 +0545 Subject: [PATCH] fix(markdown-template): resolve {{this}} in blocks over enum and scalar types A list, join or foreach over an enum or scalar array scoped its children to the enum or scalar declaration, and an #optional block over a scalar-typed property scoped to the scalar declaration. A declaration has none of a property's methods, so {{this}} inside such a block threw "property.isPrimitive is not a function": {{#ulist colors}}- {{this}}{{/ulist}} (Color[], an enum) {{#ulist names}}- {{this}}{{/ulist}} (Name[], a scalar) {{#optional nickname}}{{this}}{{/optional}} (Name, optional) Keep the property itself as the model for these blocks, as for primitives: {{this}} then becomes an EnumVariableDefinition with the enum's values, or a VariableDefinition typed by the scalar, and a scalar #optional block keeps the parent scope like a primitive one. Blocks over concept arrays still scope to the concept. Co-Authored-By: Claude Opus 5.5 Signed-off-by: rajanpanth --- .../src/TemplateMarkTransformer.test.ts | 63 +++++++++++++++++++ packages/markdown-template/src/TypeVisitor.ts | 18 +++++- 2 files changed, 78 insertions(+), 3 deletions(-) diff --git a/packages/markdown-template/src/TemplateMarkTransformer.test.ts b/packages/markdown-template/src/TemplateMarkTransformer.test.ts index 93ce4fb0..25873891 100644 --- a/packages/markdown-template/src/TemplateMarkTransformer.test.ts +++ b/packages/markdown-template/src/TemplateMarkTransformer.test.ts @@ -177,4 +177,67 @@ describe('#TemplateMarkTransformer', () => { }); }); }); + + describe('#{{this}} over enum and scalar types', () => { + const VALUE_MODEL = ` +namespace test@1.0.0 +scalar Name extends String +scalar Signed extends DateTime +enum Color { + o RED + o BLUE +} +concept Party { + o String name +} +@template +concept TemplateData { + o String title + o Name[] names + o Signed[] dates + o Color[] colors + o Party[] parties + o Name nickname optional +}`; + const parse = (content: string) => { + const transformer = new TemplateMarkTransformer(); + const modelManager = new ModelManager(); + modelManager.addCTOModel(VALUE_MODEL); + return transformer.fromMarkdownTemplate({ content }, modelManager, 'clause'); + }; + const variables = (node: any): any[] => { + const own = [`${TemplateMarkModel.NAMESPACE}.VariableDefinition`, `${TemplateMarkModel.NAMESPACE}.EnumVariableDefinition`] + .includes(node.$class) ? [node] : []; + const children = ['nodes', 'whenTrue', 'whenFalse', 'whenSome', 'whenNone'] + .flatMap((key) => node[key] || []) + .flatMap(variables); + return [...own, ...children]; + }; + const self = (content: string) => variables(parse(content)).find((v) => v.name === 'this'); + + it('should type {{this}} in a list over a scalar array by the scalar', () => { + expect(self('{{#ulist names}}\n- {{this}}\n{{/ulist}}').elementType).toBe('test@1.0.0.Name'); + expect(self('{{#olist dates}}\n- {{this}}\n{{/olist}}').elementType).toBe('test@1.0.0.Signed'); + }); + + it('should make {{this}} in a list over an enum array an enum variable', () => { + const color = self('{{#ulist colors}}\n- {{this}}\n{{/ulist}}'); + expect(color.$class).toBe(`${TemplateMarkModel.NAMESPACE}.EnumVariableDefinition`); + expect(color.elementType).toBe('test@1.0.0.Color'); + expect(color.enumValues).toEqual(['RED', 'BLUE']); + }); + + it('should keep the parent scope in a scalar #optional block', () => { + const vars = variables(parse('{{#optional nickname}}{{this}} for {{title}}.{{/optional}}')); + expect(Object.fromEntries(vars.map((v) => [v.name, v.elementType]))).toEqual({ + this: 'test@1.0.0.Name', + title: 'String', + }); + }); + + it('should still scope a list over a concept array to the concept', () => { + expect(variables(parse('{{#ulist parties}}\n- {{name}}\n{{/ulist}}')).map((v) => v.elementType)).toEqual(['String']); + expect(() => parse('{{#ulist names}}\n- {{title}}\n{{/ulist}}')).toThrow('Unknown property: title'); + }); + }); }); diff --git a/packages/markdown-template/src/TypeVisitor.ts b/packages/markdown-template/src/TypeVisitor.ts index ce80bded..24a50cd8 100644 --- a/packages/markdown-template/src/TypeVisitor.ts +++ b/packages/markdown-template/src/TypeVisitor.ts @@ -84,6 +84,16 @@ export class TypeVisitor { }); } + /** + * Whether a property holds a single value rather than a concept: a primitive, + * an enum or a scalar. Blocks over such a property keep the property itself as + * the model, so {{this}} resolves to it; an enum or scalar declaration has no + * properties of its own to scope to. + */ + static isValueProperty(property: any): boolean { + return property.isPrimitive() || property.isTypeEnum() || Boolean(property.isTypeScalar?.()); + } + static nextModel(property: any, parameters: any): any { const declaration = property.isPrimitive() ? null : parameters.introspector.getClassDeclaration(property.getFullyQualifiedTypeName()); return { @@ -230,7 +240,9 @@ export class TypeVisitor { nextModel = property; } else { thing.elementType = property.getFullyQualifiedTypeName(); - nextModel = parameters.introspector.getClassDeclaration(thing.elementType); + nextModel = TypeVisitor.isValueProperty(property) + ? property + : parameters.introspector.getClassDeclaration(thing.elementType); } TypeVisitor.visitChildren(this, thing, { templateMarkModelManager: parameters.templateMarkModelManager, @@ -275,8 +287,8 @@ export class TypeVisitor { const serializer = parameters.templateMarkModelManager.getSerializer(); thing.decorators = processDecorators(serializer, property); thing.elementType = property.getFullyQualifiedTypeName(); - // A primitive or enum optional keeps the parent scope; a complex one scopes to its class - const isScalar = property.isPrimitive() || property.isTypeEnum(); + // A primitive, enum or scalar optional keeps the parent scope; a complex one scopes to its class + const isScalar = TypeVisitor.isValueProperty(property); const nextModel = isScalar ? currentModel : parameters.introspector.getClassDeclaration(thing.elementType); TypeVisitor.visitChildren(this, thing, { templateMarkModelManager: parameters.templateMarkModelManager,