Skip to content

Commit 6dde6fd

Browse files
committed
ast-test
1 parent 29d7d85 commit 6dde6fd

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

packages/transform/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
"lint": "eslint . --fix",
3030
"test": "jest",
3131
"test:watch": "jest --watch",
32-
"kitchen-sink": "ts-node scripts/make-kitchen-sink.ts"
32+
"kitchen-sink": "ts-node scripts/make-kitchen-sink.ts",
33+
"test:ast": "ts-node scripts/test-ast.ts"
3334
},
3435
"devDependencies": {
3536
"@pgsql/parser": "^1.0.2",
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
input.sql
2+
output*.json
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { v13, v14, v15, v16, v17 } from '@pgsql/parser';
2+
import { readFileSync, writeFileSync } from 'fs';
3+
import { join } from 'path';
4+
import { cleanTree } from '../test-utils/clean-tree';
5+
6+
async function testAst() {
7+
try {
8+
// Read SQL from input.sql in the same directory
9+
const inputPath = join(__dirname, 'input.sql');
10+
const sql = readFileSync(inputPath, 'utf8');
11+
12+
// Parse the SQL
13+
const astv17 = await v17.parse(sql);
14+
const astv16 = await v16.parse(sql);
15+
const astv15 = await v15.parse(sql);
16+
const astv14 = await v14.parse(sql);
17+
const astv13 = await v13.parse(sql);
18+
19+
const cleanedAstv17 = cleanTree(astv17);
20+
const cleanedAstv16 = cleanTree(astv16);
21+
const cleanedAstv15 = cleanTree(astv15);
22+
const cleanedAstv14 = cleanTree(astv14);
23+
const cleanedAstv13 = cleanTree(astv13);
24+
25+
// Write JSON to output.json in the same directory
26+
const outputPathv13 = join(__dirname, 'output-v13.json');
27+
const outputPathv14 = join(__dirname, 'output-v14.json');
28+
const outputPathv15 = join(__dirname, 'output-v15.json');
29+
const outputPathv16 = join(__dirname, 'output-v16.json');
30+
const outputPathv17 = join(__dirname, 'output-v17.json');
31+
32+
writeFileSync(outputPathv13, JSON.stringify(cleanedAstv13, null, 2));
33+
writeFileSync(outputPathv14, JSON.stringify(cleanedAstv14, null, 2));
34+
writeFileSync(outputPathv15, JSON.stringify(cleanedAstv15, null, 2));
35+
writeFileSync(outputPathv16, JSON.stringify(cleanedAstv16, null, 2));
36+
writeFileSync(outputPathv17, JSON.stringify(cleanedAstv17, null, 2));
37+
38+
console.log(`Successfully parsed SQL from ${inputPath} and wrote AST`);
39+
} catch (error) {
40+
console.error('Error processing SQL:', error);
41+
}
42+
}
43+
44+
testAst();

0 commit comments

Comments
 (0)