Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions packages/markdown-template/src/TemplateMarkTransformer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});
18 changes: 15 additions & 3 deletions packages/markdown-template/src/TypeVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down