fix(plpgsql-deparser): support nested DECLARE blocks inside FOR loops #271
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.
fix(plpgsql-deparser): support nested DECLARE blocks inside FOR loops
Summary
Implements lineno-based scope tracking to correctly assign variables to their enclosing blocks. Previously, variables declared inside a FOR loop body were hoisted to the top-level DECLARE section, which changed semantics (variables should be reinitialized on each loop iteration).
The fix uses line numbers from the AST to determine which variables belong to which blocks:
lineno <= topLevelBlockLinenostay in the top-level DECLARElineno > topLevelBlockLinenoare assigned to the nearest nested block whoselinenois greater than the variable'slinenoThis fixes the last remaining failing fixture (
plpgsql_varprops-13.sql). All 190 fixtures now pass round-trip testing.Review & Testing Checklist for Human
Recommended test plan: Run the round-trip tests locally (
npm testinpackages/plpgsql-deparser) and manually inspect the output forplpgsql_varprops-13.sqlto verify the nested DECLARE appears in the correct position.Notes
topLevelBlockLinenois undefined (discovered during debugging withbig-function.sql)