|
| 1 | +import * as fs from 'fs'; |
| 2 | +import * as path from 'path'; |
| 3 | +import { |
| 4 | + parseSync, |
| 5 | + deparseEnhanced, |
| 6 | + isRawComment, |
| 7 | + isRawWhitespace, |
| 8 | + isRawStmt, |
| 9 | + loadModule, |
| 10 | +} from '../src'; |
| 11 | + |
| 12 | +const FIXTURES_DIR = path.join(__dirname, 'fixtures'); |
| 13 | + |
| 14 | +const fixtures = fs.readdirSync(FIXTURES_DIR) |
| 15 | + .filter(f => f.endsWith('.sql')) |
| 16 | + .sort() |
| 17 | + .map(f => ({ |
| 18 | + name: f.replace('.sql', ''), |
| 19 | + sql: fs.readFileSync(path.join(FIXTURES_DIR, f), 'utf-8'), |
| 20 | + })); |
| 21 | + |
| 22 | +beforeAll(async () => { |
| 23 | + await loadModule(); |
| 24 | +}); |
| 25 | + |
| 26 | +describe('fixture round-trip (CST)', () => { |
| 27 | + for (const { name, sql } of fixtures) { |
| 28 | + describe(name, () => { |
| 29 | + it('parse→deparse→parse→deparse is idempotent', () => { |
| 30 | + // First round trip |
| 31 | + const result1 = parseSync(sql); |
| 32 | + const output1 = deparseEnhanced(result1); |
| 33 | + |
| 34 | + // Second round trip |
| 35 | + const result2 = parseSync(output1); |
| 36 | + const output2 = deparseEnhanced(result2); |
| 37 | + |
| 38 | + // The two deparses must produce identical output |
| 39 | + expect(output2).toBe(output1); |
| 40 | + }); |
| 41 | + |
| 42 | + it('preserves all -- comments from the original', () => { |
| 43 | + const result = parseSync(sql); |
| 44 | + const output = deparseEnhanced(result); |
| 45 | + |
| 46 | + // Extract expected comments: lines starting with -- that are NOT |
| 47 | + // inside dollar-quoted blocks |
| 48 | + const expectedComments = extractTopLevelComments(sql); |
| 49 | + |
| 50 | + for (const comment of expectedComments) { |
| 51 | + expect(output).toContain(comment); |
| 52 | + } |
| 53 | + }); |
| 54 | + |
| 55 | + it('preserves all SQL statements from the original', () => { |
| 56 | + const result = parseSync(sql); |
| 57 | + const stmts = result.stmts.filter(isRawStmt); |
| 58 | + |
| 59 | + // Should have at least one real statement |
| 60 | + expect(stmts.length).toBeGreaterThan(0); |
| 61 | + |
| 62 | + // Deparse should produce valid SQL for each statement |
| 63 | + const output = deparseEnhanced(result); |
| 64 | + expect(output.length).toBeGreaterThan(0); |
| 65 | + }); |
| 66 | + |
| 67 | + it('CST node ordering matches source order', () => { |
| 68 | + const result = parseSync(sql); |
| 69 | + const types = result.stmts.map(s => { |
| 70 | + if (isRawComment(s)) return 'comment'; |
| 71 | + if (isRawWhitespace(s)) return 'whitespace'; |
| 72 | + if (isRawStmt(s)) return 'stmt'; |
| 73 | + return 'unknown'; |
| 74 | + }); |
| 75 | + |
| 76 | + // No unknown node types |
| 77 | + expect(types).not.toContain('unknown'); |
| 78 | + |
| 79 | + // Should have at least one statement |
| 80 | + expect(types).toContain('stmt'); |
| 81 | + }); |
| 82 | + }); |
| 83 | + } |
| 84 | +}); |
| 85 | + |
| 86 | +/** |
| 87 | + * Extract top-level -- comments from SQL source, skipping any |
| 88 | + * that appear inside dollar-quoted strings. |
| 89 | + */ |
| 90 | +function extractTopLevelComments(sql: string): string[] { |
| 91 | + const comments: string[] = []; |
| 92 | + let inDollarQuote = false; |
| 93 | + let dollarTag = ''; |
| 94 | + |
| 95 | + const lines = sql.split('\n'); |
| 96 | + for (const line of lines) { |
| 97 | + const trimmed = line.trim(); |
| 98 | + |
| 99 | + // Check for dollar-quote boundaries |
| 100 | + const dollarMatch = trimmed.match(/\$([a-zA-Z_]*)\$/); |
| 101 | + if (dollarMatch) { |
| 102 | + const tag = dollarMatch[0]; |
| 103 | + if (!inDollarQuote) { |
| 104 | + // Check if this line also closes the dollar quote |
| 105 | + const firstIdx = trimmed.indexOf(tag); |
| 106 | + const secondIdx = trimmed.indexOf(tag, firstIdx + tag.length); |
| 107 | + if (secondIdx === -1) { |
| 108 | + inDollarQuote = true; |
| 109 | + dollarTag = tag; |
| 110 | + } |
| 111 | + // If both open and close on same line, not entering a block |
| 112 | + } else if (tag === dollarTag) { |
| 113 | + inDollarQuote = false; |
| 114 | + dollarTag = ''; |
| 115 | + } |
| 116 | + continue; |
| 117 | + } |
| 118 | + |
| 119 | + if (!inDollarQuote && trimmed.startsWith('--')) { |
| 120 | + comments.push(trimmed); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + return comments; |
| 125 | +} |
0 commit comments