@wireweave/core 1.0.0-beta.20260107130839 → 1.0.0-beta.20260107132939

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/ast/utils.ts DELETED
@@ -1,238 +0,0 @@
1
- /**
2
- * AST utility functions for wireweave
3
- *
4
- * Provides traversal and search utilities for AST nodes
5
- */
6
-
7
- import type { AnyNode, WireframeDocument, NodeType } from './types';
8
- import { hasChildren } from './guards';
9
-
10
- /**
11
- * Callback function for AST traversal
12
- */
13
- export type WalkCallback = (node: AnyNode, parent?: AnyNode, depth?: number) => void | boolean;
14
-
15
- /**
16
- * Predicate function for finding nodes
17
- */
18
- export type NodePredicate = (node: AnyNode) => boolean;
19
-
20
- /**
21
- * Walk through all nodes in the AST
22
- *
23
- * @param node - The starting node
24
- * @param callback - Function called for each node. Return false to stop traversal.
25
- * @param parent - Parent node (used internally)
26
- * @param depth - Current depth (used internally)
27
- */
28
- export function walk(
29
- node: AnyNode,
30
- callback: WalkCallback,
31
- parent?: AnyNode,
32
- depth: number = 0
33
- ): void {
34
- const result = callback(node, parent, depth);
35
-
36
- // Stop traversal if callback returns false
37
- if (result === false) {
38
- return;
39
- }
40
-
41
- if (hasChildren(node)) {
42
- for (const child of node.children) {
43
- walk(child, callback, node, depth + 1);
44
- }
45
- }
46
- }
47
-
48
- /**
49
- * Walk through a document's AST
50
- *
51
- * @param document - The wireframe document
52
- * @param callback - Function called for each node
53
- */
54
- export function walkDocument(document: WireframeDocument, callback: WalkCallback): void {
55
- for (const page of document.children) {
56
- walk(page, callback);
57
- }
58
- }
59
-
60
- /**
61
- * Find the first node matching a predicate
62
- *
63
- * @param node - The starting node
64
- * @param predicate - Function to test each node
65
- * @returns The first matching node, or undefined
66
- */
67
- export function find(node: AnyNode, predicate: NodePredicate): AnyNode | undefined {
68
- if (predicate(node)) {
69
- return node;
70
- }
71
-
72
- if (hasChildren(node)) {
73
- for (const child of node.children) {
74
- const found = find(child, predicate);
75
- if (found) {
76
- return found;
77
- }
78
- }
79
- }
80
-
81
- return undefined;
82
- }
83
-
84
- /**
85
- * Find all nodes matching a predicate
86
- *
87
- * @param node - The starting node
88
- * @param predicate - Function to test each node
89
- * @returns Array of matching nodes
90
- */
91
- export function findAll(node: AnyNode, predicate: NodePredicate): AnyNode[] {
92
- const results: AnyNode[] = [];
93
-
94
- walk(node, (n) => {
95
- if (predicate(n)) {
96
- results.push(n);
97
- }
98
- });
99
-
100
- return results;
101
- }
102
-
103
- /**
104
- * Find all nodes of a specific type
105
- *
106
- * @param node - The starting node
107
- * @param type - The node type to find
108
- * @returns Array of matching nodes
109
- */
110
- export function findByType<T extends AnyNode>(
111
- node: AnyNode,
112
- type: NodeType
113
- ): T[] {
114
- return findAll(node, (n) => n.type === type) as T[];
115
- }
116
-
117
- /**
118
- * Count all nodes in the AST
119
- *
120
- * @param node - The starting node
121
- * @returns Total number of nodes
122
- */
123
- export function countNodes(node: AnyNode): number {
124
- let count = 0;
125
- walk(node, () => {
126
- count++;
127
- });
128
- return count;
129
- }
130
-
131
- /**
132
- * Get the maximum depth of the AST
133
- *
134
- * @param node - The starting node
135
- * @returns Maximum depth
136
- */
137
- export function getMaxDepth(node: AnyNode): number {
138
- let maxDepth = 0;
139
- walk(node, (_n, _p, depth) => {
140
- if (depth !== undefined && depth > maxDepth) {
141
- maxDepth = depth;
142
- }
143
- });
144
- return maxDepth;
145
- }
146
-
147
- /**
148
- * Get ancestors of a node (path from root to node)
149
- *
150
- * @param root - The root node
151
- * @param target - The target node to find
152
- * @returns Array of ancestor nodes, or empty array if not found
153
- */
154
- export function getAncestors(root: AnyNode, target: AnyNode): AnyNode[] {
155
- const path: AnyNode[] = [];
156
-
157
- function findPath(node: AnyNode, ancestors: AnyNode[]): boolean {
158
- if (node === target) {
159
- path.push(...ancestors);
160
- return true;
161
- }
162
-
163
- if (hasChildren(node)) {
164
- for (const child of node.children) {
165
- if (findPath(child, [...ancestors, node])) {
166
- return true;
167
- }
168
- }
169
- }
170
-
171
- return false;
172
- }
173
-
174
- findPath(root, []);
175
- return path;
176
- }
177
-
178
- /**
179
- * Map over all nodes in the AST
180
- *
181
- * @param node - The starting node
182
- * @param mapper - Function to transform each node
183
- * @returns New AST with transformed nodes
184
- */
185
- export function mapNodes<T>(node: AnyNode, mapper: (node: AnyNode) => T): T[] {
186
- const results: T[] = [];
187
- walk(node, (n) => {
188
- results.push(mapper(n));
189
- });
190
- return results;
191
- }
192
-
193
- /**
194
- * Clone an AST node (deep clone)
195
- *
196
- * @param node - The node to clone
197
- * @returns A deep clone of the node
198
- */
199
- export function cloneNode<T extends AnyNode>(node: T): T {
200
- return JSON.parse(JSON.stringify(node)) as T;
201
- }
202
-
203
- /**
204
- * Check if a node contains a specific child (at any depth)
205
- *
206
- * @param node - The parent node
207
- * @param target - The target node to find
208
- * @returns True if the target is found
209
- */
210
- export function contains(node: AnyNode, target: AnyNode): boolean {
211
- if (node === target) {
212
- return true;
213
- }
214
-
215
- if (hasChildren(node)) {
216
- for (const child of node.children) {
217
- if (contains(child, target)) {
218
- return true;
219
- }
220
- }
221
- }
222
-
223
- return false;
224
- }
225
-
226
- /**
227
- * Get all node types present in the AST
228
- *
229
- * @param node - The starting node
230
- * @returns Set of node types
231
- */
232
- export function getNodeTypes(node: AnyNode): Set<NodeType> {
233
- const types = new Set<NodeType>();
234
- walk(node, (n) => {
235
- types.add(n.type as NodeType);
236
- });
237
- return types;
238
- }