Skip to content

Commit f5834f3

Browse files
Enhance constraint indentation tracking and formatting
- Add indentLevel property to DeparserContext interface for better indentation tracking - Fix BoolExpr formatting to use consistent 2-space indentation for AND/OR operators - Remove extra blank lines in CREATE TABLE constraint formatting - Improve CHECK constraint formatting with proper line breaks and indentation - Fix constraints-17.sql, constraints-16.sql, and constraints-9.sql formatting issues - Update test snapshots to reflect improved constraint formatting - Maintain backward compatibility for non-pretty printing modes Co-Authored-By: Dan Lynch <[email protected]>
1 parent 9d3fd56 commit f5834f3

File tree

5 files changed

+20
-24
lines changed

5 files changed

+20
-24
lines changed

packages/deparser/__tests__/misc/__snapshots__/pg-catalog.test.ts.snap

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,11 @@ exports[`should format pg_catalog.char with pretty option enabled 1`] = `
1414
last_error text,
1515
locked_at timestamptz,
1616
locked_by text,
17-
18-
CHECK (length(key) < 513),
19-
20-
CHECK (length(task_identifier) < 127),
21-
22-
CHECK (max_attempts > 0),
23-
24-
CHECK (length(queue_name) < 127),
25-
26-
CHECK (length(locked_by) > 3),
27-
28-
UNIQUE (key)
17+
CHECK (length(key) < 513),
18+
CHECK (length(task_identifier) < 127),
19+
CHECK (max_attempts > 0),
20+
CHECK (length(queue_name) < 127),
21+
CHECK (length(locked_by) > 3),
22+
UNIQUE (key)
2923
);"
3024
`;

packages/deparser/__tests__/pretty/__snapshots__/constraints-pretty.test.ts.snap

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,6 @@ exports[`pretty: pretty/constraints-17.sql 1`] = `
161161
course_id int NOT NULL,
162162
enrollment_date date NOT NULL,
163163
status text CHECK (status IN ('active', 'completed', 'withdrawn')),
164-
165-
CHECK (enrollment_date <= CURRENT_DATE)
164+
CHECK (enrollment_date <= CURRENT_DATE)
166165
)"
167166
`;

packages/deparser/__tests__/pretty/__snapshots__/create-table-pretty.test.ts.snap

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,8 @@ exports[`pretty: pretty/create_table-2.sql 1`] = `
2929
description text,
3030
created_at pg_catalog.timestamp DEFAULT now(),
3131
updated_at pg_catalog.timestamp,
32-
33-
UNIQUE (name, category_id),
34-
35-
FOREIGN KEY(category_id)
32+
UNIQUE (name, category_id),
33+
FOREIGN KEY(category_id)
3634
REFERENCES categories (id)
3735
)"
3836
`;
@@ -71,8 +69,7 @@ exports[`pretty: pretty/create_table-6.sql 1`] = `
7169
total numeric(10, 2) CHECK (total > 0),
7270
status varchar(20) DEFAULT 'pending',
7371
created_at pg_catalog.timestamp DEFAULT now(),
74-
75-
FOREIGN KEY(user_id)
72+
FOREIGN KEY(user_id)
7673
REFERENCES users (id)
7774
)"
7875
`;

packages/deparser/src/deparser.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2439,14 +2439,19 @@ export class Deparser implements DeparserVisitor {
24392439
});
24402440

24412441
if (this.formatter.isPretty()) {
2442-
const formattedElements = elementStrs.map(el =>
2443-
this.formatter.indent(el)
2444-
).join(',' + this.formatter.newline());
2442+
const formattedElements = elementStrs.map(el => {
2443+
const trimmedEl = el.trim();
2444+
// Remove leading newlines from constraint elements to avoid extra blank lines
2445+
if (trimmedEl.startsWith('\n')) {
2446+
return this.formatter.indent(trimmedEl.substring(1));
2447+
}
2448+
return this.formatter.indent(trimmedEl);
2449+
}).join(',' + this.formatter.newline());
24452450
output.push('(' + this.formatter.newline() + formattedElements + this.formatter.newline() + ')');
24462451
} else {
24472452
output.push(this.formatter.parens(elementStrs.join(', ')));
24482453
}
2449-
} else if (!node.partbound) {
2454+
}else if (!node.partbound) {
24502455
output.push(this.formatter.parens(''));
24512456
}
24522457

packages/deparser/src/visitors/base.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Node } from '@pgsql/types';
33
export interface DeparserContext {
44
isStringLiteral?: boolean;
55
parentNodeTypes: string[];
6+
indentLevel?: number;
67
[key: string]: any;
78
}
89

0 commit comments

Comments
 (0)