Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/deparser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ The deparser accepts optional configuration for formatting and output control:
import { deparseSync as deparse } from 'pgsql-deparser';

const options = {
pretty: true, // Enable pretty formatting (default: false)
pretty: true, // Enable pretty formatting (default: true)
newline: '\n', // Newline character (default: '\n')
tab: ' ', // Tab/indentation character (default: ' ')
semicolons: true // Add semicolons to statements (default: true)
Expand All @@ -110,7 +110,7 @@ const sql = deparse(ast, options);

| Option | Type | Default | Description |
|--------|------|---------|-------------|
| `pretty` | `boolean` | `false` | Enable pretty formatting with indentation and line breaks |
| `pretty` | `boolean` | `true` | Enable pretty formatting with indentation and line breaks |
| `newline` | `string` | `'\n'` | Character(s) used for line breaks |
| `tab` | `string` | `' '` | Character(s) used for indentation |
| `semicolons` | `boolean` | `true` | Add semicolons to SQL statements |
Expand Down
18 changes: 9 additions & 9 deletions packages/deparser/__tests__/entry-point.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ describe('Entry Point Refactoring', () => {

describe('ParseResult handling', () => {
it('should handle bare ParseResult (duck-typed)', () => {
const result = Deparser.deparse(parseResult);
const result = Deparser.deparse(parseResult, { pretty: false });
expect(result).toContain('SELECT * FROM users WHERE id = 1');
expect(result).toContain('INSERT INTO logs (message) VALUES (\'test\')');
});

it('should handle wrapped ParseResult', () => {
const wrappedParseResult = { ParseResult: parseResult } as t.Node;
const result = Deparser.deparse(wrappedParseResult);
const result = Deparser.deparse(wrappedParseResult, { pretty: false });
expect(result).toContain('SELECT * FROM users WHERE id = 1');
expect(result).toContain('INSERT INTO logs (message) VALUES (\'test\')');
});

it('should preserve semicolons based on stmt_len', () => {
const result = Deparser.deparse(parseResult);
const result = Deparser.deparse(parseResult, { pretty: false });
// The first statement should have a semicolon if stmt_len is set
const lines = result.split('\n').filter(line => line.trim());
if (parseResult.stmts?.[0]?.stmt_len) {
Expand All @@ -42,15 +42,15 @@ describe('Entry Point Refactoring', () => {
it('should handle wrapped RawStmt', () => {
const rawStmt = parseResult.stmts![0];
const wrappedRawStmt = { RawStmt: rawStmt } as t.Node;
const result = Deparser.deparse(wrappedRawStmt);
const result = Deparser.deparse(wrappedRawStmt, { pretty: false });
expect(result).toContain('SELECT * FROM users WHERE id = 1');
});

it('should add semicolon when stmt_len is present', () => {
const rawStmt = parseResult.stmts![0];
if (rawStmt.stmt_len) {
const wrappedRawStmt = { RawStmt: rawStmt } as t.Node;
const result = Deparser.deparse(wrappedRawStmt);
const result = Deparser.deparse(wrappedRawStmt, { pretty: false });
expect(result).toMatch(/;$/);
}
});
Expand All @@ -59,14 +59,14 @@ describe('Entry Point Refactoring', () => {
describe('Array handling', () => {
it('should handle array of statements', () => {
const statements = parseResult.stmts!.map(rawStmt => rawStmt.stmt!);
const result = Deparser.deparse(statements);
const result = Deparser.deparse(statements, { pretty: false });
expect(result).toContain('SELECT * FROM users WHERE id = 1');
expect(result).toContain('INSERT INTO logs (message) VALUES (\'test\')');
});

it('should handle array of wrapped nodes', () => {
const wrappedNodes = parseResult.stmts!.map(rawStmt => ({ RawStmt: rawStmt } as t.Node));
const result = Deparser.deparse(wrappedNodes);
const result = Deparser.deparse(wrappedNodes, { pretty: false });
expect(result).toContain('SELECT * FROM users WHERE id = 1');
expect(result).toContain('INSERT INTO logs (message) VALUES (\'test\')');
});
Expand All @@ -75,7 +75,7 @@ describe('Entry Point Refactoring', () => {
describe('Single node handling', () => {
it('should handle single statement', () => {
const stmt = parseResult.stmts![0].stmt!;
const result = Deparser.deparse(stmt);
const result = Deparser.deparse(stmt, { pretty: false });
expect(result).toContain('SELECT * FROM users WHERE id = 1');
});
});
Expand Down Expand Up @@ -123,4 +123,4 @@ describe('Entry Point Refactoring', () => {
expect(deparser['tree'][0]).toBe(wrapped);
});
});
});
});
2 changes: 1 addition & 1 deletion packages/deparser/src/deparser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export interface DeparserOptions {
functionDelimiter?: string; // Default: '$$'
// Alternative delimiter when the default is found in the body
functionDelimiterFallback?: string; // Default: '$EOFCODE$'
pretty?: boolean; // Default: false
pretty?: boolean; // Default: true
}

// Type guards for better type safety
Expand Down
2 changes: 1 addition & 1 deletion packages/deparser/src/utils/sql-formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export class SqlFormatter {
private tabChar: string;
private prettyMode: boolean;

constructor(newlineChar: string = '\n', tabChar: string = ' ', prettyMode: boolean = false) {
constructor(newlineChar: string = '\n', tabChar: string = ' ', prettyMode: boolean = true) {
this.newlineChar = newlineChar;
this.tabChar = tabChar;
this.prettyMode = prettyMode;
Expand Down
2 changes: 1 addition & 1 deletion packages/deparser/src/visitors/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class DeparserContext {

constructor({
indentLevel = 0,
prettyMode = false,
prettyMode = true,
isStringLiteral,
parentNodeTypes = [],
formatter,
Expand Down
10 changes: 5 additions & 5 deletions packages/utils/__test__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ it('simple SelectStmt', () => {
(stmt.SelectStmt.fromClause[0] as {RangeVar: RangeVar}).RangeVar.relname = 'another_table';

expect(stmt).toMatchSnapshot();
expect(deparse(stmt)).toMatchSnapshot();
expect(deparse(stmt, { pretty: false })).toMatchSnapshot();
});

it('SelectStmt with WHERE clause', () => {
Expand Down Expand Up @@ -68,7 +68,7 @@ it('SelectStmt with WHERE clause', () => {
op: 'SETOP_NONE'
});

expect(deparse(selectStmt, {})).toEqual(`SELECT * FROM myschema.mytable WHERE a = CAST('t' AS boolean)`);
expect(deparse(selectStmt, { pretty: false })).toEqual(`SELECT * FROM myschema.mytable WHERE a = CAST('t' AS boolean)`);
});

it('queries', () => {
Expand Down Expand Up @@ -106,7 +106,7 @@ it('queries', () => {
op: 'SETOP_NONE'
});

expect(deparse(query, {})).toEqual(`SELECT name, email FROM users WHERE age > 18`);
expect(deparse(query, { pretty: false })).toEqual(`SELECT name, email FROM users WHERE age > 18`);

});
it('dynamic creation of tables', () => {
Expand Down Expand Up @@ -142,6 +142,6 @@ it('dynamic creation of tables', () => {
});

// `deparse` function converts AST to SQL string
const sql = deparse(createStmt);
const sql = deparse(createStmt, { pretty: false });
expect(sql).toMatchSnapshot();
})
})