Skip to content
Source
Sign inSign up
TanStack/devtools
Open History to see the last commit

ast-utils.test.ts

109 lines3.1 KB

1import { describe, expect, it } from 'vitest'
2import { forEachChild, walk } from './ast-utils'
3import type { Node } from 'oxc-parser'
4​
5describe('forEachChild', () => {
6 it('visits only direct typed children, skipping null and non-type values', () => {
7 const childA = { type: 'ChildA', start: 0, end: 1 }
8 const childB = { type: 'ChildB', start: 1, end: 2 }
9 const nested = { type: 'Nested', start: 2, end: 3 }
10 const root = {
11 type: 'ForEachRoot',
12 start: 0,
13 end: 10,
14 // array-valued key holding two typed children
15 items: [childA, childB],
16 // object-valued typed child
17 body: nested,
18 // null field: skipped
19 optional: null,
20 // primitive string value: not a child (and excluded from child keys)
21 name: 'hello',
22 // primitive number value: not a child
23 count: 5,
24 } as unknown as Node
25​
26 const visited: Array<Node> = []
27 forEachChild(root, (child) => visited.push(child))
28​
29 expect(visited).toEqual([childA, childB, nested])
30 expect(visited).toHaveLength(3)
31 })
32​
33 it('skips non-object array items and array items without a type', () => {
34 const real = { type: 'RealItem', start: 0, end: 1 }
35 const root = {
36 type: 'ForEachArrayRoot',
37 start: 0,
38 end: 10,
39 items: [real, null, 'string-item', 42, { noType: true }],
40 } as unknown as Node
41​
42 const visited: Array<Node> = []
43 forEachChild(root, (child) => visited.push(child))
44​
45 expect(visited).toEqual([real])
46 })
47})
48​
49describe('walk', () => {
50 it('visits every node depth-first with the correct parentNode', () => {
51 const grandchild = { type: 'WalkGrandchild', start: 4, end: 5 }
52 const childA = {
53 type: 'WalkChildA',
54 start: 1,
55 end: 6,
56 inner: grandchild,
57 }
58 const childB = { type: 'WalkChildB', start: 6, end: 7 }
59 const root = {
60 type: 'WalkRoot',
61 start: 0,
62 end: 10,
63 children: [childA, childB],
64 } as unknown as Node
65​
66 const visits: Array<{ node: Node; parent: Node | undefined }> = []
67 walk(root, (node, parentNode) => {
68 visits.push({ node, parent: parentNode })
69 })
70​
71 // Depth-first order: root, childA, grandchild, childB
72 expect(visits.map((v) => v.node.type)).toEqual([
73 'WalkRoot',
74 'WalkChildA',
75 'WalkGrandchild',
76 'WalkChildB',
77 ])
78​
79 // Correct parent for each node
80 expect(visits[0]!.parent).toBeUndefined()
81 expect(visits[1]!.parent).toBe(root)
82 expect(visits[2]!.parent).toBe(childA)
83 expect(visits[3]!.parent).toBe(root)
84 })
85​
86 it('passes the provided parentNode through to the root visit', () => {
87 const fakeParent = {
88 type: 'WalkExternalParent',
89 start: 0,
90 end: 1,
91 } as unknown as Node
92 const root = {
93 type: 'WalkRootWithParent',
94 start: 1,
95 end: 2,
96 } as unknown as Node
97​
98 const visits: Array<{ node: Node; parent: Node | undefined }> = []
99 walk(
100 root,
101 (node, parentNode) => visits.push({ node, parent: parentNode }),
102 fakeParent,
103 )
104​
105 expect(visits).toHaveLength(1)
106 expect(visits[0]!.node).toBe(root)
107 expect(visits[0]!.parent).toBe(fakeParent)
108 })
109})