From e3440c9d294c5523bc71710b27319d438554d76f Mon Sep 17 00:00:00 2001 From: Arno Dirlam Date: Sat, 31 Oct 2026 18:13:53 +0100 Subject: [PATCH 1/3] Embedding: Preserve embedded quote styles --- README.md | 4 ++++ src/embedSql.ts | 10 +--------- src/formatBigqueryString.ts | 20 ++++---------------- test/embed/execute.test.ts | 28 ++++++++++++++++++++++------ test/embed/language_js.test.ts | 16 ++++++++++++---- test/embed/language_plpgsql.test.ts | 20 +++++--------------- test/embed/language_sql.test.ts | 21 ++++++--------------- 7 files changed, 54 insertions(+), 65 deletions(-) diff --git a/README.md b/README.md index 1ccba59..61e0b95 100644 --- a/README.md +++ b/README.md @@ -259,6 +259,10 @@ Use [prettier-plugin-embed][] together with `prettier-plugin-sql-cst`. ## Limitations and development status +SQL and PL/pgSQL bodies in PostgreSQL must use dollar quotes to be formatted. +SQL and JavaScript bodies in BigQuery must use raw triple quotes. +Other quote styles are preserved without formatting their contents. + Currently this plugin supports two SQL dialects: - **SQLite** - full support. diff --git a/src/embedSql.ts b/src/embedSql.ts index e261574..37a12d0 100644 --- a/src/embedSql.ts +++ b/src/embedSql.ts @@ -142,13 +142,5 @@ const isRoutine = ( isCreateFunctionStmt(node) || isCreateProcedureStmt(node); const detectQuote = (node: StringLiteral): string | undefined => { - const match = node.text.match(/^('|\$[^$]*\$)/); - const quote = match?.[1]; - if (quote === "'") { - // Convert `'` quotes to `$$`. - // But bail out if the code already contains $$. - return node.value.includes("$$") ? undefined : "$$"; - } else { - return quote; - } + return node.text.match(/^(\$[^$]*\$)/)?.[1]; }; diff --git a/src/formatBigqueryString.ts b/src/formatBigqueryString.ts index fcf79fc..61f9b27 100644 --- a/src/formatBigqueryString.ts +++ b/src/formatBigqueryString.ts @@ -10,29 +10,17 @@ export const formatBigqueryString = ( return async ( textToDoc: (text: string, options: Options) => Promise, ) => { - const quotes = detectQuotes(node.value); - if (!quotes) { + const match = node.text.match(/^([rR])('''|""")/); + if (!match) { return undefined; } const code = await textToDoc(node.value, options); return [ - quotes[0], + match[0], indent([hardline, stripTrailingHardline(code)]), hardline, - quotes[1], + match[2], ]; }; }; - -// Whether to quote the code with single- or double-quotes. -// Returns undefined when neither can be used without escaping. -const detectQuotes = (code: string): [string, string] | undefined => { - if (!/'''/.test(code)) { - return ["r'''", "'''"]; - } - if (!/"""/.test(code)) { - return ['r"""', '"""']; - } - return undefined; -}; diff --git a/test/embed/execute.test.ts b/test/embed/execute.test.ts index 7df46ed..f97b59d 100644 --- a/test/embed/execute.test.ts +++ b/test/embed/execute.test.ts @@ -96,11 +96,24 @@ describe("PL/pgSQL EXECUTE embedding", () => { }); describe("BigQuery EXECUTE IMMEDIATE embedding", () => { + it("does not introduce conflicting triple quotes through JSON embedding", async () => { + const result = await pretty( + `EXECUTE IMMEDIATE r'''select JSON '{"message":"a long message that will force this JSON document to wrap","enabled":true}' '''`, + { dialect: "bigquery", printWidth: 40 }, + ); + expect(result).toContain( + `JSON '{"message":"a long message that will force this JSON document to wrap","enabled":true}'`, + ); + await testBigquery(result, { printWidth: 40 }); + }); it("formats a command with INTO and named parameters", async () => { expect( - await pretty('EXECUTE IMMEDIATE "select @id" INTO result USING 1 AS id', { - dialect: "bigquery", - }), + await pretty( + "EXECUTE IMMEDIATE r'''select @id''' INTO result USING 1 AS id", + { + dialect: "bigquery", + }, + ), ).toBe(dedent` EXECUTE IMMEDIATE r''' @@ -113,7 +126,7 @@ describe("BigQuery EXECUTE IMMEDIATE embedding", () => { it("preserves backslashes when quoting the formatted command", async () => { expect( - await pretty(String.raw`EXECUTE IMMEDIATE r"select '\n'"`, { + await pretty(String.raw`EXECUTE IMMEDIATE r'''select '\n' '''`, { dialect: "bigquery", }), ).toBe( @@ -126,9 +139,9 @@ describe("BigQuery EXECUTE IMMEDIATE embedding", () => { ); }); - it("uses double quotes when the command contains triple single quotes", async () => { + it("preserves triple double quotes", async () => { expect( - await pretty(`EXECUTE IMMEDIATE ${JSON.stringify(`select "'''"`)}`, { + await pretty(`EXECUTE IMMEDIATE r"""select "'''" """`, { dialect: "bigquery", }), ).toBe(dedent` @@ -146,6 +159,9 @@ describe("BigQuery EXECUTE IMMEDIATE embedding", () => { }); it.each([ + 'EXECUTE IMMEDIATE "select @id"', + "EXECUTE IMMEDIATE r'select ?'", + "EXECUTE IMMEDIATE '''select 1'''", 'EXECUTE IMMEDIATE "select " || column_name', 'EXECUTE IMMEDIATE "unsupported SQL syntax"', ])("preserves commands it cannot embed: %s", async (source) => { diff --git a/test/embed/language_js.test.ts b/test/embed/language_js.test.ts index a467ba6..3f482c5 100644 --- a/test/embed/language_js.test.ts +++ b/test/embed/language_js.test.ts @@ -2,6 +2,14 @@ import dedent from "dedent-js"; import { pretty, testBigquery } from "../test_utils"; describe("LANGUAGE js", () => { + it.each(["'return 1'", '"return 1"', "'''return 1'''", "r'return 1'"])( + "preserves JavaScript in unsupported quote style: %s", + async (body) => { + await testBigquery( + `CREATE FUNCTION f()\nRETURNS INT64\nLANGUAGE js\nAS ${body}`, + ); + }, + ); it(`formats JavaScript FUNCTION`, async () => { await testBigquery(dedent` CREATE FUNCTION gen_random() @@ -21,7 +29,7 @@ describe("LANGUAGE js", () => { CREATE FUNCTION gen_random() RETURNS FLOAT64 LANGUAGE js - AS ' if(true) {return Math.random () *2}' + AS r''' if(true) {return Math.random () *2}''' `, { dialect: "bigquery" }, ), @@ -37,14 +45,14 @@ describe("LANGUAGE js", () => { `); }); - it(`quotes JavaScript in double-quotes when single-quotes can't be used`, async () => { + it(`preserves raw double quotes and uppercase prefix`, async () => { expect( await pretty( dedent` CREATE FUNCTION contains_quotes(x STRING) RETURNS FLOAT64 LANGUAGE js - AS " return /'''/.test(x) " + AS R""" return /'''/.test(x) """ `, { dialect: "bigquery" }, ), @@ -52,7 +60,7 @@ describe("LANGUAGE js", () => { CREATE FUNCTION contains_quotes(x STRING) RETURNS FLOAT64 LANGUAGE js - AS r""" + AS R""" return /'''/.test(x); """ `); diff --git a/test/embed/language_plpgsql.test.ts b/test/embed/language_plpgsql.test.ts index f7966b4..e2a99ce 100644 --- a/test/embed/language_plpgsql.test.ts +++ b/test/embed/language_plpgsql.test.ts @@ -30,25 +30,15 @@ describe("LANGUAGE plpgsql", () => { `); }); - it(`converts single-quoted string to dollar-quoted string`, async () => { - expect( - await pretty( - dedent` + it(`preserves single-quoted string`, async () => { + await testPostgresql( + dedent` ${code} LANGUAGE plpgsql AS 'BEGIN SELECT ''foo''; END' `, - { dialect: "postgresql", sqlExperimentalPlpgsql: true }, - ), - ).toBe(dedent` - ${code} - LANGUAGE plpgsql - AS $$ - BEGIN - SELECT 'foo'; - END; - $$ - `); + { sqlExperimentalPlpgsql: true }, + ); }); it(`does not reformat single-quoted SQL string when its source contains $$-quotes`, async () => { diff --git a/test/embed/language_sql.test.ts b/test/embed/language_sql.test.ts index ee0bce8..cd3d27f 100644 --- a/test/embed/language_sql.test.ts +++ b/test/embed/language_sql.test.ts @@ -30,23 +30,14 @@ describe("LANGUAGE sql", () => { `); }); - it(`converts single-quoted SQL string to dollar-quoted string`, async () => { - expect( - await pretty( - dedent` + it(`preserves single-quoted SQL string`, async () => { + await testPostgresql( + dedent` ${code} LANGUAGE sql AS 'SELECT ''foo''' `, - { dialect: "postgresql" }, - ), - ).toBe(dedent` - ${code} - LANGUAGE sql - AS $$ - SELECT 'foo'; - $$ - `); + ); }); it(`does not reformat single-quoted SQL string when its source contains $$-quotes`, async () => { @@ -75,7 +66,7 @@ describe("LANGUAGE sql", () => { dedent` ${code} LANGUAGE Sql - AS 'SELECT 1' + AS $$select 1$$ `, { dialect: "postgresql" }, ), @@ -94,7 +85,7 @@ describe("LANGUAGE sql", () => { expect( await pretty( dedent` - DO LANGUAGE sql 'select 1' + DO LANGUAGE sql $$select 1$$ `, { dialect: "postgresql", sqlExperimentalPlpgsql: true }, ), From f0d2510c1cc9b913a187f00bf96bf8d0f135fcc1 Mon Sep 17 00:00:00 2001 From: Arno Dirlam Date: Wed, 11 Nov 2026 14:28:59 +0100 Subject: [PATCH 2/3] Embedding: Simplify quote detection & tests --- README.md | 4 ---- src/embedSql.ts | 2 +- src/formatBigqueryString.ts | 13 +++++++++---- test/embed/execute.test.ts | 22 +++++++++++----------- test/embed/language_js.test.ts | 7 ++++++- test/embed/language_plpgsql.test.ts | 2 +- test/embed/language_sql.test.ts | 4 ++-- 7 files changed, 30 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 61e0b95..1ccba59 100644 --- a/README.md +++ b/README.md @@ -259,10 +259,6 @@ Use [prettier-plugin-embed][] together with `prettier-plugin-sql-cst`. ## Limitations and development status -SQL and PL/pgSQL bodies in PostgreSQL must use dollar quotes to be formatted. -SQL and JavaScript bodies in BigQuery must use raw triple quotes. -Other quote styles are preserved without formatting their contents. - Currently this plugin supports two SQL dialects: - **SQLite** - full support. diff --git a/src/embedSql.ts b/src/embedSql.ts index 37a12d0..f970a8c 100644 --- a/src/embedSql.ts +++ b/src/embedSql.ts @@ -142,5 +142,5 @@ const isRoutine = ( isCreateFunctionStmt(node) || isCreateProcedureStmt(node); const detectQuote = (node: StringLiteral): string | undefined => { - return node.text.match(/^(\$[^$]*\$)/)?.[1]; + return node.text.match(/^\$[^$]*\$/)?.[0]; }; diff --git a/src/formatBigqueryString.ts b/src/formatBigqueryString.ts index 61f9b27..7b2fd78 100644 --- a/src/formatBigqueryString.ts +++ b/src/formatBigqueryString.ts @@ -10,17 +10,22 @@ export const formatBigqueryString = ( return async ( textToDoc: (text: string, options: Options) => Promise, ) => { - const match = node.text.match(/^([rR])('''|""")/); - if (!match) { + const quotes = detectQuotes(node.text); + if (!quotes) { return undefined; } const code = await textToDoc(node.value, options); return [ - match[0], + quotes[0], indent([hardline, stripTrailingHardline(code)]), hardline, - match[2], + quotes[1], ]; }; }; + +const detectQuotes = (text: string): [string, string] | undefined => { + const match = text.match(/^[rR]('''|""")/); + return match ? [match[0], match[1]] : undefined; +}; diff --git a/test/embed/execute.test.ts b/test/embed/execute.test.ts index f97b59d..cb36bdf 100644 --- a/test/embed/execute.test.ts +++ b/test/embed/execute.test.ts @@ -96,14 +96,18 @@ describe("PL/pgSQL EXECUTE embedding", () => { }); describe("BigQuery EXECUTE IMMEDIATE embedding", () => { - it("does not introduce conflicting triple quotes through JSON embedding", async () => { + it("does not embed wrapped JSON with conflicting triple quotes", async () => { const result = await pretty( `EXECUTE IMMEDIATE r'''select JSON '{"message":"a long message that will force this JSON document to wrap","enabled":true}' '''`, { dialect: "bigquery", printWidth: 40 }, ); - expect(result).toContain( - `JSON '{"message":"a long message that will force this JSON document to wrap","enabled":true}'`, - ); + // embedJson can use ''' for wrapped JSON, closing the outer raw string. + expect(result).toBe(dedent` + EXECUTE IMMEDIATE + r''' + SELECT JSON '{"message":"a long message that will force this JSON document to wrap","enabled":true}'; + ''' + `); await testBigquery(result, { printWidth: 40 }); }); it("formats a command with INTO and named parameters", async () => { @@ -140,14 +144,10 @@ describe("BigQuery EXECUTE IMMEDIATE embedding", () => { }); it("preserves triple double quotes", async () => { - expect( - await pretty(`EXECUTE IMMEDIATE r"""select "'''" """`, { - dialect: "bigquery", - }), - ).toBe(dedent` + await testBigquery(dedent` EXECUTE IMMEDIATE r""" - SELECT "'''"; + SELECT 'value'; """ `); }); @@ -163,7 +163,7 @@ describe("BigQuery EXECUTE IMMEDIATE embedding", () => { "EXECUTE IMMEDIATE r'select ?'", "EXECUTE IMMEDIATE '''select 1'''", 'EXECUTE IMMEDIATE "select " || column_name', - 'EXECUTE IMMEDIATE "unsupported SQL syntax"', + `EXECUTE IMMEDIATE r'''unsupported SQL syntax'''`, ])("preserves commands it cannot embed: %s", async (source) => { await testBigquery(source); }); diff --git a/test/embed/language_js.test.ts b/test/embed/language_js.test.ts index 3f482c5..2af48d5 100644 --- a/test/embed/language_js.test.ts +++ b/test/embed/language_js.test.ts @@ -6,7 +6,12 @@ describe("LANGUAGE js", () => { "preserves JavaScript in unsupported quote style: %s", async (body) => { await testBigquery( - `CREATE FUNCTION f()\nRETURNS INT64\nLANGUAGE js\nAS ${body}`, + dedent` + CREATE FUNCTION f() + RETURNS INT64 + LANGUAGE js + AS ${body} + `, ); }, ); diff --git a/test/embed/language_plpgsql.test.ts b/test/embed/language_plpgsql.test.ts index e2a99ce..725b9e1 100644 --- a/test/embed/language_plpgsql.test.ts +++ b/test/embed/language_plpgsql.test.ts @@ -35,7 +35,7 @@ describe("LANGUAGE plpgsql", () => { dedent` ${code} LANGUAGE plpgsql - AS 'BEGIN SELECT ''foo''; END' + AS 'BEGIN RETURN 1; END' `, { sqlExperimentalPlpgsql: true }, ); diff --git a/test/embed/language_sql.test.ts b/test/embed/language_sql.test.ts index cd3d27f..8b84cf6 100644 --- a/test/embed/language_sql.test.ts +++ b/test/embed/language_sql.test.ts @@ -35,7 +35,7 @@ describe("LANGUAGE sql", () => { dedent` ${code} LANGUAGE sql - AS 'SELECT ''foo''' + AS 'SELECT 1' `, ); }); @@ -66,7 +66,7 @@ describe("LANGUAGE sql", () => { dedent` ${code} LANGUAGE Sql - AS $$select 1$$ + AS $$SELECT 1$$ `, { dialect: "postgresql" }, ), From f980eddff1497fcd297388b568709cf03a31b749 Mon Sep 17 00:00:00 2001 From: Arno Dirlam Date: Wed, 11 Nov 2026 14:39:56 +0100 Subject: [PATCH 3/3] Embedding: Remove obsolete delimiter-selection tests --- test/embed/execute.test.ts | 6 ------ test/embed/language_js.test.ts | 11 ----------- test/embed/language_plpgsql.test.ts | 11 ----------- test/embed/language_sql.test.ts | 10 ---------- 4 files changed, 38 deletions(-) diff --git a/test/embed/execute.test.ts b/test/embed/execute.test.ts index cb36bdf..4f37b28 100644 --- a/test/embed/execute.test.ts +++ b/test/embed/execute.test.ts @@ -152,12 +152,6 @@ describe("BigQuery EXECUTE IMMEDIATE embedding", () => { `); }); - it("preserves commands containing both triple-quote delimiters", async () => { - await testBigquery( - `EXECUTE IMMEDIATE ${JSON.stringify(`select "'''", '"""'`)}`, - ); - }); - it.each([ 'EXECUTE IMMEDIATE "select @id"', "EXECUTE IMMEDIATE r'select ?'", diff --git a/test/embed/language_js.test.ts b/test/embed/language_js.test.ts index 2af48d5..c17a4b5 100644 --- a/test/embed/language_js.test.ts +++ b/test/embed/language_js.test.ts @@ -70,15 +70,4 @@ describe("LANGUAGE js", () => { """ `); }); - - it(`does not reformat JavaScript when neither ''' or """ can be easily used for quoting`, async () => { - await testBigquery( - dedent` - CREATE FUNCTION contains_quotes(x STRING) - RETURNS FLOAT64 - LANGUAGE js - AS " return /'''|\\"\\"\\"/.test(x) " - `, - ); - }); }); diff --git a/test/embed/language_plpgsql.test.ts b/test/embed/language_plpgsql.test.ts index 725b9e1..6555551 100644 --- a/test/embed/language_plpgsql.test.ts +++ b/test/embed/language_plpgsql.test.ts @@ -41,17 +41,6 @@ describe("LANGUAGE plpgsql", () => { ); }); - it(`does not reformat single-quoted SQL string when its source contains $$-quotes`, async () => { - await testPostgresql( - dedent` - ${code} - LANGUAGE plpgsql - AS 'BEGIN SELECT $$foo$$; END' - `, - { sqlExperimentalPlpgsql: true }, - ); - }); - it(`does not reformat E'quoted' strings`, async () => { await testPostgresql( dedent` diff --git a/test/embed/language_sql.test.ts b/test/embed/language_sql.test.ts index 8b84cf6..8b9716e 100644 --- a/test/embed/language_sql.test.ts +++ b/test/embed/language_sql.test.ts @@ -40,16 +40,6 @@ describe("LANGUAGE sql", () => { ); }); - it(`does not reformat single-quoted SQL string when its source contains $$-quotes`, async () => { - await testPostgresql( - dedent` - ${code} - LANGUAGE sql - AS 'SELECT $$foo$$' - `, - ); - }); - it(`does not reformat E'quoted' strings`, async () => { await testPostgresql( dedent`