Skip to content

Commit

Permalink
autoinsert trailing commas in embedded SQL blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
zth committed May 1, 2024
1 parent 4539527 commit 10b268b
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# main

- Autoinsert trailing commas in embedded SQL blocks.

# 2.4.0

- Add mode for embedding `%sql` (with `one`, `expectOne`, `many`, and `execute` flavors) in ReScript directly.
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const config: Config = {
],
},
preset: 'ts-jest/presets/default-esm',
testRegex: '\\.test_disabled\\.tsx?$',
testRegex: 'rescript\\.test\\.tsx?$',
};

export default config;
99 changes: 99 additions & 0 deletions packages/cli/src/__snapshots__/rescript.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`inserts trailing semicolon when needed 1`] = `
Object {
"events": Array [],
"queries": Array [
Object {
"name": "FindBookById",
"params": Array [
Object {
"codeRefs": Object {
"used": Array [
Object {
"a": 61,
"b": 62,
"col": 36,
"line": 2,
},
],
},
"name": "id",
"required": false,
"transform": Object {
"type": "scalar",
},
},
],
"statement": Object {
"body": "SELECT * FROM books WHERE id = :id",
"loc": Object {
"a": 29,
"b": 62,
"col": 4,
"line": 2,
},
},
"usedParamSet": Object {
"id": true,
},
},
Object {
"name": "FindBooks",
"params": Array [],
"statement": Object {
"body": "SELECT * FROM books",
"loc": Object {
"a": 92,
"b": 110,
"col": 4,
"line": 5,
},
},
"usedParamSet": Object {},
},
],
}
`;

exports[`parser finds string template in correct file 1`] = `
Object {
"events": Array [],
"queries": Array [
Object {
"name": "FindBookById",
"params": Array [
Object {
"codeRefs": Object {
"used": Array [
Object {
"a": 61,
"b": 62,
"col": 36,
"line": 2,
},
],
},
"name": "id",
"required": false,
"transform": Object {
"type": "scalar",
},
},
],
"statement": Object {
"body": "SELECT * FROM books WHERE id = :id",
"loc": Object {
"a": 29,
"b": 62,
"col": 4,
"line": 2,
},
},
"usedParamSet": Object {
"id": true,
},
},
],
}
`;
11 changes: 6 additions & 5 deletions packages/cli/src/parseRescript.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { parseSQLFile } from '@pgtyped/parser';
import { SQLParseResult } from '@pgtyped/parser/lib/loader/sql';

export function parseCode(
fileContent: string,
_fileName: string,
): SQLParseResult {
export function parseCode(fileContent: string): SQLParseResult {
if (!fileContent.includes('%sql')) {
return {
queries: [],
Expand All @@ -18,7 +15,11 @@ export function parseCode(
const queries = [];

while ((match = regex.exec(fileContent)) !== null) {
queries.push(match[1]);
let query = match[1].trim();
if (!query.endsWith(';')) {
query += ';';
}
queries.push(query);
}

const asSql = queries.join('\n\n');
Expand Down
30 changes: 30 additions & 0 deletions packages/cli/src/rescript.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { parseCode } from './parseRescript.js';

test('parser finds string template in correct file', () => {
const fileContent = `
let query = %sql.one(\`
/* @name FindBookById */
SELECT * FROM books WHERE id = :id;
\`);
`;

const result = parseCode(fileContent);
expect(result).toMatchSnapshot();
});

test('inserts trailing semicolon when needed', () => {
const fileContent = `
let query = %sql.one(\`
/* @name FindBookById */
SELECT * FROM books WHERE id = :id
\`);
let queryMany = %sql.many(\`
/* @name FindBooks */
SELECT * FROM books
\`);
`;

const result = parseCode(fileContent);
expect(result).toMatchSnapshot();
});

0 comments on commit 10b268b

Please sign in to comment.