Skip to content

Commit d557649

Browse files
committed
refactor(template): rename AST and children properties to ASTTag and tags
- Rename AST interface to ASTTag to better represent structure - Change all occurrences of children property to tags in parser and compiler - Update types to reflect ASTTag with tags array instead of children - Modify parser logic to use tags collection for nested nodes - Adjust tests and snapshots to expect tags instead of children - Update template compiler and tag implementations accordingly
1 parent 5b1ed2d commit d557649

8 files changed

Lines changed: 82 additions & 82 deletions

File tree

packages/template/src/compiler.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Parser } from './parser'
2-
import type { AST, ASTNode, EngineOptions } from './types'
2+
import type { ASTNode, ASTTag, EngineOptions } from './types'
33
import { CONTEXT } from './config'
44
import { OutScript } from './out-script'
55
import { SourceMap } from './source-map'
@@ -14,10 +14,10 @@ export class Compiler {
1414
if (parser.valid) {
1515
out.start()
1616

17-
const { children } = parser
18-
if (children.length) {
19-
for (const child of children) {
20-
await this.compileNode(template, child, CONTEXT, parser, out, sourcemap)
17+
const { tags } = parser
18+
if (tags.length) {
19+
for (const tag of tags) {
20+
await this.compileNode(template, tag, CONTEXT, parser, out, sourcemap)
2121
}
2222

2323
out.pushStr(template.slice(parser.cursor.endIndex), {
@@ -37,7 +37,7 @@ export class Compiler {
3737

3838
private async compileNode(
3939
template: string,
40-
{ nodes }: AST,
40+
{ nodes }: ASTTag,
4141
context: string,
4242
parser: Parser,
4343
out: OutScript,
@@ -93,9 +93,9 @@ export class Compiler {
9393
out: OutScript
9494
sourcemap: SourceMap
9595
}) {
96-
if (node.children.length) {
97-
for (const child of node.children) {
98-
await this.compileNode(template, child, context, parser, out, sourcemap)
96+
if (node.tags.length) {
97+
for (const tag of node.tags) {
98+
await this.compileNode(template, tag, context, parser, out, sourcemap)
9999
}
100100
}
101101
else {

packages/template/src/parser.test.ts

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,23 @@ it('empty', () => {
1212
const tree = new Parser({} as Required<EngineOptions>)
1313

1414
expect(tree.valid).toBe(true)
15-
expect(tree.children).toMatchInlineSnapshot(`[]`)
15+
expect(tree.tags).toMatchInlineSnapshot(`[]`)
1616

1717
tree.start({
1818
name: 'root',
1919
startIndex: 0,
2020
endIndex: 0,
2121
})
2222
expect(tree.valid).toBe(false)
23-
expect(tree.children).toMatchInlineSnapshot(`[]`)
23+
expect(tree.tags).toMatchInlineSnapshot(`[]`)
2424

2525
tree.end({
2626
name: 'end_root',
2727
startIndex: 0,
2828
endIndex: 0,
2929
})
3030
expect(tree.valid).toBe(true)
31-
expect(tree.children).toMatchInlineSnapshot(`[]`)
31+
expect(tree.tags).toMatchInlineSnapshot(`[]`)
3232
})
3333

3434
it('add nodes', () => {
@@ -37,7 +37,7 @@ it('add nodes', () => {
3737
let i = 0
3838

3939
expect(parser.valid).toBe(true)
40-
expect(parser.children).toMatchInlineSnapshot(`[]`)
40+
expect(parser.tags).toMatchInlineSnapshot(`[]`)
4141

4242
parser.start({
4343
name: 'root',
@@ -139,7 +139,7 @@ describe('validation', () => {
139139
endIndex: 1,
140140
})
141141
expect(parser.valid).toBe(true)
142-
expect(parser.children).toMatchInlineSnapshot(`[]`)
142+
expect(parser.tags).toMatchInlineSnapshot(`[]`)
143143
})
144144

145145
it('unexpected else node', () => {
@@ -151,7 +151,7 @@ describe('validation', () => {
151151
endIndex: 1,
152152
})
153153
expect(parser.valid).toBe(true)
154-
expect(parser.children).toMatchInlineSnapshot(`[]`)
154+
expect(parser.tags).toMatchInlineSnapshot(`[]`)
155155
})
156156

157157
it('unexpected next node', () => {
@@ -172,15 +172,15 @@ describe('validation', () => {
172172
expect(parser.nodes).toMatchInlineSnapshot(`
173173
[
174174
{
175-
"ast": AST,
176-
"children": [],
177175
"endIndex": 1,
178176
"name": "raw",
179177
"next": null,
180178
"nextSibling": null,
181179
"previous": null,
182180
"previousSibling": null,
183181
"startIndex": 0,
182+
"tag": AST,
183+
"tags": [],
184184
},
185185
]
186186
`)
@@ -204,15 +204,15 @@ describe('validation', () => {
204204
expect(parser.nodes).toMatchInlineSnapshot(`
205205
[
206206
{
207-
"ast": AST,
208-
"children": [],
209207
"endIndex": 1,
210208
"name": "raw",
211209
"next": null,
212210
"nextSibling": null,
213211
"previous": null,
214212
"previousSibling": null,
215213
"startIndex": 0,
214+
"tag": AST,
215+
"tags": [],
216216
},
217217
]
218218
`)
@@ -355,16 +355,14 @@ it('real world', async () => {
355355
await ast.parse(
356356
`{{= "hello, {name}" | t name="IJK" }}`,
357357
)
358-
expect(ast.children).toMatchInlineSnapshot(`
358+
expect(ast.tags).toMatchInlineSnapshot(`
359359
[
360360
{
361361
"index": 0,
362362
"level": 1,
363363
"nextSibling": null,
364364
"nodes": [
365365
{
366-
"ast": [Circular],
367-
"children": [],
368366
"data": ""hello, {name}" | t name="IJK"",
369367
"endIndex": 37,
370368
"identifier": "=",
@@ -381,15 +379,15 @@ it('real world', async () => {
381379
"nextSibling": null,
382380
"previous": [Circular],
383381
"previousSibling": {
384-
"ast": AST,
385-
"children": [Circular],
386382
"endIndex": 0,
387383
"name": "root",
388384
"next": [Circular],
389385
"nextSibling": [Circular],
390386
"previous": null,
391387
"previousSibling": null,
392388
"startIndex": 0,
389+
"tag": AST,
390+
"tags": [Circular],
393391
},
394392
"startIndex": 37,
395393
},
@@ -413,15 +411,15 @@ it('real world', async () => {
413411
"nextSibling": null,
414412
"previous": [Circular],
415413
"previousSibling": {
416-
"ast": AST,
417-
"children": [Circular],
418414
"endIndex": 0,
419415
"name": "root",
420416
"next": [Circular],
421417
"nextSibling": [Circular],
422418
"previous": null,
423419
"previousSibling": null,
424420
"startIndex": 0,
421+
"tag": AST,
422+
"tags": [Circular],
425423
},
426424
"startIndex": 37,
427425
},
@@ -435,8 +433,6 @@ it('real world', async () => {
435433
},
436434
"original": "{{= "hello, {name}" | t name="IJK" }}",
437435
"previous": {
438-
"ast": AST,
439-
"children": [Circular],
440436
"endIndex": 0,
441437
"name": "root",
442438
"next": [Circular],
@@ -465,11 +461,15 @@ it('real world', async () => {
465461
"previous": null,
466462
"previousSibling": null,
467463
"startIndex": 0,
464+
"tag": AST,
465+
"tags": [Circular],
468466
},
469467
"previousSibling": null,
470468
"startIndex": 0,
471469
"stripAfter": false,
472470
"stripBefore": false,
471+
"tag": [Circular],
472+
"tags": [],
473473
},
474474
{
475475
"data": ""hello, {name}" | t name="IJK"",
@@ -483,13 +483,9 @@ it('real world', async () => {
483483
"nextSibling": null,
484484
"previous": [Circular],
485485
"previousSibling": {
486-
"ast": AST,
487-
"children": [Circular],
488486
"endIndex": 0,
489487
"name": "root",
490488
"next": {
491-
"ast": [Circular],
492-
"children": [],
493489
"data": ""hello, {name}" | t name="IJK"",
494490
"endIndex": 37,
495491
"identifier": "=",
@@ -502,19 +498,21 @@ it('real world', async () => {
502498
"startIndex": 0,
503499
"stripAfter": false,
504500
"stripBefore": false,
501+
"tag": [Circular],
502+
"tags": [],
505503
},
506504
"nextSibling": [Circular],
507505
"previous": null,
508506
"previousSibling": null,
509507
"startIndex": 0,
508+
"tag": AST,
509+
"tags": [Circular],
510510
},
511511
"startIndex": 37,
512512
},
513513
"nextSibling": null,
514514
"original": "{{= "hello, {name}" | t name="IJK" }}",
515515
"previous": {
516-
"ast": [Circular],
517-
"children": [],
518516
"data": ""hello, {name}" | t name="IJK"",
519517
"endIndex": 37,
520518
"identifier": "=",
@@ -523,8 +521,6 @@ it('real world', async () => {
523521
"nextSibling": [Circular],
524522
"original": "{{= "hello, {name}" | t name="IJK" }}",
525523
"previous": {
526-
"ast": AST,
527-
"children": [Circular],
528524
"endIndex": 0,
529525
"name": "root",
530526
"next": [Circular],
@@ -540,15 +536,17 @@ it('real world', async () => {
540536
"previous": null,
541537
"previousSibling": null,
542538
"startIndex": 0,
539+
"tag": AST,
540+
"tags": [Circular],
543541
},
544542
"previousSibling": null,
545543
"startIndex": 0,
546544
"stripAfter": false,
547545
"stripBefore": false,
546+
"tag": [Circular],
547+
"tags": [],
548548
},
549549
"previousSibling": {
550-
"ast": [Circular],
551-
"children": [],
552550
"data": ""hello, {name}" | t name="IJK"",
553551
"endIndex": 37,
554552
"identifier": "=",
@@ -557,8 +555,6 @@ it('real world', async () => {
557555
"nextSibling": [Circular],
558556
"original": "{{= "hello, {name}" | t name="IJK" }}",
559557
"previous": {
560-
"ast": AST,
561-
"children": [Circular],
562558
"endIndex": 0,
563559
"name": "root",
564560
"next": [Circular],
@@ -574,11 +570,15 @@ it('real world', async () => {
574570
"previous": null,
575571
"previousSibling": null,
576572
"startIndex": 0,
573+
"tag": AST,
574+
"tags": [Circular],
577575
},
578576
"previousSibling": null,
579577
"startIndex": 0,
580578
"stripAfter": false,
581579
"stripBefore": false,
580+
"tag": [Circular],
581+
"tags": [],
582582
},
583583
"startIndex": 37,
584584
"stripAfter": false,

0 commit comments

Comments
 (0)