plpgsql-parse 0.2.5 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/deparse.js +19 -3
- package/esm/deparse.js +19 -3
- package/package.json +5 -5
package/deparse.js
CHANGED
|
@@ -152,10 +152,21 @@ function reinjectBodyComments(originalBody, deparsedBody, comments, plpgsqlAst)
|
|
|
152
152
|
const stmtKeywords = buildStmtKeywords(plpgsqlAst);
|
|
153
153
|
let stmtKeyIdx = 0;
|
|
154
154
|
const usedAnchors = new Set();
|
|
155
|
+
// Track DECLARE sections: lines between DECLARE and the following BEGIN
|
|
156
|
+
// are variable declarations, not statements, and must not consume
|
|
157
|
+
// statement keyword matches (e.g. a declaration `v_x int;` would
|
|
158
|
+
// otherwise match an assignment to `v_x`).
|
|
159
|
+
let inDeclare = false;
|
|
155
160
|
for (const line of depLines) {
|
|
156
161
|
const trimmed = line.trim().toUpperCase();
|
|
162
|
+
if (trimmed === 'DECLARE') {
|
|
163
|
+
inDeclare = true;
|
|
164
|
+
}
|
|
165
|
+
else if (inDeclare && trimmed.startsWith('BEGIN')) {
|
|
166
|
+
inDeclare = false;
|
|
167
|
+
}
|
|
157
168
|
// Try to match this line to the next expected statement
|
|
158
|
-
if (stmtKeyIdx < stmtKeywords.length) {
|
|
169
|
+
if (!inDeclare && stmtKeyIdx < stmtKeywords.length) {
|
|
159
170
|
const { lineno, keywords } = stmtKeywords[stmtKeyIdx];
|
|
160
171
|
if (lineMatchesKeywords(trimmed, keywords)) {
|
|
161
172
|
// Insert comments anchored to this statement
|
|
@@ -505,19 +516,24 @@ function findLastEndLine(lines) {
|
|
|
505
516
|
}
|
|
506
517
|
/**
|
|
507
518
|
* Replace the function body in a CREATE FUNCTION AST node.
|
|
519
|
+
* The body is padded with newlines so it renders as
|
|
520
|
+
* `AS $$\n<body>\n$$` rather than gluing onto the dollar quotes.
|
|
508
521
|
*/
|
|
509
522
|
function stitchBodyIntoAst(createFunctionStmt, newBody) {
|
|
510
523
|
if (!createFunctionStmt?.options)
|
|
511
524
|
return;
|
|
525
|
+
const padded = (newBody.startsWith('\n') ? '' : '\n') +
|
|
526
|
+
newBody +
|
|
527
|
+
(newBody.endsWith('\n') ? '' : '\n');
|
|
512
528
|
for (const opt of createFunctionStmt.options) {
|
|
513
529
|
if (opt?.DefElem?.defname === 'as') {
|
|
514
530
|
const arg = opt.DefElem.arg;
|
|
515
531
|
if (arg?.List?.items?.[0]?.String) {
|
|
516
|
-
arg.List.items[0].String.sval =
|
|
532
|
+
arg.List.items[0].String.sval = padded;
|
|
517
533
|
return;
|
|
518
534
|
}
|
|
519
535
|
if (arg?.String) {
|
|
520
|
-
arg.String.sval =
|
|
536
|
+
arg.String.sval = padded;
|
|
521
537
|
return;
|
|
522
538
|
}
|
|
523
539
|
}
|
package/esm/deparse.js
CHANGED
|
@@ -148,10 +148,21 @@ function reinjectBodyComments(originalBody, deparsedBody, comments, plpgsqlAst)
|
|
|
148
148
|
const stmtKeywords = buildStmtKeywords(plpgsqlAst);
|
|
149
149
|
let stmtKeyIdx = 0;
|
|
150
150
|
const usedAnchors = new Set();
|
|
151
|
+
// Track DECLARE sections: lines between DECLARE and the following BEGIN
|
|
152
|
+
// are variable declarations, not statements, and must not consume
|
|
153
|
+
// statement keyword matches (e.g. a declaration `v_x int;` would
|
|
154
|
+
// otherwise match an assignment to `v_x`).
|
|
155
|
+
let inDeclare = false;
|
|
151
156
|
for (const line of depLines) {
|
|
152
157
|
const trimmed = line.trim().toUpperCase();
|
|
158
|
+
if (trimmed === 'DECLARE') {
|
|
159
|
+
inDeclare = true;
|
|
160
|
+
}
|
|
161
|
+
else if (inDeclare && trimmed.startsWith('BEGIN')) {
|
|
162
|
+
inDeclare = false;
|
|
163
|
+
}
|
|
153
164
|
// Try to match this line to the next expected statement
|
|
154
|
-
if (stmtKeyIdx < stmtKeywords.length) {
|
|
165
|
+
if (!inDeclare && stmtKeyIdx < stmtKeywords.length) {
|
|
155
166
|
const { lineno, keywords } = stmtKeywords[stmtKeyIdx];
|
|
156
167
|
if (lineMatchesKeywords(trimmed, keywords)) {
|
|
157
168
|
// Insert comments anchored to this statement
|
|
@@ -501,19 +512,24 @@ function findLastEndLine(lines) {
|
|
|
501
512
|
}
|
|
502
513
|
/**
|
|
503
514
|
* Replace the function body in a CREATE FUNCTION AST node.
|
|
515
|
+
* The body is padded with newlines so it renders as
|
|
516
|
+
* `AS $$\n<body>\n$$` rather than gluing onto the dollar quotes.
|
|
504
517
|
*/
|
|
505
518
|
function stitchBodyIntoAst(createFunctionStmt, newBody) {
|
|
506
519
|
if (!createFunctionStmt?.options)
|
|
507
520
|
return;
|
|
521
|
+
const padded = (newBody.startsWith('\n') ? '' : '\n') +
|
|
522
|
+
newBody +
|
|
523
|
+
(newBody.endsWith('\n') ? '' : '\n');
|
|
508
524
|
for (const opt of createFunctionStmt.options) {
|
|
509
525
|
if (opt?.DefElem?.defname === 'as') {
|
|
510
526
|
const arg = opt.DefElem.arg;
|
|
511
527
|
if (arg?.List?.items?.[0]?.String) {
|
|
512
|
-
arg.List.items[0].String.sval =
|
|
528
|
+
arg.List.items[0].String.sval = padded;
|
|
513
529
|
return;
|
|
514
530
|
}
|
|
515
531
|
if (arg?.String) {
|
|
516
|
-
arg.String.sval =
|
|
532
|
+
arg.String.sval = padded;
|
|
517
533
|
return;
|
|
518
534
|
}
|
|
519
535
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "plpgsql-parse",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"author": "Constructive <developers@constructive.io>",
|
|
5
5
|
"description": "Comment and whitespace preserving PL/pgSQL parser",
|
|
6
6
|
"main": "index.js",
|
|
@@ -41,14 +41,14 @@
|
|
|
41
41
|
"round-trip"
|
|
42
42
|
],
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@libpg-query/parser": "^17.
|
|
44
|
+
"@libpg-query/parser": "^17.8.0",
|
|
45
45
|
"@pgsql/types": "^17.6.2",
|
|
46
46
|
"pgsql-deparser": "17.18.5",
|
|
47
|
-
"pgsql-parse": "0.
|
|
48
|
-
"plpgsql-deparser": "0.
|
|
47
|
+
"pgsql-parse": "0.3.0",
|
|
48
|
+
"plpgsql-deparser": "0.8.0"
|
|
49
49
|
},
|
|
50
50
|
"devDependencies": {
|
|
51
51
|
"makage": "^0.1.8"
|
|
52
52
|
},
|
|
53
|
-
"gitHead": "
|
|
53
|
+
"gitHead": "60fb86690a2d8e9bf90e2626edf242673e283980"
|
|
54
54
|
}
|