Skip to content

Commit 6d4de97

Browse files
rtolsmaclaude
andcommitted
fix: Escape backticks, \${ and backslashes in generated query literals
Query text is emitted inside a template literal, so MySQL backtick-quoted identifiers (and any backslash or "${" sequence in a SQL string literal) terminated or corrupted the literal and produced unparseable or wrong generated code. The raw template text now escapes backslashes, backticks, and "${" while the runtime string value stays byte-identical to the original SQL. Verified by evaluating the emitted literal for a MySQL query using backtick quoting and a literal containing "${" and backslashes, and comparing against the source SQL. Existing example output is unchanged. Fixes #40. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GFyrFwpJC2eivaBRyNBJqv
1 parent 74e1a5e commit 6d4de97

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

‎src/app.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,13 @@ function readInput(): GenerateRequest {
239239
}
240240

241241
function queryDecl(name: string, sql: string) {
242+
// Escape characters that have special meaning inside a template literal so
243+
// queries containing backticks (MySQL quoting), "${" or backslashes still
244+
// produce the original SQL text at runtime
245+
const raw = sql
246+
.replace(/\\/g, "\\\\")
247+
.replace(/`/g, "\\`")
248+
.replace(/\$\{/g, "\\${");
242249
return factory.createVariableStatement(
243250
[factory.createToken(SyntaxKind.ExportKeyword)],
244251
factory.createVariableDeclarationList(
@@ -247,7 +254,7 @@ function queryDecl(name: string, sql: string) {
247254
factory.createIdentifier(name),
248255
undefined,
249256
undefined,
250-
factory.createNoSubstitutionTemplateLiteral(sql, sql)
257+
factory.createNoSubstitutionTemplateLiteral(sql, raw)
251258
),
252259
],
253260
NodeFlags.Const //| NodeFlags.Constant | NodeFlags.Constant

0 commit comments

Comments
 (0)