feat: add @pgsql/traverse package for AST traversal #201
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Add @pgsql/traverse package for PostgreSQL AST traversal
Summary
This PR introduces a new
@pgsql/traversepackage that provides PostgreSQL AST traversal utilities using a visitor pattern similar to Babel's traverse functionality. The package offers two APIs:walkAPI: Features aNodePathclass for better context tracking, early return mechanism for skipping subtrees, and runtime schema integration for precise traversalvisitAPI: Maintains backward compatibility with a simpler visitor patternKey features:
@pgsql/transformto precisely identify which fields are Node types that need traversalfalseto skip subtree traversalReview & Testing Checklist for Human
@pgsql/transformresolves correctly when publishedfalsefrom a visitor actually prevents traversal into child nodesRecommended test plan: Create a small script that parses a complex SQL statement and uses both APIs to traverse and collect all node types, comparing results to ensure consistency.
Diagram
%%{ init : { "theme" : "default" }}%% graph TB subgraph "transform package" T1["packages/transform/src/index.ts"]:::minor-edit T2["packages/transform/src/17/runtime-schema.ts"]:::context end subgraph "traverse package (NEW)" N1["packages/traverse/package.json"]:::major-edit N2["packages/traverse/src/traverse.ts"]:::major-edit N3["packages/traverse/__test__/traverse.test.ts"]:::major-edit N4["packages/traverse/README.md"]:::major-edit end subgraph "types package" TY1["@pgsql/types"]:::context end T1 -->|exports runtime schema| N2 T2 -->|runtime schema data| T1 TY1 -->|Node types| N2 N2 -->|tested by| N3 subgraph Legend L1["Major Edit"]:::major-edit L2["Minor Edit"]:::minor-edit L3["Context/No Edit"]:::context end classDef major-edit fill:#90EE90 classDef minor-edit fill:#87CEEB classDef context fill:#FFFFFFNotes
visitfunction maintains the exact same API as the original pseudocode provided