feat: add @pgsql/traverse package for AST traversal#201
Merged
pyramation merged 10 commits intomainfrom Jul 1, 2025
Merged
Conversation
Contributor
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
- Implement visitor pattern for PostgreSQL AST nodes - Add comprehensive test suite with various node types - Follow existing package structure and build patterns - Support both individual nodes and ParseResult objects - Include Jest configuration for TypeScript testing - Add README with usage examples and API documentation Co-Authored-By: Dan Lynch <pyramation@gmail.com>
- 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 <pyramation@gmail.com>
…mports - Export runtime schema from @pgsql/transform package index - Add @pgsql/transform as dependency to @pgsql/traverse - Use proper package import instead of direct file import - Maintain all enhanced traverse functionality (NodePath, early return, runtime schema) Co-Authored-By: Dan Lynch <pyramation@gmail.com>
- Fix ParseResult nodes test by implementing proper recursive traversal in visit function - Correct context path generation to exclude node type names - All 10 tests now pass locally - Maintain backward compatibility with legacy visit API Co-Authored-By: Dan Lynch <pyramation@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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