Skip to content

Commit 75e21df

Browse files
committed
Some fixes
Created childCtx for parent d-nodes
1 parent 865504d commit 75e21df

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

src/parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ export default class AST {
274274
* Recursively adds dot-notation paths to tag nodes in the AST.
275275
* Raw text nodes are ignored.
276276
*/
277-
private static addPaths(nodes: ParseToken[], currentPath = ''): void {
277+
static addPaths(nodes: ParseToken[], currentPath = ''): void {
278278
for (const [index, node] of nodes.entries()) {
279279
// Only add paths to single or double tags, not raw text
280280
if (node.name && (node.single || node.double)) {

src/runtime.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ export default class Runtime {
158158
return true; // Indicate that the file was changed
159159
}
160160

161+
/**
162+
* Evaluate all tags in the AST and return the final text.
163+
*/
161164
async evaluateAll(customCtx: Record<string, any> = {}): Promise<string> {
162165
if (this.state.running) {
163166
// Should I return an error here?
@@ -190,6 +193,10 @@ export default class Runtime {
190193
return text;
191194
}
192195

196+
/**
197+
* Evaluate a specific tag and its children.
198+
* This is a lower-level method, use `evaluateAll` for the whole AST.
199+
*/
193200
async evaluateTag(tag: T.ParseToken, customCtx: Record<string, any> = {}): Promise<void> {
194201
if (!tag.name) {
195202
return; // Raw text or invalid tag
@@ -260,6 +267,9 @@ export default class Runtime {
260267
const children = new AST(this.config).parse(result);
261268
if (children.length) {
262269
tag.children = children;
270+
// Sync indexes and paths after parsing children
271+
this.ast.syncIndexes();
272+
AST.addPaths(this.ast.nodes);
263273
}
264274
}
265275
}
@@ -286,7 +296,8 @@ export default class Runtime {
286296
// a separate variable scope for the children
287297
// At this point, the parent props are interpolated
288298
if (evalChildren) {
289-
const childrenCtx = deepClone(customCtx);
299+
// The children scope is also saved in the parent tag
300+
(tag as T.DoubleTag).childCtx = deepClone(customCtx);
290301
for (const c of tag.children) {
291302
if (c.name && (c.single || c.double)) {
292303
c.parent = { name: tag.name, index: tag.index, params: tag.params, rawText: '' };
@@ -302,7 +313,7 @@ export default class Runtime {
302313
}
303314
}
304315

305-
await this.evaluateTag(c, childrenCtx);
316+
await this.evaluateTag(c, (tag as T.DoubleTag).childCtx);
306317
}
307318
}
308319
}

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export type DoubleTag = {
4848
firstTagText: string;
4949
secondTagText: string;
5050
children?: ParseToken[];
51+
childCtx?: Record<string, any>;
5152
params?: Record<string, any>;
5253
rawParams?: Record<string, string>;
5354
parent?: ParseToken;

test/evaluate.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ test('simple evaluate', async () => {
2323
path: '1',
2424
firstTagText: '<main>',
2525
secondTagText: '</main>',
26+
childCtx: {},
2627
children: [
2728
{
2829
rawText: '9',
@@ -58,19 +59,22 @@ test('evaluate custom tags', async () => {
5859
t1: (_s, _a, meta) => {
5960
expect(meta.node.children.length).toBe(5);
6061
expect(meta.node.parent).toEqual({});
62+
expect(meta.node.childCtx).toEqual({ t4: 23 });
6163
meta.node.params.x = 'x';
6264
return meta.node;
6365
},
6466
t2: (_s, _a, meta) => {
6567
expect(meta.node.children.length).toBe(3);
6668
expect(meta.node.parent.name).toBe('t1');
69+
expect(meta.node.childCtx).toEqual({ t3: 12 });
6770
meta.node.params = { z: 'z' };
6871
return meta.node;
6972
},
7073
t3: (_s, _a, meta) => {
7174
expect(meta.node.rawText).toBe('<t3 "a" b=1 />');
7275
expect(meta.node.parent.name).toBe('t2');
7376
meta.node.params.b = 2;
77+
meta.globalCtx.t3 = 12;
7478
return meta.node;
7579
},
7680
t4: (_s, _a, meta) => {
@@ -79,6 +83,7 @@ test('evaluate custom tags', async () => {
7983
expect(meta.node.parent.name).toBe('t1');
8084
meta.node.params.d = true;
8185
meta.node.params.e = 'e';
86+
meta.globalCtx.t4 = 23;
8287
return meta.node;
8388
},
8489
});

0 commit comments

Comments
 (0)