diff --git a/packages/deparser/README.md b/packages/deparser/README.md index 91cbeb08..820c768f 100644 --- a/packages/deparser/README.md +++ b/packages/deparser/README.md @@ -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) @@ -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 | diff --git a/packages/deparser/__tests__/entry-point.test.ts b/packages/deparser/__tests__/entry-point.test.ts index c8b11496..3ed8ce71 100644 --- a/packages/deparser/__tests__/entry-point.test.ts +++ b/packages/deparser/__tests__/entry-point.test.ts @@ -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) { @@ -42,7 +42,7 @@ 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'); }); @@ -50,7 +50,7 @@ describe('Entry Point Refactoring', () => { 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(/;$/); } }); @@ -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\')'); }); @@ -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'); }); }); @@ -123,4 +123,4 @@ describe('Entry Point Refactoring', () => { expect(deparser['tree'][0]).toBe(wrapped); }); }); -}); \ No newline at end of file +}); diff --git a/packages/deparser/src/deparser.ts b/packages/deparser/src/deparser.ts index ec63da90..16d3e884 100644 --- a/packages/deparser/src/deparser.ts +++ b/packages/deparser/src/deparser.ts @@ -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 diff --git a/packages/deparser/src/utils/sql-formatter.ts b/packages/deparser/src/utils/sql-formatter.ts index 0f56504f..a3daee53 100644 --- a/packages/deparser/src/utils/sql-formatter.ts +++ b/packages/deparser/src/utils/sql-formatter.ts @@ -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; diff --git a/packages/deparser/src/visitors/base.ts b/packages/deparser/src/visitors/base.ts index 2803a260..b18429ec 100644 --- a/packages/deparser/src/visitors/base.ts +++ b/packages/deparser/src/visitors/base.ts @@ -48,7 +48,7 @@ export class DeparserContext { constructor({ indentLevel = 0, - prettyMode = false, + prettyMode = true, isStringLiteral, parentNodeTypes = [], formatter, diff --git a/packages/utils/__test__/utils.test.ts b/packages/utils/__test__/utils.test.ts index e3be8e49..ff47941f 100644 --- a/packages/utils/__test__/utils.test.ts +++ b/packages/utils/__test__/utils.test.ts @@ -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', () => { @@ -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', () => { @@ -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', () => { @@ -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(); -}) \ No newline at end of file +})