feat: add plpgsql-parse package with comment preservation for PL/pgSQL function bodies#293
Open
pyramation wants to merge 4 commits intomainfrom
Open
feat: add plpgsql-parse package with comment preservation for PL/pgSQL function bodies#293pyramation wants to merge 4 commits intomainfrom
pyramation wants to merge 4 commits intomainfrom
Conversation
…L function bodies New packages/plpgsql-parse/ package that wraps plpgsql-parser and plpgsql-deparser to preserve -- line comments and vertical whitespace inside PL/pgSQL function bodies through parse→deparse round trips. - body-scanner.ts: extract comments from function bodies with line numbers - parse.ts: combines pgsql-parse (outer SQL) + body comment extraction - deparse.ts: re-injects body comments into deparsed PL/pgSQL output - return-info.ts: extract return type info for correct RETURN handling - 57 tests, 8 fixtures with snapshots, all passing - No modifications to existing packages (plpgsql-parser, plpgsql-deparser, etc.)
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:
|
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.
Summary
New
packages/plpgsql-parse/workspace package that preserves--line comments inside PL/pgSQL function bodies through parse→deparse round trips. Wraps existingplpgsql-parserandplpgsql-deparserwithout modifying them — same pattern as thepgsql-parsepackage from PR #290.How it works:
pgsql-parsefor outer SQL comment/whitespace preservation, then for each PL/pgSQL function, scans the$$...$$body to extract--comments with their line numbers and anchor them to the nearest following statementKey modules:
body-scanner.ts— extract--comments from function body text, group by anchor statementparse.ts— enhanced parse combining pgsql-parse + body comment extractiondeparse.ts— re-inject body comments using keyword matching against PL/pgSQL ASTreturn-info.ts— extract return type info for correct plpgsql-deparser invocationTests: 57 tests (9 scanner + 9 parse + 39 round-trip), 8 SQL fixtures with snapshots covering: simple functions, multiple comment groups, trigger functions, exception handlers, loops, nested blocks, multi-function files, and no-comment baselines.
Updates since last revision
README.mdfor the package (was missing, causingmakage assetscopy step to fail in CI)jest.config.jsto match existing package conventions — removedmoduleNameMapperand customtransformIgnorePatterns; now uses the standardtransformIgnorePatterns: ['/node_modules/*']pattern likedeparserandplpgsql-deparsertsconfig.test.jsonentirely — pnpm handles workspace linking, no manualpathsmappings needed. Jest now usestsconfig.jsondirectly, matching the convention of other packages in the monorepoReview & Testing Checklist for Human
as anycasts inparse.tsanddeparse.ts—rawStmt?.stmtis cast toanyto accessCreateFunctionStmtand PL/pgSQL AST nodes. These bypass type safety and would silently break if AST shapes change upstream. Verify the access patterns match actual node shapes from@pgsql/typesandplpgsql-parser.deparse.ts— Comments are re-inserted by matching statement keywords (e.g.,IF,FOR,RETURN) against deparsed output lines. ReviewgetKeywordsForStmtKind()andlineMatchesKeywords()for edge cases where keywords could appear in string literals, column names, or unexpected formatting.collectStmtLinenosandbuildStmtKeywordsdo a DFS walk over the PL/pgSQL AST. Verify all relevant statement types are handled (especially less common ones likePLpgSQL_stmt_dynexecute,PLpgSQL_stmt_case,PLpgSQL_stmt_forc, etc.).pnpm-lock.yamlhas a large diff (mostly quote style reformatting from a pnpm version difference). Verify no unintended dependency additions or version changes beyond the newplpgsql-parseworkspace entry.cd packages/plpgsql-parse && npx jest --verboselocally. Try adding a new.sqlfixture with a complex real-world PL/pgSQL function (e.g., one with dynamic SQL, cursors, or nested exception blocks) to stress-test the comment round trip.Notes
--line comments are supported (no/* */block comments) — this was a deliberate decision.plpgsql-parser,plpgsql-deparser,pgsql-deparser, or any other existing packages.pgsql-parse).Link to Devin session: https://app.devin.ai/sessions/67facbcfe0ae424bad3eafb4e6ca9059
Requested by: @pyramation