You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: improve traverse with NodePath, early return, and runtime schema
- Add NodePath class for better context handling with proper TypeScript generics
- Implement early return mechanism (return false to skip subtrees)
- Use runtime schema for precise traversal instead of guessing which fields are nodes
- Add comprehensive tests for new functionality including NodePath properties and early return
- Maintain backward compatibility with existing visit API as wrapper around new walk API
- Update documentation with new walk API examples and NodePath class details
- Fix traversal logic to handle both schema-guided and fallback traversal modes
Co-Authored-By: Dan Lynch <[email protected]>
console.log(`Visiting ${path.tag} at path:`, path.path);
24
+
25
+
// Return false to skip traversing children
26
+
if (path.tag==='SelectStmt') {
27
+
returnfalse; // Skip SELECT statement children
28
+
}
29
+
};
18
30
31
+
walk(ast, walker);
32
+
33
+
// Using a visitor object (recommended for multiple node types)
19
34
const visitor:Visitor= {
35
+
SelectStmt: (path) => {
36
+
console.log('SELECT statement:', path.node);
37
+
},
38
+
RangeVar: (path) => {
39
+
console.log('Table:', path.node.relname);
40
+
console.log('Path to table:', path.path);
41
+
console.log('Parent node:', path.parent?.tag);
42
+
}
43
+
};
44
+
45
+
walk(ast, visitor);
46
+
```
47
+
48
+
### NodePath Class
49
+
50
+
The `NodePath` class provides rich context information:
51
+
52
+
```typescript
53
+
classNodePath<TTagextendsNodeTag=NodeTag> {
54
+
tag:TTag; // Node type (e.g., 'SelectStmt', 'RangeVar')
55
+
node:Node[TTag]; // The actual node data
56
+
parent:NodePath|null; // Parent NodePath (null for root)
57
+
keyPath:readonly (string|number)[]; // Full path array
58
+
59
+
get path(): (string|number)[]; // Copy of keyPath
60
+
get key():string|number; // Last element of path
61
+
}
62
+
```
63
+
64
+
### Runtime Schema Integration
65
+
66
+
The new implementation uses PostgreSQL's runtime schema to precisely determine which fields contain Node types that need traversal, eliminating guesswork and improving accuracy.
67
+
68
+
### Legacy Visit API (Backward Compatible)
69
+
70
+
The original `visit` function is still available for backward compatibility:
0 commit comments