From 10b268b2cfd39513d437662d4705275ea56af138 Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Wed, 1 May 2024 18:34:24 +0200 Subject: [PATCH] autoinsert trailing commas in embedded SQL blocks --- CHANGELOG.md | 2 + packages/cli/jest.config.ts | 2 +- .../src/__snapshots__/rescript.test.ts.snap | 99 +++++++++++++++++++ packages/cli/src/parseRescript.ts | 11 ++- packages/cli/src/rescript.test.ts | 30 ++++++ 5 files changed, 138 insertions(+), 6 deletions(-) create mode 100644 packages/cli/src/__snapshots__/rescript.test.ts.snap create mode 100644 packages/cli/src/rescript.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f6422ab..abfd2d02 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/packages/cli/jest.config.ts b/packages/cli/jest.config.ts index 3e80045a..22318f27 100644 --- a/packages/cli/jest.config.ts +++ b/packages/cli/jest.config.ts @@ -18,7 +18,7 @@ const config: Config = { ], }, preset: 'ts-jest/presets/default-esm', - testRegex: '\\.test_disabled\\.tsx?$', + testRegex: 'rescript\\.test\\.tsx?$', }; export default config; diff --git a/packages/cli/src/__snapshots__/rescript.test.ts.snap b/packages/cli/src/__snapshots__/rescript.test.ts.snap new file mode 100644 index 00000000..755c8693 --- /dev/null +++ b/packages/cli/src/__snapshots__/rescript.test.ts.snap @@ -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, + }, + }, + ], +} +`; diff --git a/packages/cli/src/parseRescript.ts b/packages/cli/src/parseRescript.ts index fe731387..528356b9 100644 --- a/packages/cli/src/parseRescript.ts +++ b/packages/cli/src/parseRescript.ts @@ -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: [], @@ -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'); diff --git a/packages/cli/src/rescript.test.ts b/packages/cli/src/rescript.test.ts new file mode 100644 index 00000000..3ac755be --- /dev/null +++ b/packages/cli/src/rescript.test.ts @@ -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(); +});