Skip to content

Commit ea5bdf2

Browse files
committed
Change detectQuote() to work like detectQuotes() in embedJs.ts for consistency
1 parent 88c58b8 commit ea5bdf2

1 file changed

Lines changed: 19 additions & 20 deletions

File tree

src/embedSql.ts

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
CreateFunctionStmt,
44
CreateProcedureStmt,
55
Node,
6-
StringLiteral
6+
StringLiteral,
77
} from "sql-parser-cst";
88
import {
99
isAsClause,
@@ -14,7 +14,10 @@ import {
1414
} from "./node_utils";
1515
import { hardline, indent, stripTrailingHardline } from "./print_utils";
1616

17-
export const embedSql: NonNullable<Printer<Node>["embed"]> = (path, options) => {
17+
export const embedSql: NonNullable<Printer<Node>["embed"]> = (
18+
path,
19+
options,
20+
) => {
1821
const node = path.node;
1922
const parent = path.getParentNode(0);
2023
const grandParent = path.getParentNode(1);
@@ -26,19 +29,9 @@ export const embedSql: NonNullable<Printer<Node>["embed"]> = (path, options) =>
2629
grandParent.clauses.some(isSqlLanguageClause)
2730
) {
2831
return async (textToDoc) => {
29-
let quote = detectQuote(node);
30-
32+
const quote = detectQuote(node);
3133
if (!quote) {
32-
return;
33-
}
34-
35-
if (quote === "'") {
36-
// Convert `'` quotes to `$$` to simplify handling of strings inside the
37-
// function. But bail out if the function contains dollar-quoted strings.
38-
if (node.value.includes("$$")) {
39-
return;
40-
}
41-
quote = "$$";
34+
return undefined;
4235
}
4336

4437
const sql = await textToDoc(node.value, options);
@@ -56,12 +49,18 @@ export const embedSql: NonNullable<Printer<Node>["embed"]> = (path, options) =>
5649
};
5750

5851
const isSqlLanguageClause = (
59-
clause: CreateFunctionStmt["clauses"][0] | CreateProcedureStmt['clauses'][0],
60-
): boolean => isLanguageClause(clause) && clause.name.name.toLowerCase() === "sql";
52+
clause: CreateFunctionStmt["clauses"][0] | CreateProcedureStmt["clauses"][0],
53+
): boolean =>
54+
isLanguageClause(clause) && clause.name.name.toLowerCase() === "sql";
6155

62-
const detectQuote = (
63-
node: StringLiteral,
64-
): string | undefined => {
56+
const detectQuote = (node: StringLiteral): string | undefined => {
6557
const match = node.text.match(/^('|\$[^$]*\$)/);
66-
return match?.[1];
58+
const quote = match?.[1];
59+
if (quote === "'") {
60+
// Convert `'` quotes to `$$`.
61+
// But bail out if the code already contains $$.
62+
return node.value.includes("$$") ? undefined : "$$";
63+
} else {
64+
return quote;
65+
}
6766
};

0 commit comments

Comments
 (0)