feat: scaffold plpgsql-deparser package for PL/pgSQL AST deparsing #251
+6,489
−5,403
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.
feat: scaffold plpgsql-deparser package for PL/pgSQL AST deparsing
Summary
This PR adds a new
plpgsql-deparserpackage that converts PL/pgSQL function ASTs back to SQL strings. The PL/pgSQL AST (returned byparsePlPgSQLfrom@libpg-query/parser) is structurally different from the regular SQL AST and represents the internal structure of PL/pgSQL function bodies.Background: Investigation confirmed that
@libpg-query/parser(libpg-query-node'sfull/package) exportsparsePlPgSQLandparsePlPgSQLSyncfunctions, butpgsql-parserdoes not currently re-export these. This package provides the deparsing counterpart for PL/pgSQL ASTs.Supported constructs include:
Updates since last revision
CALLfrom the parservar.lineno === stmt.lineno). Explicitly declared variables in DECLARE sections are preserved even when used in FOR loops.__fixtures__/plpgsql-pretty/with sample SQL files (simple-function.sql, if-else-function.sql, loop-function.sql)PlpgsqlPrettyTestutility class for generating snapshot testsReview & Testing Checklist for Human
var.lineno === stmt.linenoto detect implicitly declared loop variables. This heuristic could fail with same-line declarations or minified SQL. CheckcollectLoopVariables()inplpgsql-deparser.ts.plpgsql_domainfixtures pass full round-trip testing. Other categories (plpgsql_control, plpgsql_transaction) still fail due to cursor declarations and complex SQL expression handling.Recommended test plan:
pnpm installandpnpm testinpackages/plpgsql-deparser/npm run fixturesto verify fixture generation worksNotes
pgsql-parserexports; users need to use@libpg-query/parserdirectly for parsing